//\\//=== configuration constants ===\\//\\

var EMPTY_RESULT = "EMPTY_RESULT";
window.OK = "OK";

//====== Namespace UA
if(!window.UA) UA = {};
//=== Class Game
UA.Game = Class.create();
/*    static members	*/
//-------------------------------------------------------------
/**
 * @private
 * holds references to all requests sent by this instance
 **/
UA.Game.Requests = [];

UA.Game.Methods =	{   GAMEHIGHSCORES:	    "GetGameHighScores",    //1400::"GameHighScores"
					    USERSCORE:			"GetUserScoreData",     //1400::"UserScore"
                        SESSION_CERT:       "GetSingleSessionCert",
                        GRACE_CERTS:        "GetGraceCerts"
                    }

UA.Game.Scores = {};
UA.Game.Scores.Periods = {};
UA.Game.Scores.Periods.WEEK = "WEEK";
UA.Game.Scores.Periods.HOUR = "HOUR";
UA.Game.Scores.Periods.EVER = "EVER";


/*    members			*/
//-------------------------------------------------------------
UA.Game.prototype.SEND_GAME_DATA_POST_URL = "UNSET_VALUE"; 


/*	  Constructors		*/
//-------------------------------------------------------------
UA.Game.prototype.initialize = function(p_sku){
	//Claim.isNumber(p_sku ,"UA.Game(Sku): Sku should be a number.");
	//Claim.check(p_sku > 0 && parseInt(p_sku) == p_sku, "UA.Game(Sku)", "Game Sku must be an integer, bigger then 0");

	this._prv = {	Sku: p_sku
				,	scores: {}	
				};
	this.log = new Log4Js.Logger(this.toString())
	this.log.info("UA.Game(" + p_sku + ") initiated.");
}


/*    inner class		*/

/* -- start inner class : UA.Game.GameRequest   --*/

/**
 * @class 
 * The goal of this class is to allow multiple calls on the same Game object-instance,
 * and aid in making the Asyncronous work as transparent as possible. 
 * 
 * This inner class extends the "abstract"-class UI.Request
 *
 * The GameRequest prepare the parameters stack based on the Game's Sku.
 * The Jast.Request expects handlers. Assuming that in most cases there will be nothing
 * important to say on case of time-out or of failior, the class provides almost empty 
 * default event-handler stubs. 
 * To make the calls look as if they were called on the UA.Game object, 
 * these stubs pass on the call to the Game Object - if in deed the caller-instance 
 * defined handlers for those cases. 
 **/
UA.Game.GameRequest = Class.createSubclass("UA.Request");
UA.Game.GameRequest.prototype = UA.Request.AsPrototype()
//UA.Game.GameRequest.prototype = new UA.Request();
/*	  --- Constructors		*/
/**
 *  @param {oGame} UA.Game
 *      The game-instance that uses the request 
 *
 *  @param {function(result)} fSuccessCallback
 *      Callback function for Success
 * 
 *  @param-optional {function(request)} fFailCallback
 *      Callback function for Fail
 *      If the parameter is not supplied, the oGame.defaultOnFailior is used
 *
 *  @param-optional {function(request)} fTimeoutCallback
 *      Callback function for Timeout
 *      If the parameter is not supplied, the oGame.defaultOnTimeout is used

 **/
UA.Game.GameRequest.prototype.initialize = function gameCtor( oGame	, fSuccessCallback , fFailCallback, fTimeoutCallback){
	this.url = this.boss.PRIME_HANDLER_URL;
	this.parameters = {	Sku: oGame._prv.Sku,
						Period: "",
						Mode: "" };
}

/* -- end inner class : UA.Game.GameRequest   --*/

/*	private functions	*/
//-------------------------------------------------------------


/**
 * @private
 * Sets up a GameRequest
 *
 * @param {function(result)} fSuccessCallback
 *   Callback function for Success
  * 
 * @param-optional {function(request)} fFailCallback
 *   Callback function for Fail
 *   If the parameter is not supplied, the UA.Game.defaultOnFailior is used
 *
 * @param-optional {function(request)} fTimeoutCallback
 *   Callback function for Timeout
 *   If the parameter is not supplied, the UA.Game.defaultOnTimeout is used
 *  
 */
UA.Game.prototype.getGameRequest = function(fSuccessCallback, fFailCallback, fTimeoutCallback){
	var r = new UA.Game.GameRequest(this,fSuccessCallback, fFailCallback, fTimeoutCallback);

	r.id = UA.Game.Requests.length;
	UA.Game.Requests[ r.id ] = r;

	return r;
}

/*	public methods	*/
//-------------------------------------------------------------
/**
 * Default failure handler for all requests
 * Possible to be overriden in projectile code
 *
 * @type function
 **/
UA.Game.prototype.defaultOnFailior = null;
/**
 * Default timeout handler for all requests
 * Possible to be overriden in projectile code
 *
 * @type function
 **/
UA.Game.prototype.defaultOnTimeout = null;

/**
 * Overrides Object.toString() 
 **/
UA.Game.prototype.toString = function gameToString(){
	return "[UI.Game(" + this._prv.Sku + ")]";
}

/**
 * Gets the user high-score in the appointed game-mode.
 * Returns the score to the callback function on the UC.Game instance, with the score.
 *
 * @param {int} iMode
 *  The game mode  (examples of what mode means: arcade, journy, survival, sudden-death)
 *
 * @param {function(result)} fSuccess
 *	The callback function that is expected to be called on success with 'request.result.Data' as a 
 *  as passed argument.
 * 
 * @param {function(result)} fFailure
 *	The callback function that is expected to be called on failure with 'request.result' as the 
 *  passed argument.
 * 
 * @param {function(result)} fTimeout
 *	The callback function that is expected to be called on timeout with 'request' as the 
 *  passed argument.
 * 
 **/
UA.Game.prototype.GetUserHighScore = function(iMode, fSuccess, fFailure, fTimeout){
    //default values for optional parameters
    if(iMode == null || isNaN(iMode)) iMode = 0;

	
	//apply a request
	var r = this.getGameRequest(fSuccess, fFailure, fTimeout);
	r.parameters.Mode = iMode;
	r.parameters.MethodName = UA.Game.Methods.USERSCORE;
	r.apply();
	return r;
}

/**
 * Gets the single session certification key.
 * Returns the key to the callback function on the UA.Game instance. 
 * The method will be Invoked with the GETDATA() mechanism (see UA.User.prototype.GetData)
 **/
UA.Game.prototype.GetSingleSessionCert = function(iMode, fSuccess, fFailure, fTimeout){
	//apply a request
	var r = this.getGameRequest(fSuccess, fFailure, fTimeout);
	r.parameters.methodName = UA.User.Methods.GETDATA;
	r.parameters.MethodList = UA.Game.Methods.SESSION_CERT;
	r.apply();
	return r;
}

/**
 * Gets the single session certification key.
 * Returns the key to the callback function on the UA.Game instance. 
 * The method will be Invoked with the GETDATA() mechanism (see UA.User.prototype.GetData)
 **/
UA.Game.prototype.GetGraceCerts = function(iMode, fSuccess, fFailure, fTimeout){
	//apply a request
	var r = this.getGameRequest(fSuccess, fFailure, fTimeout);
	r.parameters.MethodName = UA.User.Methods.GETDATA;
	r.parameters.MethodList = UA.Game.Methods.GRACE_CERTS;
	r.apply();
	return r;
}

/**
 * @param {int} iMode
 *  The game mode  (examples of what mode means: arcade, journy, survival, sudden-death)
 *
 * @param {int} iAmount
 *  The top results count
 *
 * @param {} ePeriod
 *	The argument ePeriod can be one from the next values:
						UA.Game.Scores.Periods.WEEK
						UA.Game.Scores.Periods.HOUR
						UA.Game.Scores.Periods.EVER
 *
 * @param {function(result)} fSuccess
 *	The callback function that is expected to be called on success with 'request.result.Data' as a 
 *  as passed argument.
 * 
 * @param {function(result)} fFailure
 *	The callback function that is expected to be called on failure with 'request.result' as the 
 *  passed argument.
 * 
 * @param {function(result)} fTimeout
 *	The callback function that is expected to be called on timeout with 'request' as the 
 *  passed argument.
 * 
 **/

UA.Game.prototype.GetGameHighScores = function(iMode, iAmount, ePeriod, fSuccess, fFail, fTimeout){
    var r = this.getGameRequest(fSuccess, fFail, fTimeout);
    r.parameters.Mode = iMode;    
    r.parameters.TopScores = iAmount;
    r.parameters.Period = ePeriod;
	r.parameters.MethodName = UA.Game.Methods.GAMEHIGHSCORES;
    r.apply();
}

// Send game data with POST
UA.Game.prototype.SendGameData = function(gameData, callback){
	var formSendGameData = document.createElement("form");
	formSendGameData.setAttribute("method", "POST");
	formSendGameData.setAttribute("action", UA.Game.prototype.SEND_GAME_DATA_POST_URL);
	formSendGameData.appendChild(CreateHiddenInput("GameData", gameData));
	PostIframeRequest(formSendGameData, callback);
}