// AJAX based voting for BootyArcade
// copyright Moonbug Studio
// hap@moonbugstudio.com

// mouse positions
var posX = -1;
var posY = -1;
var xmlHttp = null;

// 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)
{
	// go away
	if (gameid.length == 0)
  		return;

  	// go away
	if (rank < 1 || rank > 5)
  		return;

 	document.cookie = 'bootyarcade_' + 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 = "includes/insertrating5c2b.html?rank=" + rank + "&gameid=" + gameid;

	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange=function()
	{
  		if (xmlHttp.readyState==4)
  		{
			v = document.getElementById("voteInfo");
			v.innerHTML = "Thanks for voting."
		}
	}
	xmlHttp.send(null);
}

// get the mouse position (used by quote box)
function mousePos()
{
    if(document.all)	// IE
	{
		// posX = window.event.x + document.body.scrollLeft + document.documentElement.scrollLeft;
		// posY = window.event.y + document.body.scrollTop	+ document.documentElement.scrollTop;
		posX = window.event.x - 200;
		posY = window.event.y - 180;
	}
	else				// FF
	{
        window.captureEvents(Event.MOUSEMOVE);
  		window.onmousemove = mousePosFF;
	}
}

function mousePosFF(e)
{
	posX = e.pageX - 508;
    posY = e.pageY - 663;
}

// popup quote box
function showQuote(obj)
{
	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');

	mousePos();

	if(posX == -1 || posY == -1) // FF
		mousePos();

    m = document.getElementById(obj).style;

	m.left = posX + "px";
	m.top = posY + "px";

	m.visibility = 'visible';

	//setTimeout("showQuote3('"+obj+"');", 1);
}

// 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';
}
