var scorePopupTimeOut = 0;
var highScorePopupTimeOut = 0;
var starPopupTimeOut = 0;
var preRollTimeOut = 0;
var preRollTime = 15000;
var stepSize = preRollTime / 50;
var prerollComplete; //is called when preroll is done.
function submitScore(score, hash)
{
	if(isVerified)
	{
		submitScoreRequest(score, hash, null);
	}
	else
	{
		if(score > bottomScore)
		{
			var submitForm = document.getElementById("submitHighScoreForm");
			submitForm.score.value = score;
			submitForm.hash.value = hash;
			setNodeText("highScorePopupYourScore", score);
			highScorePopupTimeOut = 30;
			setNodeText("highScorePopupTimeOut", " " + highScorePopupTimeOut + " ");
			popup("highScorePopup");
			document.submitHighScoreForm.name.focus();
			setTimeout("highScorePopupInterval()", 1000);
		}
		else
		{
			setNodeText("scorePopupYourScore", score);
			setNodeText("scorePopupTopScore", topScore);
			scorePopupTimeOut = 5;
			setNodeText("scorePopupTimeOut", " " + scorePopupTimeOut + " ");
			popup("scorePopup");
			setTimeout("scorePopupInterval()", 1000);
		}
	}
}

function submitLevel(level, hash)
{
	if(isVerified)
	{
		submitScoreRequest(level, hash, null);
	}
}

function scorePopupInterval()
{
	scorePopupTimeOut--;
	if(scorePopupTimeOut <= 0)
	{
		document.getElementById("scorePopup").style.display = "none";
	}
	else
	{
		setNodeText("scorePopupTimeOut", " " + scorePopupTimeOut + " ");
		setTimeout("scorePopupInterval()", 1000);
	}
}

function highScorePopupInterval()
{
	highScorePopupTimeOut--;
	if(highScorePopupTimeOut <= 0)
	{
		document.getElementById("highScorePopup").style.display = "none";
	}
	else
	{
		setNodeText("highScorePopupTimeOut", " " + highScorePopupTimeOut + " ");
		setTimeout("highScorePopupInterval()", 1000);
	}
}

function starPopupInterval()
{
	starPopupTimeOut--;
	if(starPopupTimeOut <= 0)
	{
		document.getElementById("starCollectedArea").style.display = "none";
	}
	else
	{
		setTimeout("starPopupInterval()", 1000);
	}
}

function submitScoreForm()
{
	document.getElementById("highScorePopup").style.display = "none";
	var submitForm = document.getElementById("submitHighScoreForm");
	var n = submitForm.name.value;
	if(n.length < 1)
		n = "Anonymous";
	submitScoreRequest(submitForm.score.value, submitForm.hash.value, n);
	return false;
}

function submitScoreRequest(score, hash, name)
{
    var reqvars = "action=submit&"
	+ "gameId=" + gameId + "&"
	+ "score=" + score + "&"
	+ "hash=" + encodeURIComponent(hash) + "&";


    if(name != null)
    	reqvars += "name=" + encodeURIComponent(name) + "&";
	var me = new Mess(reqvars,"submitScoreResponse");
	me.url = "/servlet/FreeGameServlet";
	
	makeHttpRequest(me, false);
}

function submitScoreResponse(msg, doc)
{
	var response = eval("(" + doc + ")");
	if(response.topScore != null)
		topScore = response.topScore;

	if(response.bottomScore != null)
		bottomScore = response.bottomScore;

	if(response.hsl != null)
		buildHighscoreList(response.hsl);

	if(response.starsBefore != null && response.starsAfter != null)
	{
		if(response.starsAfter > response.starsBefore)
		{
			/*
		    var starSwf = new SWFObject(mirrorHostname + "/images/StarCollected.swf?starCollectedText=" + newStarText, "star", 200, 200, "8");
		    starSwf.useExpressInstall('/expressinstall.swf');
		    starSwf.addParam("wmode", "opaque");
		    starSwf.write("newStarDiv");
			*/
			starPopupTimeOut = 6;
			setTimeout("starPopupInterval()", 1000);
			var newStarPopup = document.getElementById("starCollectedArea");
			newStarPopup.style.display = "";
		}
	}
	if(response.played)
	{
		if(response.newBestScore)
			updateScore(response.played, response.score, response.starsAfter);
		else
			updateScore(response.played, null, null);
	}
}

function submitPlayRequest()
{
    var reqvars = "action=play&"
	+ "gameId=" + gameId + "&";

	var me = new Mess(reqvars,"submitPlayResponse");
	me.url = "/servlet/FreeGameServlet";
	makeHttpRequest(me, false);
}

function submitPlayResponse(msg, doc)
{
	var response = eval("(" + doc + ")");
	if(response.played)
	{
		updateScore(response.played, null, null);
	}
}

function buildHighscoreList(hsl)
{
	var highScoreList = document.getElementById("highScoreList");
	highScoreList.innerHTML = "";

	for(var i = 0; i < hsl.length; i++)
	{
		var rowDiv = document.createElement("div");

		var flagDiv = document.createElement("div");
		flagDiv.className = "hsFlagDiv";
		var img = document.createElement("img");
		img.src = mirrorHostname + "/images/flags/" + hsl[i].countryCode.toLowerCase() + ".gif"
		flagDiv.appendChild(img);
		rowDiv.appendChild(flagDiv);

		var nameDiv = document.createElement("div");
		nameDiv.className = "hsNameDiv";
		nameDiv.appendChild(document.createTextNode(hsl[i].name));
		rowDiv.appendChild(nameDiv);

		var scoreDiv = document.createElement("div");
		scoreDiv.className = "hsScoreDiv";
		scoreDiv.appendChild(document.createTextNode(hsl[i].score));
		rowDiv.appendChild(scoreDiv);

		highScoreList.appendChild(rowDiv);

		var br = document.createElement("br");
		br.className = "reset";
		highScoreList.appendChild(br);
	}
}

function updateScore(played, score, stars)
{
	setNodeText("playsStatsDiv", played);
	if(score != null)
		setNodeText("scoreStatsDiv", score);

	if(stars != null)
	{
		var starString = "";
		var i = 1;
		var starStatsDiv = document.getElementById("starStatsDiv");
		starStatsDiv.innerHTML = "";
		for(i = 1; i<=stars; i++)
		{
			var img = document.createElement("img");
			img.src = mirrorHostname + "/images/star_collected_small.gif";
			img.className = "smallStarImg";
			starStatsDiv.appendChild(img);
			//var img2 = document.getElementById("starImg" + i);
			//img2.src = mirrorHostname + "/images/star_collected_small.gif";
		}
		for( ; i<=5; i++)
		{
			var img = document.createElement("img");
			img.src = mirrorHostname + "/images/star_not_collected_small.gif";
			img.className = "smallStarImg";
			starStatsDiv.appendChild(img);
		}
	}
}

function submitAchievement(achievement, hash)
{
	alert("achievement: " + achievement + "\nhash: " + hash);
}

function popup(popupName)
{
		//	alert("popup");

	var popupDiv = document.getElementById(popupName);

	var width = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  	var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

	popupDiv.style.display = "block";
	popupDiv.style.position = "absolute";
	popupDiv.style.left = (Math.round(document.documentElement.scrollLeft+(width-popupDiv.clientWidth )/2))+"px";
    popupDiv.style.top = (Math.round(document.documentElement.scrollTop+(height-popupDiv.clientHeight)/2))+"px";
}

function setNodeText(nodeId, text)
{
	var node = document.getElementById(nodeId);
	if(node)
	{
		node.innerHTML = "";
		node.appendChild(document.createTextNode(text));
	}
}


function loadGame()
{
	submitPlayRequest();
	var gameContainer = document.getElementById("gameContainer");
		gameContainer.style.display = "";
	var lowerWide = document.getElementById("lowerWide");
	if(lowerWide && recomendedGames)
		lowerWide.appendChild(recomendedGames);
	var recomendedGames = document.getElementById("recomendedGames");
	var preRoll = document.getElementById("preRoll");
		preRoll.style.display = "none";

	var internalBanner =document.getElementById("internalBanner")
	if(internalBanner)
		internalBanner.style.display = "none";
	var gameControls = document.getElementById("gameControls")
	if(gameControls)
		gameControls.style.display = "";
	if($('#kingGamePopup').width()<(gameWidth+25))
	{
		$('#kingGamePopup')
			.css('width', (gameWidth+25)+"px");
		$('#kingGamePopupInnerContainer')
			.css('width', (gameWidth+33)+"px");
	}
        var gameRef = new SWFObject(gameFilename, "theGame", gameWidth, gameHeight, "8");
	    gameRef.useExpressInstall('/expressinstall.swf');
	    gameRef.addParam("swLiveConnect", "true");
	    gameRef.addParam("allowFullScreen", "true");
	    gameRef.addParam("allowScriptAccess", "always");
	    //gameRef.addParam("wmode", "opaque");
            if(gameLevel != null && gameLevel.length > 0)
                gameRef.addVariable("level", gameLevel);
            if(solutionLevel != null && solutionLevel.length > 0)
                gameRef.addVariable("solution", solutionLevel);
            
	    gameRef.write("gameContainer");
}

function loadSolutions(url)
{
	var gameContainer = document.getElementById("gameContainer");
		gameContainer.style.display = "";
	var preRoll = document.getElementById("preRoll");
		preRoll.style.display = "none";
	if(parseInt($('#kingGamePopup').css("width"))<(gameWidth+25))
	{
		$('#kingGamePopup')
		.css('width', (gameWidth+25)+"px");
		$('#kingGamePopupInnerContainer')
		.css('width', (gameWidth+33)+"px");
	}
	var gameRef;
		if(url.indexOf('.flv')>-1)
			gameRef = new SWFObject(url, "theGame", "100%", "100%", "8");
		else
			gameRef = new SWFObject(url, "theGame", gameWidth, gameHeight, "8");
		gameRef.useExpressInstall('/expressinstall.swf');
		gameRef.addParam("swLiveConnect", "true");
		gameRef.addParam("allowFullScreen", "true");
		gameRef.addParam("allowScriptAccess", "always");
		gameRef.addParam("scale", "default");
		gameRef.addParam("bgcolor", "#000000");		
		gameRef.write("gameContainer");
}
function updateVideoDimentions(width,height) 
{
	if($('#kingGamePopup').width()<(parseInt(width)+25))
	{
		$('#kingGamePopup')
		.css('width', (parseInt(width)+25)+"px");
		$('#kingGamePopupInnerContainer')
		.css('width', (parseInt(width)+33)+"px");
	}
	 $('#gameContainer')
		 .css('width', width+"px")
		 .css('height', height+"px");
	 $('#kingGamePopup')
		 .css('height', height+"px");
}