var GameId, GameUrl, GameTitle, GameHeight, GameWidth, GameResize, GameHtml, MyRating, PageHeight, PageWidth;

var PlayBtn = new Array(1);
PlayBtn[0] = new Image(250,250); PlayBtn[0].src = "http://games.dork.com/images/play-1.png";
PlayBtn[1] = new Image(250,250); PlayBtn[1].src = "http://games.dork.com/images/play-2.png";

var StarImg = new Array(4);
StarImg[0] = new Image(16,16); StarImg[0].src = "http://dork.com/images/ratings/0.00.png";
StarImg[1] = new Image(16,16); StarImg[1].src = "http://dork.com/images/ratings/0.25.png";
StarImg[2] = new Image(16,16); StarImg[2].src = "http://dork.com/images/ratings/0.50.png";
StarImg[3] = new Image(16,16); StarImg[3].src = "http://dork.com/images/ratings/0.75.png";
StarImg[4] = new Image(16,16); StarImg[4].src = "http://dork.com/images/ratings/1.00.png";

function FindPageHeight()
{
	var DocBody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
	var Ie = document.all && !window.opera;
	return (Ie) ? DocBody.clientHeight : window.innerHeight;
}

function FindPageWidth()
{
	var DocBody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
	var Ie = document.all && !window.opera;
	return (Ie) ? DocBody.clientWidth : window.innerWidth;
}

function PlayBtnOut()
{
	var o = document.getElementById("playBtn");
	o.src = PlayBtn[0].src;
}

function PlayBtnOver()
{
	var o = document.getElementById("playBtn");
	o.src = PlayBtn[1].src;
}

function PlayGame(Id)
{
	PageHeight = FindPageHeight();
	PageWidth = FindPageWidth();
	
	var g = GameHtml;
	var b = 100;
	var vPad = 40;//Veil's padding on top and bottom.
	var hPad = 20;//Veil's padding on left and right.
	
	if (GameResize == true)
	{
		if (GameWidth > GameHeight)
		{
			var r = GameWidth/GameHeight;
			var w = PageWidth-b;
			var h = parseInt(w/r);
			
			if (h+vPad > PageHeight)
			{
				h = PageHeight-b;
				w = parseInt(h*r);
			}
		}
		else
		{
			var r = GameHeight/GameWidth;
			var h = PageHeight-b;
			var w = parseInt(h/r);
			
			if (w+hPad > PageWidth)
			{
				w = PageWidth-b;
				h = parseInt(w*r);
			}
		}
		
		g = g.replace(/height=\"([0-9]*)\"/gi, "height=\"" + h + "\"");
		g = g.replace(/width=\"([0-9]*)\"/gi, "width=\"" + w + "\"");
	}
	else
		w = GameWidth;
	
	OpenVeil(w,GameTitle,g);
	
	var result = AjaxGet("http://games.dork.com/" + GameUrl + "/played/",false,true);
	//alert(result);
}

function RatingAvgLoad(x)
{
	for (var i = 1; i < 6; i++)
		document.getElementById("star_" + i + "_" + GameId).src = "http://dork.com/images/ratings/" + RatingAvgVal(x,i) + ".png";
}

function RatingAvgVal(x,y)
{
	if (x >= y) return "1.00";
	else if (x < y-1) return "0.00";
	else
	{
		x = x - parseInt(x);
		if (x == 0) return "0.00";
		else if (x <= 0.25) return "0.25";
		else if (x <= 0.50) return "0.50";
		else if (x <= 0.75) return "0.75";
		else return "1.00";
	}
}

function RatingClick(Rating)
{
	var x = AjaxGet("http://games.dork.com/" + GameUrl + "/rating/" + Rating + "/",false,true);
	
	if (x == "err=1" || x == "err=2")
		alert("There was a connection timeout. Please try your selection again. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
	else
	{
		var a = x.split(",");
		
		if (a[0] == "0")
			document.location = "http://games.dork.com/login/?url=" + escape("http://games.dork.com/" + GameUrl + "/");
		else if (a[0] == "1")
			OpenVeilVerify("leave a rating");
		else if (a[0] == "2")
			alert("An unknown exception occurred. Please reload this page and try again.")
		else
		{
			MyRating = Rating;
			RatingLoad(MyRating);
			RatingAvgLoad(parseFloat(a[1]));
			
			document.getElementById("rating").innerHTML = FormatNumber(a[1],2,0) + " / 5";
			document.getElementById("votes").innerHTML = FormatNumber(a[2],0,0) + " Votes";
		}
	}
}

function RatingLoad(x)
{
	for (var i = 1; i < 6; i++) document.getElementById("MyStar" + i).src = "http://dork.com/images/ratings/" + ((x >= i) ? "1.00" : "0.00") + ".png";
	document.getElementById("myRating").innerHTML = (x == "0") ? "click a star" : x + " / 5";
}

function SetVars(a,b,c,d,e,f,g)
{
	GameId = a;
	GameUrl = b;
	GameTitle = c;
	GameHeight = parseInt(d);
	GameWidth = parseInt(e);
	GameResize = f;
	MyRating = g;
}

function UpdateFavorites()
{
	var o = document.getElementById("favorites");
	var x = AjaxGet("http://games.dork.com/" + GameUrl + "/favorite/",false,true);
	
	if (x == "err=1" || x == "err=2")
		alert("There was a connection timeout. Please try your selection again. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
	else if (x == "0")
		document.location = "http://games.dork.com/login/?url=" + escape("http://games.dork.com/" + GameUrl + "/");
	else if (x == "2")
		o.innerHTML = "Remove from Favorites";
	else if (x == "3")
		o.innerHTML = "Add to Favorites";
	else
		alert("An unexpected exception has occurred. Please try your selection again. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
}
