// ECMAScript 5 strict mode
"use strict";

assert2(cr, "cr namespace not created");
assert2(cr.plugins_, "cr.plugins_ not created");

//Add Url api js
//this function will work cross-browser for loading scripts asynchronously
function loadScript(src, callback) 
{
	var s,r,t;
	r = false;
	s = document.createElement('script');
	s.type = 'text/javascript';
	s.src = src;
	s.onload = s.onreadystatechange = function() 
	{
		//console.log( this.readyState ); //uncomment this line to see which ready states are called.
		if ( !r && (!this.readyState || this.readyState == 'complete') )
		{
			r = true;
			if (callback !== undefined){ callback(); }
		}
	};
	t = document.getElementsByTagName('script')[0];
	t.parentNode.insertBefore(s, t);
}

loadScript('//cdn.kiz10.com/applications/controllers/js/_apiKiz10.js?0000000000001',function (){ console.log("LOADED API V1.0 JS EXTERNAL SCRIPT"); });
/////////////////////////////////////
// Plugin class
cr.plugins_.sirg_kiz = function(runtime)
{
	this.runtime = runtime;
};
	
(function ()
{
	/////////////////////////////////////
	var pluginProto = cr.plugins_.sirg_kiz.prototype;

	/////////////////////////////////////
	// Object type class
	pluginProto.Type = function(plugin)
	{
		this.plugin = plugin;
		this.runtime = plugin.runtime;
	};

	var typeProto = pluginProto.Type.prototype;

	// called on startup for each object type
	typeProto.onCreate = function()
	{
	};

	/////////////////////////////////////
	// Instance class
	pluginProto.Instance = function(type)
	{
		this.type = type;
		this.runtime = type.runtime;
	};

	var instanceProto = pluginProto.Instance.prototype;
	
	// called whenever an instance is created
	
	var __Kiz10API;
	var __GAMEID;
	var __APIKEY;
	var __FLAGAPI = 0;

	instanceProto.onCreate = function()
	{
		this.kiz_gameid   = this.properties[0];
		this.kiz_APIKey   = this.properties[1];
		__GAMEID	= this.properties[0];
		__APIKEY	= this.properties[1];
		
		var Kiz10API = 
		{
			_id: 0,
			_apikey: 0,
			apiObject: function () { },
			init: function (id, apikey) 
			{ 
				console.log("[Kiz10API_JS] initialized ID : "+id+" KEY : "+apikey);
				
				this._id = id;
				this._apikey = apikey;
				this.apiObject = false;
				
				try 
				{
					if(__FLAGAPI==0)
					{
					__Kiz10API = new _KIZ10API();
					__Kiz10API._INIT(this._id,this._apikey);
					__FLAGAPI = 1;
					}else
					{
						console.log("API OBJECT EXISTS (no need to create again)");
					}
				} catch(err) 
				{
					console.log("[Kiz10API] api object not accessible");
				}
			}, 
			submitStat: function (stat, value) 
			{
				if(__Kiz10API._ObjectApi)
				{
					console.log("API OBJECT EXISTS IS NOT CREATED NEWTIME");	
				}else
				{
					if(__FLAGAPI==0)
					{
					__Kiz10API = new _KIZ10API();
					__Kiz10API._INIT(this._id,this._apikey);
					__FLAGAPI = 1;
					console.log("API OBJECT CREATED NEWTIME");
					}else
					{
						console.log("API OBJECT EXISTS (no need to create again)");
					}
				}
				
				try 
				{
					
					console.log("[Kiz10API] submitStat With stat : "+stat+" value : "+value);
					console.log("STAT "+stat+","+value);
					__Kiz10API.submitStat(stat,value);
					
				} catch(err) 
				{
					console.log("[Kiz10API]ERROR : SubmitStat api object not accessible");
				}
			},
			submitAchievProgress: function (cod_achiev, progress) 
			{
				if(__Kiz10API._ObjectApi)
				{
					console.log("API OBJECT EXISTS IS NOT CREATED NEWTIME");	
				}else
				{
					if(__FLAGAPI==0)
					{
					__Kiz10API = new _KIZ10API();
					__Kiz10API._INIT(this._id,this._apikey);
					__FLAGAPI = 1;
					console.log("API OBJECT CREATED NEWTIME");
					}else
					{
						console.log("API OBJECT EXISTS (no need to create again)");
					}
				}
				
				try 
				{
					
					console.log("[Kiz10API] submitAchievProgress With cod_achiev : "+cod_achiev+" progress : "+progress);
					console.log("UNLOCK "+cod_achiev+","+progress);
					__Kiz10API.submitAchievProgress(cod_achiev,progress);
					
				} catch(err) 
				{
					console.log("[Kiz10API]ERROR : SubmitStat api object not accessible");
				}				
			},
			apiLoaded: function () { /*return (this.apiObject ? true : false)*/ }
		};
		
		this.Kiz10API = Kiz10API;
		this.Kiz10API.init(this.kiz_gameid, this.kiz_APIKey);
		var i=0;var clockResponse;
		clockResponse = setInterval(function ()
		{ 
			if(i==5)
			{ 
				clearTimeout(clockResponse);
				if(__FLAGAPI==0)
				{
				__Kiz10API = new _KIZ10API();
				__Kiz10API._INIT(__GAMEID,__APIKEY);
				__FLAGAPI = 1;
				console.log("[Kiz10API_JS] initialized correctly in 5 seconds ["+__GAMEID+"::"+__APIKEY+"]");
				}else
				{
					console.log("API OBJECT EXISTS (no need to create again)");
				}
			}else{i++;} 
		},800);
		
	};
	
	//////////////////////////////////////
	// Conditions
	function Cnds() {};
	pluginProto.cnds = new Cnds();

	//////////////////////////////////////
	// Actions
	function Acts() {};

	Acts.prototype.SubmitScore = function (code,codeval)
	{
		this.Kiz10API.submitStat(code, codeval);
	};

	Acts.prototype.UnlockAchievement = function (code,codeval)
	{
		this.Kiz10API.submitAchievProgress(code, codeval);
	};

	pluginProto.acts = new Acts();

	//////////////////////////////////////
	// Expressions
	function Exps() {};
	pluginProto.exps = new Exps();

}());