//Created by Sean Kane (http://celtickane.com/programming/code/ajax.php)
//Feather Ajax v1.0.1

function AjaxObject101() {
	this.createRequestObject = function() {	//This is called at the very bottom -- it sets this.http to the Ajax request object (ro)
		var ro;	//This will hold the request object -- either Microsoft.XMLHTTP or XMLHttpRequest
		try {	//Let's use a try/catch system just in case something goes wrong -- we can at least default back to the XMLHttpRequest object
			ro = new XMLHttpRequest();
		}
		catch (e) {
			ro = new ActiveXObject("Microsoft.XMLHTTP"); //We'll at least try the IE6 and lower version	
		}
		return ro;	//Now that we've taken care of cross-browser compatibility, this.http will represent the request object for this instance of AjaxObject
	}
	this.sndReq = function(action, url, data) { //This function will start the Ajax process, and is called manually by the user
		if (action.toUpperCase() == "POST") { //Post method
			this.http.open(action,url,true); //The URL includes any GET or POST variables you want
			this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); //Encode the data
			this.http.onreadystatechange = this.handleResponse; //This is the callback function when the state of the http object changes
			this.http.send(data);	//Actually start the request process -- this will contact the URL via the action specified, and wait to call this.handleResponse
		}
		else {	//'get' method is default
			this.http.open(action,url + '?' + data,true); //The URL includes any GET or POST variables you want
			this.http.onreadystatechange = this.handleResponse; //This is the callback function when the state of the http object changes
			this.http.send(null);	//Actually start the request process -- this will contact the URL via the action specified, and wait to call this.handleResponse
		}	
		//action is either 'get' or 'post'
		//url can contain GET variables -- like myajax.php?action=test
	}
	this.handleResponse = function() { //This function is called when this.http's state changes
		if ( me.http.readyState == 4) { //State of 4 means the connection is done (data was transferred)
			if (typeof me.funcDone == 'function') { me.funcDone();} //Execute the funcDone function if it's been set by the user
			var rawdata = me.http.responseText.split("|"); //If there aren't any |'s in the string, it will just grab the entire string and put it into items[0]
			for ( var i = 0; i < rawdata.length; i++ ) { //Loop through the rawdata[] array
				var item = (rawdata[i]).split("=>");	//Split each item into id=>value where id is item[0] and the value is item[1]
				if (item[0] != "") {	//If it is a valid HTML id
					if (item[1].substr(0,3) == "%V%" ) { //When sending a response, if you want to change the .value of an item (rather than .innerHTML), preface your response with %V%
						document.getElementById(item[0]).value = item[1].substring(3);  //Set the value property of the given HTML item to the value of item[1]
					}
					else {
						document.getElementById(item[0]).innerHTML = item[1]; //Set the innerHTML property of the given HTML item to the value of item[1]
					}
					//item[0] is the id of the HTML element, item[1] is the value of that HTML element
				}
			}
		}
		if ((me.http.readyState == 1) && (typeof me.funcWait == 'function')) { //This code will run if funcWait is pointed at a function -- this could call a spinner icon, or a "Please wait..." label
			me.funcWait();
		}
	}
	var me = this;	//Unfortunately, this is necessary because this.http won't work in the callback function 'handleResponse' (we have to use me.http instead)
	this.http = this.createRequestObject(); //http holds the request object (ro), which is returned from the createRequestObject() function -- this is automatically run when you make a new AjaxObject()
	
	var funcWait = null;
	var funcDone = null;
}

function open_window(url,name,wth,hgt,display)
{
	if(wth == 'full'){
		sgswindow = window.open(url);
	}else{
		var winwidth = 800;
		var winheight = 600;
		var winname='';
		var windisplay = ',scrollbars=yes,menubar=yes,status=yes';
		
		if(wth){ winwidth = wth; }
		if(hgt){ winheight = hgt; }
		if(name){ winname = name; }
		
		if(display && display == 'plain'){
			windisplay = ',scrollbars=no,menubar=no,status=no';
		}
		sgswindow = window.open(url,winname, 'top=100,left=100,resizable=yes,width='+winwidth+',height='+winheight+windisplay);
	}
	sgswindow.focus();
}



function Generateobj(src,width,height,alt,id,Flashvars,js,style){ 
	
  	var str = '';
 	str += '<!--[if IE]>\n';
	str += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+ width +'" height="'+ height +'"'+(id !='' ? 'id="'+ id +'"' : '')+(style !='' ? 'class="'+ style +'"' : '')+ js +'>\n';
	str += '<param name="movie" value="'+ src +'" />\n';
	str += '<param name="FlashVars" value="'+ Flashvars +'" />\n';
	str += '<param name="quality" value="best" />\n';
	str += '<param name="wmode" value="transparent" />\n';
	str += (alt !='' ? '+ alt +' : '')+'\n';	
	str += '</object>\n';
	str += '<![endif]-->\n';
	str += '<!--[if !IE]> <-->\n';
	str += '<object type="application/x-shockwave-flash" data="'+ src +'" width="'+ width +'" height="'+ height +'"'+(id !='' ? 'id="'+ id +'"' : '')+(style !='' ? 'class="'+ style +'"' : '')+ js +'>\n';
	str += '<param name="FlashVars" value="'+ Flashvars +'" />\n';	
	str += '<param name="quality" value="best" />\n';
	str += '<param name="wmode" value="transparent" />\n';
	str += (alt !='' ? '+ alt +' : '')+'\n';
	str += '</object>\n';
	str += '<!--> <![endif]-->\n'; 
	document.write(str);
}

function flowerMove(ev) {

	if (starsState == 1) {

		var offsetX;

		if (ev && ev != 'exp') {

			var coords = { x: 0, y: 0};
			var element = document.getElementById('flowers');

			while (element) {
				coords.x += element.offsetLeft;
				coords.y += element.offsetTop;
				element = element.offsetParent;
			}

			offsetX = ev.clientX - coords.x;

		} else {
			offsetX = event.offsetX;
		}

		var rating = Math.round(offsetX * 11 / starsWidth - 0.5);

		if (rating < 0)
			rating = 0;
		else if (rating > 10)
			rating = 10;

		starsRating = rating / 10;

		document.getElementById('flowers').style.backgroundPosition = "0 " + (-rating * 37) + "px";
	}

	return true;
}

function gameClicked(gamename,shortname)
{
	var ao = new AjaxObject101(); 
	ao.sndReq("post", "ajax.php", "type=gameClick&gamename=" + urlencode(gamename) + "&shortname=" + urlencode(shortname));
}

function flowerClick(game_name) {

	if (starsState == 0) {
		document.getElementById('votepanel2').innerHTML = "(vote by choosing a rating and clicking)";
		if (Event) {
			document.getElementById('flowers').onmousemove = flowerMove;
		}
		starsState ++;
	} else if (starsState == 1) {
		starsState ++;
		document.getElementById('votepanel2').innerHTML = "Thank you for voting!";
		your = starsRating * 5;
		document.getElementById('votepanel1').innerHTML = "Your rating: " + your;
		
		var ao = new AjaxObject101(); 
		ao.sndReq("post", "ajax.php", 
					"type=ratingForm&gamename=" + urlencode(game_name) + 
					"&rating=" + starsRating);
	}
}

function nullfunc(s)
{
	
}

function urlencode(s)
{
	if (s == null)
		return null;
	return escape(s);
}

starsState = 0;
starsRating = 0;
var starsWidth = 171
var Event;


