/**
 * Flash Stats API - v.1.04
 * Author ~ Niels Lyngsø
 */

// name should be exoforce:mainstage   (do not add ":" in the end)
function FlashStatsAPI(appReference) {

	//this._name = appReference;
	
	if (appReference != "") {
		this._reference = appReference;
		this._prop14LinkVars = "prop14,";
	} else {
		this._reference = "";
		this._prop14LinkVars = "";
	}
	
	this._accountName = s_account;
	
	//this._loadTimeCategories = this._generateTimeCategories();
	//this._speedCategories = this._generateSpeedCategories();
	
	this._initDate = new Date();
}


//FlashStatsAPI.prototype._name;
FlashStatsAPI.prototype._accountName;
FlashStatsAPI.prototype._initDate;

FlashStatsAPI.prototype._prop14LinkVars;
FlashStatsAPI.prototype._reference;

FlashStatsAPI.prototype._loadTimeCategories;
FlashStatsAPI.prototype._speedCategories;


FlashStatsAPI.prototype.isReady = function() {
	
	return "true";
	
}


// LOADS


FlashStatsAPI.prototype._generateTimeCategories = function() {
	var a = new Array();
	a[0.3] = "0-0.3";
	a[0.6] = "0.3-0.6";
	a[1] = "0.6-1";
	a[2] = "1-2";
	a[3] = "2-3";
	a[5] = "3-5";
	a[8] = "5-8";
	a[13] = "8-13";
	a[20] = "13-20";
	a[30] = "20-30";
	a[50] = "30-50";
	a[90] = "50-90";
	a[200] = "90-200";
	a["end"] = "200+";
	return a;
}

FlashStatsAPI.prototype._convertToLoadCategory = function(time) {
	
	this._loadTimeCategories = this._generateTimeCategories();
	
	for (var i in this._loadTimeCategories) {
		if (time < i) {
			return this._loadTimeCategories[i];
		}
	} 
	
	return this._loadTimeCategories["end"];
	
}

// definisions


// 1,1,2,3,5,8,13,21,34,55,89

// !cashed!??? (0-0.2)
// 0-1

// 0-0.3
// 0.3-0.6
// 0.6-1
// 1-2
// 2-3
// 3-5
// 5-8
// 8-13
// 13-20
// 20-30
// 30-50
// 50-90
// 90+



// time from load to complete load.
FlashStatsAPI.prototype.loaded = function() {
	
	var s = s_gi(this._accountName);
	
	s.linkTrackVars=this._prop14LinkVars+'prop15,prop16';
	
	s.prop14=this._reference;
	s.prop15=this._reference+':loaded';
	
	var currentDate = new Date();
	var msSinceInit = currentDate.getTime()-this._initDate.getTime();
	
	
	s.prop16=this._convertToLoadCategory(msSinceInit/1000);
	
	
	s.tl(this,'o','loaded');
}


// PAGES

FlashStatsAPI.prototype.page = function(p) {
	this._statsPage(p);
}

FlashStatsAPI.prototype._statsPage = function(pname) {
	
	var s = s_gi(this._accountName);
	
	s.pageName=this._reference+":"+pname;
	s.prop14=this._reference;
	
	s.t();
	
}





// ACTIONS

//							button
//							success



// GAME POINTS

FlashStatsAPI.prototype._generatePointCategories = function() {
	var a = new Array();
	a[10] = "0-10";
	a[100] = "10-100";
	a[1000] = "100-1000";
	a[10000] = "1000-10000";
	a["end"] = "10000+";
	return a;
}

FlashStatsAPI.prototype._convertToPointCategory = function(p) {
	
	var pointCategories = this._generatePointCategories();
	
	for (var i in pointCategories) {
		if (p < i) {
			return pointCategories[i];
		}
	} 
	
	return pointCategories["end"];
}


//						gameScore(2350)
FlashStatsAPI.prototype.gameScore = function(point) {
	
	this._statsAction("gameScore", this._convertToPointCategory(Number(point)));
}


//						gameLevel(12);
FlashStatsAPI.prototype.gameLevel = function(level) {
	
	this._statsAction("gameLevel", level);
}


//						buttonClick("saveButton", type);
FlashStatsAPI.prototype.buttonClick = function(name, t) {
	
	this._statsAction("button", name);
}


//						easterEgg("hiddenSkull");
FlashStatsAPI.prototype.easterEgg = function(name) {
	
	this._statsAction("easterEgg",name);
}


//						generic("tell a friend", 3);
FlashStatsAPI.prototype.generic = function(action, l) {
	
	this._statsAction(action);
}






//						download("http://www.lego.com/files/file.jpg");
FlashStatsAPI.prototype.download = function(value) {
	
	var s = s_gi(this._accountName);
	
	if (this._reference != "") {
		s.linkTrackVars='prop14';
		
		s.prop14=this._reference;
	}
	//s.prop15=this._reference+''+value;
	
	s.tl(this,'d',value);
}



FlashStatsAPI.prototype._statsAction = function(action, value) {
	
	var s = s_gi(this._accountName);
	
	s.linkTrackVars=this._prop14LinkVars+'prop15';
	
	s.prop14=this._reference;
	
	
	var prop15Items = new Array();
	
	prop15Items.push(this._reference);
	prop15Items.push(action);
	if (value != null) {
		prop15Items.push(value);
	}
	
	s.prop15 = prop15Items.join(":");
	
	s.tl(this,'o',action);
}





/*
FlashStatsAPI.prototype.appAction = function(a) {
	
	var s = s_gi(this._accountName);
	
	s.linkTrackVars=this._prop14LinkVars+'prop15';
	
	s.prop14=this._reference;
	s.prop15=this._reference+''+a;
	
	s.tl(this,'o',a);
}
*/
/*



11:17:54
11:18:33


6+33 == 39


*/
// MOVIE


FlashStatsAPI.prototype.movieOpen = function(mediaName, length) {
	
	var s = s_gi(this._accountName);
	
	s.Media.open(mediaName, length, this._reference);
	
	//alert("open ("+length+")");
}


FlashStatsAPI.prototype.moviePlay = function(mediaName, offset) {

	var s = s_gi(this._accountName);
	
	s.Media.play(mediaName, offset);
	
	//alert("play "+offset);
}


FlashStatsAPI.prototype.moviePause = function(mediaName, offset) {
	
	var s = s_gi(this._accountName);
	
	s.Media.stop(mediaName, offset);
	
	//alert("pause "+offset);
}


FlashStatsAPI.prototype.movieEnd = function(mediaName, offset) {
	
	var s = s_gi(this._accountName);
	
	//alert("s.Media.stop("+mediaName+", "+offset+");");
	
	s.Media.stop(mediaName, offset);
	s.Media.close(mediaName);
	
	//alert("end "+offset);
}




/*
FlashStatsAPI.prototype._statsMovie = function(a) {
	
	var s = s_gi(this._accountName);
	
	s.linkTrackVars=this._prop14LinkVars+'prop15';
	
	s.prop14=this._reference;
	s.prop15=this._reference+''+a;
	
	s.tl(this,'o',a);
}
*/



// movie load


FlashStatsAPI.prototype.movieStreamed = function(mediaName, speed) {
	
	this._statsAction("MovieStreamed", mediaName+":"+this._convertToSpeedCategory(speed));
}


FlashStatsAPI.prototype._generateSpeedCategories = function() {
	var a = new Array();
	a[0] = "cached";
	a[10] = "0-10";
	a[20] = "10-20";
	a[30] = "20-30";
	a[50] = "30-50";
	a[80] = "50-80";
	a[130] = "80-130";
	a[200] = "130-200";
	a[300] = "200-300";
	a[500] = "300-500";
	a["end"] = "500+";
	return a;
}

FlashStatsAPI.prototype._convertToSpeedCategory = function(speed) {
	
	this._speedCategories = this._generateSpeedCategories();
	
	for (var i in this._speedCategories) {
		if (speed < i) {
			return this._speedCategories[i];
		}
	} 
	
	return this._speedCategories["end"];
	
}



/*

<a href="download.pdf" onClick="

    var s=s_gi('RSID');

    s.linkTrackVars='eVar7,events,prop7,prop8';      //list de variabler der skal sendes

    s.linkTrackEvents='xxxxx';                       //specificer hvilket event der skal trackes - skal kun bruges hvis events skal trackes

    s.events='xxxxx';                                //specificer hvilket event der skal trackes - skal kun bruges hvis events skal trackes

    s.eVar7='xxxx';                                   //skal kun bruges hvis eVar skal trackes

    s.prop8=xxxxx;                                   //list de props og værdi der skal sendes

    s.prop7=xxxxx;

	
	s.t() = page change
	s.t();
	
			d = download
			//e = exit link
			o = costume link (action)
			
    s.tl(this,'d','DOWNLOAD_NAME');                  //d sættes hvis det er en download, e for exit og o for custom link

">Download Now</a>

*/
