var request = null;
function MakeAJAXRequest(url, async, responseEventHandler)
{
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	else alert("Your browser does not support Ajax!");	
	
	request.open("GET", url, async);
	if(async) request.onreadystatechange = responseEventHandler;
	request.send(null);
}

function InitGamePage()
{
	/*CenterFlash();*/
	//DrawRateGameControl();
	//FillGameLinkTextBox();
}

function CenterFlash()
{
	var gameObject = document.getElementById("game").getElementsByTagName("object")[0];
	var gameEmbed = document.getElementById("game").getElementsByTagName("embed")[0];
	var gameIFrame = document.getElementById("game").getElementsByTagName("iframe")[0];
	
	gameObject.style.marginTop = Math.ceil((600 - gameObject.height) / 2) + "px";
	gameEmbed.style.marginTop = Math.ceil((600 - gameEmbed.height) / 2) + "px";
	if(gameIFrame) gameIFrame.style.marginTop = Math.ceil((600 - gameIFrame.height) / 2) + "px";
}

function FillGameLinkTextBox()
{
	var gameLinkTextBox = document.getElementById("gameLinkTextBox");
	if(!gameLinkTextBox) return;
	document.getElementById("gameLinkTextBox").value = '<a href="' + location.href + '">' + gameLinkTextBox.getAttribute("name") + '</a>';
}

function DrawRateGameControl()
{
	var container = document.getElementById("rateGameControl");
	
	if(container)
	{
		var html = "";
		
		container.setAttribute("onmouseout", "MouseOverRating(0)");
		html += '<img src="/graphics/rating_star_empty.gif" onmouseover="MouseOverRating(1)"  onclick="RateGame(1)" />';
		html += '<img src="/graphics/rating_star_empty.gif" onmouseover="MouseOverRating(2)"  onclick="RateGame(2)" />';
		html += '<img src="/graphics/rating_star_empty.gif" onmouseover="MouseOverRating(3)"  onclick="RateGame(3)" />';
		html += '<img src="/graphics/rating_star_empty.gif" onmouseover="MouseOverRating(4)"  onclick="RateGame(4)" />';
		html += '<img src="/graphics/rating_star_empty.gif" onmouseover="MouseOverRating(5)"  onclick="RateGame(5)" />';
		container.innerHTML = html;
	}	
}

function MouseOverRating(currentRating)
{
	var ratingStars = document.getElementById("rateGameControl").getElementsByTagName("img");
	
	currentRating = parseInt(currentRating);
	if(!currentRating) currentRating = 0;	
	
	for(var i = 0; i < 5; i++)
		if(i < currentRating) ratingStars[i].src = "/graphics/rating_star_full.gif";
		else ratingStars[i].src = "/graphics/rating_star_empty.gif";
}

function RateGame(rating)
{
	document.getElementById("rateGameControl").setAttribute("onmouseout", "");
	
	var ratingStars = document.getElementById("rateGameControl").getElementsByTagName("img");
	for(var i = 0; i < ratingStars.length; i++)
	{
		ratingStars[i].setAttribute("onmouseover", "");
		ratingStars[i].setAttribute("onclick", "");
	}
	
	var nameid = document.getElementById("rateGameControl").getAttribute("nameid");
	MakeAJAXRequest("/server/server.php?action=rate_game&nameid=" + nameid + "&rating=" + rating);
	//alert(request.responseText);
}

function GameWindow(url, width, height, isFullscreen)
{
	if(isFullscreen)
	{
		width = screen.availWidth;
		height = screen.availHeight;
		window.open(location.href + "?fullscreen", "fullscreenWindow", "width=" + width + ",height=" + height + ",menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0");
	}
	else window.open(url, "gameWindow", "width=" + width + ", height=" + height + ", menubar=0, toolbar=0");
}

function ShareOnIWIW()
{
	u = location.href;
	t = document.title;
	window.open("http://iwiw.hu/pages/share/share.jsp?u=" + encodeURIComponent(u) + "&amp;t=" + encodeURIComponent(t), "iwiwshare","toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1");
}

function AddToStartlap()
{
	window.open("http://www.startlap.hu/sajat_linkek/addlink.php?url=" + encodeURIComponent(location.href) + "&title=" + encodeURIComponent(document.title));
}

function ShareOnFacebook()
{
	var url = encodeURIComponent(location.href);
	var title = encodeURIComponent(document.title);
	window.open("http://www.facebook.com/sharer.php?u=" + url + "&t=" + title, "facebookShareWindow", "width=640, height=480, menubar=0, toolbar=0");
}