/**
* Verifica consola de Firebug
*/
function consoleLog(str){
	if(typeof(console)!="undefined") {
		if(console) {
			console.log(str);
		}
	}
}

///CLASSES
/**
* Slot-Cookie prototype
*/
function SlotCookie(){
	this.COOKIE_NAME = "slot";
}

/**
 * Invoked from AS
 * returns s String
 */
SlotCookie.prototype.getSlot = function(){
	if($.cookie(this.COOKIE_NAME) != null){
	var s = eval("("+$.cookie(this.COOKIE_NAME)+")");
	}else{s = null;}
	return s;
}

/**
 * Invoked from AS
 * @param slotvalue {slot:'String'} JSON Object
 */
SlotCookie.prototype.setSlot = function(slotvalue){
	if(slotvalue != null){
	var s = "{slot:'"+slotvalue+"'}";
	$.cookie(this.COOKIE_NAME, s);
	}else{$.cookie(this.COOKIE_NAME, null);}
}

/**
* TdaCookie prototype
*/
function TdaCookie(){
	this.COOKIE_NAME = "TEGid";
	this.COOKIE_PATH = "/";
}

/**
* Invoked from AS
* @param ls {userName:'String', password:'String', userId:'String'} JSON
*/
TdaCookie.prototype.setTDACookie = function(username, password, userId){
	if(userId != null && username != null){
	var s = "{username:\""+username+"\",userId:\""+userId+"\"}";
	$.cookie(this.COOKIE_NAME, s, { path: this.COOKIE_PATH });
	} else {$.cookie(this.COOKIE_NAME, null);}
}


/**
 * LocalSession prototype
 */
function LocalSession(){
	this.COOKIE_NAME = "cn25";
	this.DELAY = 120000;
}

/**
 * Invoked from AS
 * returns {userName:String, password:String}
 */
LocalSession.prototype.getLocalSession = function(){
	if($.cookie(this.COOKIE_NAME) != null){
	var ls = eval("("+$.cookie(this.COOKIE_NAME)+")");
	delete ls.timestamp;
	}else{ls = null;}
	return ls;
}

/**
 * Invoked from AS
 * @param ls {userName:'String', password:'String'} JSON
 */
LocalSession.prototype.setLocalSession = function(ls){
	if(ls != null){ls.timestamp = new Date().getTime();
	var s = "{userName:'"+ls.userName+"',password:'"+ls.password+"',url:'"+ls.url+"',id:'"+ls.id+"',timestamp:'"+ls.timestamp+"'}";
	$.cookie(this.COOKIE_NAME, s);
	}else{$.cookie(this.COOKIE_NAME, null);}
}

/**
 * Invoked from JSP/HTML container
 */
LocalSession.prototype.onLoadHandler = function(event){
var _this = event.data;
if($.cookie(_this.COOKIE_NAME) != null){var ls = eval("("+$.cookie(_this.COOKIE_NAME)+")");
var now = new Date();if ((now.getTime() - ls.timestamp)>_this.DELAY){
$.cookie(_this.COOKIE_NAME, null);}}
}

/**
 * Invoked from JSP/HTML container
 */
LocalSession.prototype.onUnloadHandler = function(event){
var _this = event.data;
if($.cookie(_this.COOKIE_NAME) != null){var ls = eval("("+$.cookie(_this.COOKIE_NAME)+")");
ls.timestamp = new Date().getTime();
var s = "{userName:'"+ls.userName+"',password:'"+ls.password+"',url:'"+ls.url+"',id:'"+ls.id+"',timestamp:'"+ls.timestamp+"'}";
$.cookie(_this.COOKIE_NAME, s);}
}
///---



//jQuery required
if(this["jQuery"]){
var localSession = new LocalSession();
var slotSession = new SlotCookie();

$(window).bind("load", localSession, localSession.onLoadHandler);
$(window).bind("unload", localSession, localSession.onUnloadHandler);
}else{
consoleLog("jQuery required !");
}
