/********************** PopUnder **************************/
var Cwidth = window.screen.width;
var Cheight = window.screen.height;

var Cleft = (window.screen.width-Cwidth)/2;
var Ctop = (window.screen.height - Cheight)/2;

var winfeatures="scrollbars=0,resizable=0,toolbar=0,location=1,menubar=0,status=0,directories=0,top=" + Ctop + ",left=" + Cleft + ",width=" + Cwidth + ",height=" + Cheight;
var thecookiename="mmotrafficpopunder";

function get_cookie(Name)
{
	var NewName = Name + "=";
	var Cookies = document.cookie.split(';');
	for(var i=0;i < Cookies.length;i++) {
		var TheCookie = Cookies[i];
		while (TheCookie.charAt(0)==' ') TheCookie = TheCookie.substring(1,TheCookie.length);
		if (TheCookie.indexOf(NewName) == 0) return TheCookie.substring(NewName.length,TheCookie.length);
	}
	return false;
}

function loadornot(baseURL)
{

	if (!get_cookie(thecookiename))
	{
		// launch ajax request
		xmlHttp=GetXmlHttpObject();
		xmlUrl = baseURL + "includes/mmotraffic.php?id=" + popunderurlid;
		xmlHttp.open("GET", xmlUrl, true);
		xmlHttp.send(null);

		loadpopunder();
		var d = new Date();
		d.setDate(d.getDate()+1);
		d.toGMTString();
		document.cookie=thecookiename+"=yes; expires=" + d + "; path=/";
	}
}

function loadpopunder()
{
	win2=window.open(popunderurl,"",winfeatures);
	win2.blur();
	window.focus();
}

/********************** Voting **************************/


// mouse positions
var posX = -1;
var posY = -1;
var xmlHttp = null;

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(base_url, rank, gameid)
{
	// go away
	if (gameid == null)	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 = base_url + "includes/insertrating.php?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 - 100;
		posY = window.event.y - 200;
	}
	else				// FF
	{
        window.captureEvents(Event.MOUSEMOVE);
  		window.onmousemove = mousePosFF;
	}
}

function mousePosFF(e)
{
	posX = e.pageX - 508;
    posY = e.pageY - 563;
}

// 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';
}

/********************** Game Thumbs **************************/

function gameThumbOver(div)
{
	var titleDiv = div.firstChild;
	// titleDiv.firstChild.innerHTML = titleDiv.lang;
	setDivOpacity(titleDiv, .8);
}
function gameThumbOut(div)
{
	var titleDiv = div.firstChild;
	setDivOpacity(titleDiv, 0);

}
function setDivOpacity(div, num)
{
	div.style.opacity = num;
	div.style.MozOpacity = num;
	div.style.filter = 'alpha(opacity=' + (num*100) + ')';
}

/********************** Load Game **************************/

function doIt(base_url, gameid)
{
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var params = encodeURI("gameid=" + gameid);
	var rand = Math.random();
	url = base_url + "index.php?action=getgame&rand=" + rand;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
		{
			fileData = xmlHttp.responseText.split("|~|")[0];
			layoutData = xmlHttp.responseText.split("|~|")[1];
			// layoutData = xmlHttp.responseText;
			var gameDiv = document.getElementById("gameOuterDiv");
			if(gameDiv != null)
			{
				// document.getElementById("texter").value = layoutData;
				gameDiv.innerHTML = layoutData;
				loadGame();
			}
		}
	}
	xmlHttp.send(params);

}
function closePreRollAd(kill)
{
	if(kill)
	{
		document.getElementById("preroll_div").innerHTML = "";
		document.getElementById("gameMiddleDiv").innerHTML = "";

	}
	else
	{
		document.getElementById("preroll_div").style.display = "none";
		document.getElementById("gameMiddleDiv").style.display = "none";
	}
	document.getElementById("gameOuterDiv").style.height = calcGameHeight + "px";
	// if(unlocker != null) unlocker("#gameInnerDiv");

}
function loadGame()
{
	var iframe = document.getElementById('gameIframe').contentWindow;
	var gameDiv = iframe.document.getElementById('gameDiv');
	if(gameDiv == null)
	{
		setTimeout("loadGame();", 200);
	}
	else
	{
		gameDiv.innerHTML = fileData;
		// setTimeout("closePreRollAd();", 50000);
	}
}

