/********************** 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();
}


/********************** PullDownBox **************************/

// popup quote box
var fileData = "";

function show()
{
	clearTimeout(document.closey);

	m = document.getElementById('pulldown_hide').style;
	n = document.getElementById('pulldown_show').style;

	m.display = "none";
	n.display = "block";
}

// close popup
function hide()
{
	document.closey = setTimeout("hide2();", 500);  // give it a frame, FF needs it the first time to get mouse positions
}

function hide2()
{
    m = document.getElementById('pulldown_hide').style;
	n = document.getElementById('pulldown_show').style;

	n.display = "none";
	m.display = "block";
}

function popBlog(URL)
{
	window.open(URL,"","width=700,height=120,left=10,top=10,status=0,scrollbars=1,resizable=1,menubar=0,location=0,toolbar=0")
}

// JavaScript Document

function popUp(URL){
winpops=window.open(URL,"","width=200,height=125,left=100,top=100,status=0,scrollbars=0,resizable=1,menubar=0,location=0,toolbar=0")
}


function popUp2(URL){
winpops=window.open(URL,"","width=200,height =450,left=100,top=100,status=0,scrollbars=0,resizable=1,menubar=0,location=0,toolbar=0")
}


function popUpFavorite(URL){
winpops=window.open(URL,"","width=200,height=100,left=100,top=100,status=0,scrollbars=0,resizable=1,menubar=0,location=0,toolbar=0")
}


function popComments(URL){
winpops=window.open(URL,"","width=440,height=400,left=100,top=100,status=0,scrollbars=1,resizable=1,menubar=0,location=0,toolbar=0")
}


function popRecoverPassword(URL){
winpops=window.open(URL,"","width=440,height=200,left=100,top=100,status=0,scrollbars=0,resizable=1,menubar=0,location=0,toolbar=0")
}

function popReportGame(URL){
winpops=window.open(URL,"","width=250,height=225,left=100,top=100,status=0,scrollbars=0,resizable=1,menubar=0,location=0,toolbar=0")
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


// Nannette Thacker http://www.shiningstar.net
function confirmDeleteMessage()
{
var agree=confirm("Are you sure you wish to delete this message?");
if (agree)
	return true ;
else
	return false ;
}

/********************************************* Voting Stuff **************************************/

// 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(baseURL, 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 = baseURL + "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 = "<p style='font-weight:bold;font-size:18px;margin:0px;padding-top:5px;'>Thanks for voting!</p>";
}

// 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()
{
}


/**************************************** FEATURED ROTATOR STUFF *****************************************/
function rotateFeaturedGames()
{
	if(allowFeaturedRotator)
	{
		paginateNext();
		window.setTimeout("rotateFeaturedGames();",3000);
	}
}
function paginateOver(num)
{
	var pageDiv = document.getElementById("paginate-slider");
	pageDiv.style.backgroundPosition = "0px " + ((num + 1) * -35) + "px";
}
function paginateOut(num)
{
	var pageDiv = document.getElementById("paginate-slider");
	pageDiv.style.backgroundPosition = "0px 0px";

}
function paginateNext()
{
	if(currentFeaturedNum != 10) paginateDown(currentFeaturedNum + 1);
	else paginateDown(1);
}
function paginatePrev()
{
	if(currentFeaturedNum != 1) paginateDown(currentFeaturedNum - 1);
	else paginateDown(10);
}
function paginateDown(num)
{
	for(i = 0; i < 12; i++)
	{
		var pageImg = document.getElementById("pageImg" + i);
		if(i != num) pageImg.style.display = "none";
		else pageImg.style.display = "inline";
	}

	var prevGameImg = document.getElementById("featuredSliderImg" + currentFeaturedNum);
	var newGameImg = document.getElementById("featuredSliderImg" + num);
	currentFeaturedNum = num;
	fadeOutDiv(prevGameImg, 500, 10);
	fadeInDiv(newGameImg, 500, 10);

}
function fadeInDiv(div, speed, numTimes)
{
	var timePerCycle = speed / numTimes;
	var id = div.id;
	for(i = 0; i < numTimes; i++)
	{
		var num = (i + 1) / numTimes;
		window.setTimeout("setDivOpacity('" + id + "', '" + num + "')",(timePerCycle * i));
	}
}
function fadeOutDiv(div, speed, numTimes)
{
	var timePerCycle = speed / numTimes;
	var id = div.id;
	for(i = 0; i < numTimes; i++)
	{
		var num = (numTimes - i - 1) / numTimes;
		window.setTimeout("setDivOpacity('" + id + "', '" + num + "')",(timePerCycle * i));
	}
}
function setDivOpacity(divId, num)
{
	var div = document.getElementById(divId);
	div.style.opacity = num;
	div.style.MozOpacity = num;
	div.style.filter = 'alpha(opacity=' + (num*100) + ')';
	if(num == 0) div.style.display = "none";
	else div.style.display = "block";
}




/************************** Comments *********************************/
function showCommentsAddBox(loggedIn)
{
	if(loggedIn)
	{
		document.getElementById("game_info_comments_inner").style.display = "none";
		document.getElementById("game_info_comments_add").style.display = "block";
	}
	else
	{
		document.getElementById("game_info_comments_inner").style.display = "none";
		document.getElementById("game_info_comments_sorry").style.display = "block";
	}
}
function showCommentsBox()
{
	document.getElementById("game_info_comments_inner").style.display = "block";
	document.getElementById("game_info_comments_add").style.display = "none";
	document.getElementById("game_info_comments_sorry").style.display = "none";
}
function addComment(baseURL, gameid, userid)
{
	var comment = document.getElementById("addnewcomment").value;
	if(comment != "")
	{
		// Submit it
		xmlHttp=GetXmlHttpObject();

		if (xmlHttp == null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		}
		var params = encodeURI("gameid=" + gameid + "&userid=" + userid + "&comment=" + comment);
		url = baseURL + "insertcomment.php";
		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)
			{
				document.getElementById("game_info_comments_inner").innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.send(params);

		document.getElementById("addnewcomment").value = "";
		showCommentsBox();
	}

}
function cancelComment()
{
	document.getElementById("addnewcomment").value = "";
	showCommentsBox();
}

/********************** 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";

}
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);
	}
}


