var myJavascriptObject;
function GameGateway(basicUrl) {
	this.baseEndpointUrl = basicUrl;
	this.code=0;
	this.id=0;
	this.score=0;
}

GameGatewayAutoload.prototype = new GameGateway();        // We inherit methods
GameGatewayAutoload.prototype.constructor=GameGatewayAutoload;       // We averride the contructor properties
GameGatewayAutoload.prototype.parent = GameGateway.prototype; //we defin a parent property to be used as super

/*
 * Game gateway for external applications
 * basicUrl    Basic url of the service es :http://www.wazzamba.com/wazzamba-web
 * targetFbId  Facebook id of the user that will be gifted with wac miles
 * gameId      The game id of the game to be loaded es (10003 is Jenny in the clouds) for a complete reference of game id refer to www.wazzamba.com
 * currentFbPlayer The facebook id of the player that is currently playing  
 * exitGameUrl The url to redirect on the game end
 * 
 */
function GameGatewayAutoload(basicUrl,targetFbId,gameId,currentFbPlayer,exitGameUrl) {
	this.baseEndpointUrl = basicUrl;
	this.exitGameUrl = exitGameUrl;
	this.code=0;
	this.id=gameId;
	this.score=0;
	this.targetFbId=targetFbId;	
	this.currentFbPlayer=currentFbPlayer;
	this.claim=false;
	jQuery.get(this.baseEndpointUrl + '/apispring/casualGameFbServiceLoad',{game:this.id,targetFbId:this.targetFbId},function(data){
		if(jQuery(data).find("status").text()==0){
		myJavascriptObject.isFbWazzambaUser=true;
		}else{
			myJavascriptObject.isFbWazzambaUser=false;
		}
		
	   });
}


GameGateway.prototype.flipRightPanel = function(data) {

	jQuery('.game-right-panel-wrapper').html(data);
};

GameGateway.prototype.init = function(data) {

	jQuery('.game-right-panel-wrapper').html(data);
};
GameGateway.prototype.injectWacValue = function(data) {
	jQuery('#wacMilesLable').html(data);
	jQuery('#headerMiles').html(data);
	//jQuery("#wacMilesLable").formatNumber({format:"#,###", locale:"us"});
	//jQuery("#headerMiles").formatNumber({format:"#,###", locale:"us"});
};
GameGateway.prototype.startGame = function(data) {
	jQuery.post(this.baseEndpointUrl + '/spring/casualGameBoxControllerStart',{input:data},function(data){
		myJavascriptObject.flipRightPanel(data);
	   });
};

GameGateway.prototype.endGame = function(data) {

	jQuery.post(this.baseEndpointUrl + '/spring/casualGameBoxControllerEnd',{input:data},function(data){
		myJavascriptObject.flipRightPanel(data);
	   });
	
};

GameGatewayAutoload.prototype.endGame = function(data) {

	jQuery.post(this.baseEndpointUrl + '/apispring/casualGameFbServiceEnd',{input:data},function(data){
		
		if(jQuery(data).find("status").text()==0){
			
			myJavascriptObject.wac=jQuery(data).find("wac").text();
			myJavascriptObject.claim=jQuery(data).find("claim").text();
			var redirectUrl=myJavascriptObject.exitGameUrl+"?player="+myJavascriptObject.currentFbPlayer;
			redirectUrl+="&miles="+myJavascriptObject.wac;
			redirectUrl+="&referrer="+myJavascriptObject.targetFbId;
			redirectUrl+="&score="+myJavascriptObject.score;
			redirectUrl+="&claim="+myJavascriptObject.claim;
			redirectUrl+="&isFbWazzambaUser="+myJavascriptObject.isFbWazzambaUser;
			document.location.href=redirectUrl;
			
			}else{
				myJavascriptObject.isFbUser=false;
			}
	   });
};

GameGateway.prototype.respond = function(data) {

	
	           myJavascriptObject.setGameValues(data);
			   var requestedMethod=myJavascriptObject.code;
			   switch(requestedMethod) {
			   
			   		case 'gamestart':
			            this.startGame(data);
			            logPortalEvent( "MINI_GAME_START", myJavascriptObject.id );
			   			break; 

			   		case 'gameover':
			   			this.endGame(data);
			   			logPortalEvent( "MINI_GAME_END", myJavascriptObject.id, "score", myJavascriptObject.score);			   			
			   			break; 
			   			   
			   		default:
			     // donothing
			 }
			
		
};

GameGateway.prototype.setGameValues = function(data) {

	
	    var parameters=data.split('_');	
		for (i=0;i<parameters.length;i++)
		{
		   
			   
			   var keyAndValue=parameters[i].split('=');
			   var key=keyAndValue[0];
			   switch(key) {
			   
			   		case 'code':
			   			myJavascriptObject.code=keyAndValue[1];
			   			break; 

			   		case 'gameid':
			   			myJavascriptObject.id=keyAndValue[1];
			   			break;
			   			
			   		case 'score':
			   			myJavascriptObject.score=keyAndValue[1];
			   			break; 
			   			   
			   		default:
			     // donothing
			 }
			 
		   
		}
};


/* This method evaluates a server directive 
 * The Directive "showView" force the href property fo the current page to the specified view
 * */
GameGateway.prototype.evaluateCommand = function(xml) {

	jQuery(xml).find("showView").each(function()			{
			  document.location.href=jQuery(this).text();
			});
	jQuery(xml).find("wacOwned").each(function()			{
		myJavascriptObject.injectWacValue(jQuery(this).text());
	});
	//Todo define a widget to include wac miles notifications
};
GameGateway.prototype.onWacUpdateSuccess = function(xml) {
	
	jQuery(xml).find("wacOwned").each(function()			{
		myJavascriptObject.injectWacValue(jQuery(this).text());
	});
	//Todo define a widget to include wac miles notifications
};

GameGateway.prototype.checkWac = function(data) {

	
	jQuery.post(this.baseEndpointUrl + '/apispring/genericGameCheckWac',function(data){
		myJavascriptObject.evaluateCommand(data);
	   });
};
GameGateway.prototype.updateWac = function(data) {
	jQuery.post(this.baseEndpointUrl + '/apispring/genericGameCheckWac',function(data){
		myJavascriptObject.onWacUpdateSuccess(data);
	});
};



