// AJAX based voting for BootyArcade
// copyright Moonbug Studio
// hap@moonbugstudio.com

// mouse positions
var posX = -1;
var posY = -1;

// generic ajax flavored juice
function GetXmlHttpObject()
{
	var xmlHttp=null;
	if (window.XMLHttpRequest)
 	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
 	}
	else if(window.ActiveXObject)
	{
		//Internet Explorer
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlHttp;
}

// vote function -- calls insertrating.php without reloading the page for AJAX power
function voteNow(rank, gameid, userid)
{
	// go away
	if (gameid.length == 0)
  		return;
  		
  	// go away
	if (rank < 1 || rank > 5)
  		return;	
  		
 	document.cookie = 'dfg_' + gameid + '=1; expires=Fri, 31 Dec 2099 23:59:59 GMT; path=/'; 		

	hideQuotes();	  		
  		
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp == null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 

  	url = "http://www.dumbfreegames.com/includes/insertrating.php?rank=" + rank + "&gameid=" + gameid + "&userid=" + userid;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = voteSaved;
	xmlHttp.send(null);	
}

// AJAX flavored result/save message for after voting
function voteSaved()
{
	v = document.getElementById("voteInfo");
	v.innerHTML = "Thanks for voting."			
}

// popup quote box
function showQuote(obj, spanObj)
{
	if(!obj)
		return;

	if(obj != 'rate1')
		hideQuote('rate1');
	if(obj != 'rate2')
		hideQuote('rate2');
	if(obj != 'rate3')		
		hideQuote('rate3');
	if(obj != 'rate4')	
		hideQuote('rate4');
	if(obj != 'rate5')	
		hideQuote('rate5');		
		
	posX = findPosX(spanObj);
	posY = findPosY(spanObj);
	
	posX = posX - 100;	
	posY = posY - 160;


	
	m = document.getElementById(obj).style;	
	
	m.left = posX;
	m.top = posY;

	if(document.all)	// IE
	{
		m.width = 243;
		m.height = 159;
	}
	else				// FF
	{
		m.width = 138;
		m.height = 139;
	}	

	m.visibility = 'visible';
	
	//setTimeout("showQuote3('"+obj+"');", 1);		
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		var repeat = true;
		while (repeat == true)
		{
			if(obj.offsetParent == null) repeat = false;
			else
			{
		            	curleft = curleft + obj.offsetLeft;
		            	obj = obj.offsetParent;
		        }
	        }
	}
	else if (obj.x)
	{
		curleft = curleft + obj.x;
	}
	return curleft;
}
function findPosY(obj)
{
	var curTop = 0;
	if (obj.offsetParent)
	{
		var repeat = true;
		while (repeat == true)
		{
			if(obj.offsetParent == null) repeat = false;
			else
			{
		            	curTop = curTop + obj.offsetTop;
		            	obj = obj.offsetParent;
		        }
	        }
	}
	else if (obj.x)
	{
		curTop = curTop + obj.y;
	}
	return curTop;
}


// close popup
function hideQuotes()
{
	hideQuote('rate1');
	hideQuote('rate2');
	hideQuote('rate3');
	hideQuote('rate4');
	hideQuote('rate5');
}

// close popup
function hideQuote(obj)
{
	if(!obj)
		return;	
		
	m = document.getElementById(obj).style;
	
	m.visibility = 'hidden';
}