/**
* This function is executed when the window.onload event fires.
* It initializes the application, creates a MyGames list and
* calls displayInfo, which creates the page content.  If the 
* RealArcade Games Service software is not installed on the 
* machine, an error message is displayed.
*/
function initGameList() {
	try {
		appCore_startUp();
		window.onunload = appCore_shutDown;
		myGamesList = myGamesListFactory.createMyGamesList();
		displayInfo();
	}
	catch(e) {
		document.getElementById("myGameMessage").innerHTML = "<h2>Welcome to My Games! Use this page to easily access<br/>all the games you have downloaded.<br/>Haven't downloaded any of our free games yet? Click<br/>the <a href='/free-online-games-sweepstakes-and-contests/games/'>All Games link</a> to get started.</h2>";
		
		document.getElementById("sortByTitle").innerHTML='';
		document.getElementById("sortByRecent").innerHTML='';
		document.getElementById("sortByAlpha").innerHTML='';
		document.getElementById("sortByFull").innerHTML='';
		
		var downloadGamesLink=document.getElementById("downloadGamesLink");
		var firefoxLinkLI = document.createElement( "li" );
		var firefoxLink = document.createElement( "a" );
		firefoxLink.href="http://i.real.com/games/images/firefox/realarcade-ff.xpi";
		firefoxLink.innerHTML="Download the RealArcade plug-in for Firefox";

		if( navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1 ) {

			firefoxLinkLI.appendChild(firefoxLink);
		}

		document.getElementById("downloadLinkLast").parentNode.insertBefore(firefoxLinkLI,document.getElementById("downloadLinkLast"));	
	}
	
}

function initGame(gameId,gameName) {

	var downloadDiv=document.getElementById("download");
	var buyDiv=document.getElementById("buy");
	var deluxeButtonsDiv=document.getElementById("deluxButtons");
	var deluxeButtonsP = document.createElement( "p" );
	deluxeButtonsP.className="deluxeButtonsP";

	try {
		appCore_startUp();
		window.onunload = appCore_shutDown;
		
		var game=gameObjectFactory.createGameObject(gameId);   
		if (game.purchaseStatus || game.unlimitedPlay || game.minutesRemaining>0) {
		   
			var playLinkHtml="<a href=\"javascript:playGame('"+game.gameid+"')\"";
			if (downloadDiv!=null) { 
				playLinkHtml=playLinkHtml+" onClick=\"s.tl(this,'e', '"+gameName+" - Play - Prod Page')\">";
			}
			else if (deluxeButtonsDiv!=null) {
				playLinkHtml=playLinkHtml+" onClick=\"s.tl(this,'e', '"+gameName+" - Play - Play Page')\">";
			}
			else {
				playLinkHtml=playLinkHtml+">";
			}
			
			playLinkHtml=playLinkHtml+"<img src=\"/images/games/btn-playNow-on.gif\" border=\"0\" align=\"absmiddle\"/></a>";
			
			
			if (downloadDiv!=null) {
				downloadDiv.innerHTML=playLinkHtml;
			}
			else if (deluxeButtonsDiv!=null) {
				deluxeButtonsP.innerHTML=playLinkHtml;	
		  	}
		   }
		   else {
		   	
			var playButtonImage = document.createElement( "img" );
			playButtonImage.src="/images/games/btn-playNow-off.gif";
			playButtonImage.border="0";
			playButtonImage.align="absmiddle";
			
			if (downloadDiv!=null) {
				downloadDiv.innerHTML="";
				downloadDiv.appendChild(playButtonImage);
			}
			
			if (deluxeButtonsDiv!=null) {
				deluxeButtonsP.appendChild(playButtonImage);	
		  	}
			
		   }
		   
		   if (!game.purchaseStatus) {
		   	if (buyDiv!=null) {
				buyDiv.style.display="block";
			}
			if (deluxeButtonsDiv!=null) {
				var buyLinkHtml="<a href=\"javascript:popWin(getBuyUrl('"+gameId+"'),760,535,'auto','auto')\" onClick=\"s.tl(this,'e', '"+gameName+" - Buy - Play Page');\"><img border=\"0\" alt=\"Buy Now\" src=\"/images/btn-buyNow-off.gif\"/></a>";
				deluxeButtonsP.innerHTML=deluxeButtonsP.innerHTML+buyLinkHtml;
			}	   	
		   }
	}
	catch(e) {
		if (buyDiv!=null) {
			buyDiv.style.display="block";
		}
		if (deluxeButtonsDiv!=null) {
			
			deluxeButtonsP.innerHTML="<a href=\"javascript:window.location=getDownloadUrl('"+gameId+"')\" onClick=\"s.tl(this,'e', '"+gameName+" - Download - Play Page');\"><img border=\"0\" alt=\"Download a Free Trial\" src=\"/images/btn-downloadFreeTrieal-off.gif\" /></a><a href=\"javascript:popWin(getBuyUrl('"+gameId+"'),760,535,'auto','auto')\" onClic=\"s.tl(this,'e', '"+gameName+" - Buy - Play Page');\"><img border=\"0\" alt=\"Buy Now\" src=\"/images/btn-buyNow-off.gif\"/></a>";
	
		}	
	}
	if (downloadDiv!=null) {
		downloadDiv.style.display="block";
	}
	if (deluxeButtonsDiv!=null) {
		deluxeButtonsDiv.appendChild(deluxeButtonsP);
	}
}

/**
* This function displays the contents of the game list
* on the page, and displays game information for the 
* first game in the list
*/
function displayInfo() {
	displayGameList( myGamesList,false );
}

/**
* This function displays the contents of the game list
*/
function displayGameList( list ,fullVersion) {
	// delete the old list
	while ( document.getElementById("stage").lastChild && document.getElementById("stage").lastChild.id!="myGameMessage") {
		document.getElementById("stage").removeChild( document.getElementById("stage").lastChild );
	}
	
	if (fullVersion) {
		
		for( i=0; i<list.count; i++ ) {
			game = list.item(i);
			if (game.purchaseStatus) {
				document.getElementById("stage").appendChild( createGameInfo( game ) );
			}
		}
		for( i=0; i<list.count; i++ ) {
			game = list.item(i);
			if (!game.purchaseStatus) {
				document.getElementById("stage").appendChild( createGameInfo( game ) );
			}
		}
		
	}
	else {
		for( i=0; i<list.count; i++ ) {
			game = list.item(i);
			document.getElementById("stage").appendChild( createGameInfo( game ) );
		}
	}
}

function createGameInfo( game ) {

	var gameDiv = document.createElement( "div" );
	gameDiv.className='myGame';

	var gameImageDiv = document.createElement( "div" );
	if (game.purchaseStatus) {
		gameImageDiv.className='image fullVersion';
	}
	else {
		gameImageDiv.className='image demo';
	}

	var gameImage = document.createElement( "img" );
	gameImage.setAttribute( "src", "/rnfeed/games/" + game.gameid + "/" + game.gameid + "_146x110.jpg" );
	gameImage.setAttribute( "border", "0" );
	gameImage.setAttribute( "alt", game.gameName );
	gameImageDiv.appendChild( gameImage);

	gameDiv.appendChild(gameImageDiv );

	var widgetDiv = document.createElement( "div" );
	widgetDiv.className="widget";

	var gameNameP = document.createElement( "p" );
	var gameNameLink = document.createElement( "a" );
	gameNameLink.setAttribute( "href", "/free-online-games-sweepstakes-and-contests/games/" + game.gameid + ".html" );
	gameNameLink.appendChild( document.createTextNode( game.gameName ) );
	gameNameP.appendChild(gameNameLink );
	widgetDiv.appendChild(gameNameP );

	var playedP = document.createElement( "p" );
	if (!game.purchaseStatus) {
		playedP.appendChild( document.createTextNode( 'Played ' + game.playCount + ' times' ) );
		widgetDiv.appendChild(playedP );
	}
	gameDiv.appendChild(widgetDiv );
	
	if (!game.purchaseStatus && !game.unlimitedPlay) {
	
		var widgetUL = document.createElement( "ul" );
		widgetUL.className="widget";
		var timeRemainingLI = document.createElement( "li" );
		timeRemainingLI.className="timeRemaining";
		timeRemainingLI.innerHTML='Time Remaining: <string>'+game.minutesRemaining+' Min</strong>';
		widgetUL.appendChild(timeRemainingLI);

		var sliderLI = document.createElement( "li" );
		sliderLI.className="slider";

		var zeroDiv = document.createElement( "div" );
		zeroDiv.innerHTML="0";
		sliderLI.appendChild(zeroDiv);

		var progressPixels=game.minutesRemaining/game.demoMinutes*149-149;

		var progressDiv = document.createElement( "div" );
		progressDiv.className="progressBar";
		progressDiv.style.backgroundPosition=""+progressPixels+"px 50%";

		var barMaskImg = document.createElement( "img" );
		barMaskImg.setAttribute( "src", "/images/games/game-bar-mask.gif" );

		progressDiv.appendChild(barMaskImg);;
		sliderLI.appendChild(progressDiv);

		var fullDiv = document.createElement( "div" );
		fullDiv.innerHTML=""+game.demoMinutes;
		sliderLI.appendChild(fullDiv);
		widgetUL.appendChild(sliderLI);

		widgetDiv.appendChild(widgetUL);
	}

	gameDiv.appendChild(widgetDiv);

	var buttonDiv = document.createElement( "div" );
	buttonDiv.className="buttons";
	buttonDiv.innerHTML="";

	if (game.purchaseStatus) {
		var buyButtonImage = document.createElement( "img" );
		buyButtonImage.src="/images/spacer.gif";
		buyButtonImage.border="0";
		buyButtonImage.width="135";
		buyButtonImage.height="50";
		buyButtonImage.align="absmiddle";
		buttonDiv.appendChild(buyButtonImage);
	}
	else {
		var buyButtonHTML="<a href=\"javascript:popWin(getBuyUrl('"+game.gameid+"'),760,535,'auto','auto')\" onClick=\"s.tl(this,'e', '"+escape(game.gameName)+" - Buy - My Page')\"><img src=\"/images/games/game-buy-btn.gif\" border=\"0\" align=\"absmiddle\"/></a>";
		buttonDiv.innerHTML=buttonDiv.innerHTML+buyButtonHTML;
	}

	if (game.purchaseStatus || game.unlimitedPlay || game.minutesRemaining>0) {
		var playButtonHTML="<a href=\"javascript:playGame('"+game.gameid+"')\" onClick=\"s.tl(this,'e', '"+escape(game.gameName)+" - Play - My Page');\"><img src=\"/images/games/game-play-btn-on.gif\" border=\"0\" align=\"absmiddle\"/></a>";
		buttonDiv.innerHTML=buttonDiv.innerHTML+playButtonHTML;

	}
	else {
		var playButtonImage = document.createElement( "img" );
		playButtonImage.src="/images/games/game-play-btn-off.gif";
		playButtonImage.border="0";
		playButtonImage.align="absmiddle";			
		buttonDiv.appendChild(playButtonImage);
	}

	var uninstallHTML="<a href=\"javascript:uninstallGame('"+game.gameid+"')\" onClick=\"s.tl(this,'e', '"+escape(game.gameName)+" - Uninstall - My Page');\">Uninstall</a>";
	buttonDiv.innerHTML=buttonDiv.innerHTML+uninstallHTML;
	gameDiv.appendChild(buttonDiv);
	return gameDiv;
}

function playGame(gameId) {
	var gameToPlay=gameObjectFactory.createGameObject(gameId);
	gameToPlay.launch();
}

function uninstallGame(gameId) {
	var gameToPlay=gameObjectFactory.createGameObject(gameId);
	gameToPlay.uninstall();
}

/**
* This function sorts and displays a game list
*/
function sortAndDisplayGameList( list, sortBy, sortAscending ) {
	sortGameList( list, sortBy, sortAscending );
	
	var full=false;
	
	if (sortBy==SORT_BY_TIMEREM) {
		full=true;
	}
	
	displayGameList( list ,full);
}

