/*************************************************************************
* classGameList.js
* Extracts and paints game lists for mode default, latest, all games and top 5 games
* Usage:
*
* Dependency: 			Mootools 1.21 Core (required)
*
* Author: 				Damodar Panigrahi
*
*
* v.1 (13-Apr-09) 	- 	Initial release
*
**************************************************************************/
var GameList = new Class({
	Implements: [Options, Events],

	options: {
		arrowURL: '',
		divId: '',
		promoHeadline: '',
		disclaimer:'',
		promoType:'',
		featuredLicenseText:'',
		reqUrl: '',
		trialText: '',
		buyText: '',
		comingSoonText: '',
		secureHost: '',
		nonSecureHost: '',
		errorMessage: '<center>No games are currently accessible. Please try again later.</center>',
		contextRoot: 'ngi/',
		viewType: 'all'
	},

	initialize: function(options) {
		//set the options
		this.setOptions(options);

		timeOutVar = '';
		if(this.options.viewType == 'all') {
			timeOutVar = setTimeout(this.writeError.bind(this),60000);
		}
		new Request.JSON({
			method: 'get',
			url: this.options.reqUrl,
			onSuccess: function(data){
				this.debug('onSuccess finished.');
				window.clearTimeout(timeOutVar);
				this.drawGameList(data);
				$$('div#' + this.options.divId)[0].set({html: this.gameListHtml});
			}.bind(this),
			onFailure: function(data){
				window.clearTimeout(timeOutVar);
				this.writeError();
			}.bind(this),
			onComplete: function(data){
				//alert ('onComplete:' + data);
			}.bind(this)
		}).send();
	},
	writeError: function(){
		//alert(this.options.errorMessage);
		$$('div#' + this.options.divId)[0].set({html:this.options.errorMessage});
	},
	drawGameList: function(gameData){
		this.debug('drawGameList started.');
		var gameListHtml = '';
		var baseImageUrl = this.options.contextRoot + "/etc/medialib/ngage/store/images/en/";
		var ngageCookie = new NGageCookie();
	    var countryCode = location.href.substr(location.href.indexOf('web') + 4, 2); //g0
	    var languageCode = location.href.substr(location.href.indexOf('web') + 7, 2); //en
//		var countryCode = ngageCookie.getCookie('ngage_countryid');
//		var languageCode = ngageCookie.getCookie('ngage_languageid');
		var urlForGameDetail = this.options.contextRoot + "/ngage/web/" + countryCode + "/" + languageCode + "/showroom/gamedetail.";
		var urlForGameDetailPrePend = '.GameDetail.html';
		var urlForRanking = this.options.contextRoot + "/ngage/web/" + countryCode + "/" + languageCode + "/showroom.";
		var urlForRankingPrePend = '-5-2.StarRanking.png';
		var urlForStore = this.options.secureHost + this.options.contextRoot + '/resources/ngage.play.html?d=';

		if (gameData) {
			this.debug('drawGameList games exists.');
			var gameCounter = 1;
			var gameDetailUrl;
			if (this.options.viewType == 'top5') {
				gameListHtml += '<div id="popular_game_title"><span class="subHeader">' + this.options.promoHeadline + '</span></div>';
				(gameData.games.game).each(function(g){
					var gameDetailUrl = urlForGameDetail + g.pkid + urlForGameDetailPrePend;
					gameListHtml += '<div class="popular_game_cell"><a href="';
					gameListHtml += gameDetailUrl;
					gameListHtml += '" class="toutListSidebar"><img onload="fixPNG(this)" src="';
					gameListHtml += this.options.contextRoot + g.img;
					gameListHtml += '" class="popular"></a><span><a href="';
					gameListHtml += gameDetailUrl;
					gameListHtml += '" class="toutListSidebar">';
					gameListHtml += g.title;
					gameListHtml += '<img src="';
					gameListHtml += this.options.arrowURL;
					gameListHtml += '"></a></span></div>';
				}.bind(this));
				gameListHtml += '<div style="clear: both;"></div>';
			} else {
				(gameData.games.game).each(function(g){
					//console.log('drawGameList games:game exists. counter:' + gameCounter);
					var gameDetailUrl = urlForGameDetail + g.pkid + urlForGameDetailPrePend;
					if (gameCounter == 1 && this.options.viewType == 'default') {
						gameListHtml += '<div id="game_intro_box"><div class="game_intro_box_inner"><span class="Par1ngiGameToutListPromoHeaderFont">';
						gameListHtml += this.options.promoHeadline;
						gameListHtml += '</span>';
						if (countryCode == 'g0') {
							gameListHtml += '<br></br><span class="Par1ngiGameToutListGameToutListDisclaimerFont">' + this.options.disclaimer + '</span>';
						}
						gameListHtml += '</div><div style="background: transparent url(';
						gameListHtml += baseImageUrl + g.img;
						gameListHtml += ') no-repeat scroll left top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="game_showroom_feature"><a href="';
						gameListHtml += gameDetailUrl;
						gameListHtml += '"></a></div><div class="game_intro_box_inner"><div><div><a class="Par1ngiGameToutListFeaturedGameTitleFont" href="';
						gameListHtml += gameDetailUrl;
						gameListHtml += '">' + g.title + '</a><br></br><div style="background: transparent url(';
						gameListHtml += urlForRanking + g.rating + urlForRankingPrePend;
						gameListHtml += ') no-repeat scroll left top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" id="featureStarRating"></div></div></div><span class="Par1ngiGameToutListPromoTypeFont">';
						gameListHtml += this.options.promoType;
						gameListHtml += '</span><br></br>';
						if (countryCode != 'g0' && g.isBuy == 'true') {
							gameListHtml += this.options.featuredLicenseText + ':&nbsp;<a href="';
							gameListHtml += urlForStore + g.buy;
							gameListHtml += '" class="toutList">';
							gameListHtml += g.price;
							gameListHtml += '&nbsp;';
							gameListHtml += g.currency;
							gameListHtml += '</a>';
						}
						gameListHtml += g.desc;
						gameListHtml += '</div></div>';
					} else {
						gameListHtml += '<div class="game_tout_box_wrapper box_target ';
						if (g.isBuy == 'true') {
							gameListHtml += 'releasedGame';
						} else {
							gameListHtml += 'unreleasedGame';
						}
						gameListHtml += '" id="GO' + gameCounter + '">';
						gameListHtml += '<div class="game_tout_box_inner"><div class="game_title"><h5>' + g.title + '</h5></div><div style="background: transparent url(';
						gameListHtml += baseImageUrl + g.img;
						gameListHtml += ') no-repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" class="game_pic"></div><div class="game_price"><ul><li class="';
						if (g.isBuy == 'true') {
							gameListHtml += 'gameTrialLink"><a href="javascript:window.location=\'';
							gameListHtml += urlForStore + g.trial;
							gameListHtml += '\'" class="rButton"><span class="rButtonL"><span class="rButtonR">' + this.options.trialText + '</span></span></a></li>';
							if (countryCode != 'g0'){
								gameListHtml += '<li class="gamePriceLink"><a href="javascript:window.location=\'';
								gameListHtml += urlForStore + g.buy;
								gameListHtml += '\'" class="rButton"><span class="rButtonL"><span class="rButtonR">' + this.options.buyText + '</span></span></a></li>';
							}						} else {
							gameListHtml += 'gameComingSoon">' + this.options.comingSoonText + '</li>';
						}
						gameListHtml += '</ul></div></div><div class="gameHover_wrapper inactive" id="gameHoverG' + gameCounter + '"><div class="gameHover_inner_T"></div><div class="gameHover_inner"></div><div class="gameHover_inner_B"></div><div class="filterIEFix"><a href="';
						gameListHtml += gameDetailUrl + '">' + g.title + '</a><a style="background: transparent url(';
						gameListHtml += urlForRanking + g.rating + urlForRankingPrePend;
						gameListHtml += ') no-repeat scroll left top; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;" class="gh_stars popupStarRating" href="';
						gameListHtml += gameDetailUrl;
						gameListHtml += '"></a><a class="gh_genre Par1ngiGameToutListPopupGenreFont" href="';
						gameListHtml += gameDetailUrl;
						gameListHtml += '">Strategy</a><p class="gh_desc">';
						gameListHtml += g.desc;
						gameListHtml += '</p></div></div></div>';
					}

					gameCounter++;
				}.bind(this));
			}
		} else {
			gameListHtml = this.options.errorMessage;
		}
		this.debug('drawGameList gameListHtml:' + gameListHtml);

		this.gameListHtml = gameListHtml;
		//$('hoverParentAjax').set({html: gameListHtml});

		this.debug('drawGameList finsihed.');
	},

	debug: function(message) {
		//Please note that console is a part of firebug so comment out before c/i
//		if (console) {
//			console.log(message);
//		}
	}
});