

var ideelicInitialize = function(){
	return {		
		startUp: function() {
			loginBar.render();		
			SignupDlg.render();
			Ext.History.init();
		}
	}
}();

Ext.EventManager.onDocumentReady(ideelicInitialize.startUp, ideelicInitialize, true);



/**
 * @author Olivier Monod
 */
LoginBar = function() {};

LoginBar.prototype = {
	
	render: function() {
		Ext.QuickTips.init();		
		Ext.DomHelper.UseDom = false;
							
		this.el = Ext.get("ideelic-login");
		
		this.loginPan = this.el.child("div#ideelic-login-passwd");
		this.forgotPan = this.el.child("div#ideelic-login-forgot");
		this.loginPan.enableDisplayMode();
		this.forgotPan.enableDisplayMode();
		
		this.iPseudo = this.el.child("input#pseudo", true);
		this.iPwd = this.el.child("input#pwd", true);
		this.iMail = this.el.child("input#mail", true);
	 	
		this.iPseudo.value = getCookie("__idf_ideelic");
		
		this.loginBt = new Ext.Button( this.el.child("div#login-bt"), {text:"Valider", hideParent:true, type:"submit", minWidth:70, handler:this.onSubmit, scope:this});
		this.forgotBt = this.el.child("label#forgot-bt", false);
		this.cancelBt = this.el.child("label#cancel-bt", false);
  
  		this.errorMsgEl = Ext.get("ideelic-login-error");
		//this.errorMsgEl.anchorTo(document.body, 't', [-223, 0]);
 		this.errorMsg = this.errorMsgEl.child("div#msg", true);
		//this.errorCloseBt = this.errorMsgEl.child("a#close-bt");
		this.errorMsgEl.on("click", this.hideErrorEl, this);
		
		this.forgotBt.on("click", this.onBarBtClick, this);
		this.cancelBt.on("click", this.onBarBtClick, this);		

		this.okBt = new Ext.Button( this.el.child("div#ok-bt"), {text:"Ok", handler:this.onGetLogin, scope:this, minWidth:70});
	},
	
	hideErrorEl: function() {
		this.errorMsgEl.dom.style.display = 'none';
	},
	
	onGetLogin: function() {
		if (checkMail(this.iMail.value)) {
			Ext.lib.Ajax.request("POST", "/data/get_login.php?PHPSESSID="+getCookie("PHPSESSID"), {
				success: this.successForgot,
				failure: this.failureForgot,
				scope: this,
				timeout: (this.timeout * 1000)
			}, "mail=" + this.iMail.value );
		}
		else {
			this.errorMsg.innerHTML = "Adresse email incorrecte";
			this.errorMsgEl.slideIn('t', {easing: 'easeOut', duration: .25, useDisplay:true});
		}
	},

	failureForgot: function(response) {		
		var o = (!response.responseText ? false : Ext.decode(response.responseText));		
		var msg;
		if(response.statusText == "communication failure") {
			msg = "Connexion à Ideelic impossible.";
		}
		else if( o && o.msg ) {
			msg = o.msg;
		} else {
			msg = "Service indisponible...";
		}
		this.errorMsg.innerHTML = msg;
		this.errorMsgEl.slideIn('t', {easing: 'easeOut', duration: .25, useDisplay:true});
	},
	
	successForgot: function(response) {
		var o = (!response.responseText ? false : Ext.decode(response.responseText));
		if( o && o.msg ) {
			this.errorMsg.innerHTML = o.msg;
			this.errorMsgEl.slideIn('t', {easing: 'easeOut', duration: .25, useDisplay:true});
		} else {
			this.failure(response);
		}		
	},
	
	onBarBtClick: function() {
		this.errorMsg.innerHTML = this.iPwd.value = this.iMail.value = this.iPseudo.value = "";
		this.hideErrorEl();
		
		if (this.forgotPan.isVisible()) {
			this.iPseudo.value = getCookie("__idf_ideelic");
			//this.el.dom.className = "ideelic-login";			
			this.forgotPan.hide();
			this.loginPan.show();
		}
		else {
			//this.el.dom.className = "ideelic-forgot";			
			this.loginPan.hide();
			this.forgotPan.show();
		}
	},
	
	onSubmit: function() {
		this.errorMsg.innerHTML = "";
		this.hideErrorEl();
		startWait.show();

		Ext.lib.Ajax.request("POST", "/data/login.php?PHPSESSID="+getCookie("PHPSESSID"), {
			success: this.success,
			failure: this.failure,
			scope: this,
			timeout: (this.timeout * 1000)
		}, "pseudo=" + this.iPseudo.value + "&pwd=" + this.iPwd.value );
	},
	
	failure: function(response) {
		startWait.hide();
		this.iPwd.value = "";
		
		var o = (!response.responseText ? false : Ext.decode(response.responseText));
		var msg;
		if(response.statusText == "communication failure") {
			msg = "Connexion à Ideelic impossible.";
		}
		else if( o && o.msg ) {
			msg = o.msg;
		} else {
			msg = "Connexion à Ideelic impossible.";
		}

		this.errorMsg.innerHTML = msg;
		this.errorMsgEl.slideIn('t', {easing: 'easeOut', duration: .25, useDisplay:true});
	},
	
	success: function(response) {
		var jresp = (!response.responseText ? false : Ext.decode(response.responseText));
		if( jresp.html && jresp.user) {
			setCookie("__idf_ideelic", this.iPseudo.value);
			
			this.doLogin(jresp.html, jresp.user);
		} else {
			this.failure(response);
		}		
	},
	
	doLogin: function(html, user) {
		//pag.remove();
		require_css("/elements/elements-private-all-css.php");


		startWait.next();
		this.destroy(true);
		
		eval(html);
		privateHome.init(user);
		startWait.next();
		privateHome.startChat();
	},
	
	destroy: function(removeSignup) {
		this.loginBt.destroy();
		this.okBt.destroy();
		this.errorMsgEl.remove();
		this.el.remove();
		if(removeSignup) {
			if (SignupDlg.rendered) {
				SignupDlg.destroy();
			}
		}		
		Ext.get(document.body).child("div.ideelic-header").remove();
		Ext.get(document.body).child("div.ideelic-main").remove();
		Ext.get("ideelic-footer").remove();
		Ext.get(document.body).child("div.ideelic-main-page").remove();
	}
}

var loginBar = new LoginBar();