var Login = {
	initialize: function(){
		this.form = $('form');

		//inputs
		this.inputs = this.form.getElements('input').associate(['email','clave','recordar']);
		['email','clave'].each(function(l){this.inputs[l].addEvent('keyup',function(e){var ev=new Event(e);if(ev.key=='enter'){this.send();}}.bind(this));},this);

		//buttons
		this.buttons = this.form.getElements('button,span').associate(['enviar','cancelar','recuperar']);
		this.buttons.recuperar.addEvent('click',this.toggle.bind(this,[true]));
		this.buttons.cancelar.addEvent('click',this.toggle.bind(this,[false]));
		this.buttons.enviar.addEvent('click',this.send.bind(this));

		//tip
		this.tip = new Element('div',{'class':'iVtip hidden'}).addEvent('click',this.hideTip.bind(this)).inject(document.body);
		this.tip_hide = 0;

		//request
		this.request = new Request({url:BASE.ajax+'usuario.ajax',onSuccess:function(r){this['on'+this.request.action](r);}.bind(this),onFailure:onError});

		//start
		if(window.location.href.indexOf('#')>-1){
			var anchor = window.location.href.match(/#.+$/);
			if(anchor=='#pwd'){
				var restore = true;
				this.toggle(true);
			}else{
				return_url += anchor;
			}
		}
		this.toggle(restore);
	},

	toggle: function(mode){
		this.mode = mode;
		this.inputs.email.focus();

		['clave','recordar'].each(function(i){
			this.inputs[i].getParent().getParent()[(mode?'add':'remove')+'Class']('hidden');
		},this);

		this.buttons.cancelar[(mode?'remove':'add')+'Class']('hidden');
		this.buttons.recuperar[(mode?'add':'remove')+'Class']('hidden');
	},

	checkData: function(){
		this.hideTip();
		if(!this.inputs.email.value.test(iRules.email.regx)){
			this.showTip('email',this.inputs.email.value==''?_jlng.requerido:iRules.email.msg);
			return false;
		}
		if(!this.mode& !this.inputs.clave.value.test(iRules.password.regx)){
			this.showTip('clave',this.inputs.clave.value==''?_jlng.requerido:iRules.password.msg);
			return false;
		}
		return true;
	},

	send: function(){
		if(this.checkData()){
			Loading.show('verificando datos...');
			this.hideTip();
			this.request.action = this.mode ? 'UserRestore' : 'UserLogin';
			var data = 'action='+this.request.action+'&email='+this.inputs.email.value;
			if(!this.mode){
				data += '&clave='+this.inputs.clave.value+(this.inputs.recordar.checked?'&recordar=1':'');
			}
			this.request.send({data:data});
		}
	},

	onUserRestore: function(response){
		if(['true','false'].contains(response)){
			if(response=='true'){
				this.toggle(false);
				this.inputs.clave.set('value','').focus();
				Loading.set('Le hemos enviado la clave a su correo.','success');
			}else{
				Loading.set('No se pudo restaurar su clave.','error');
			}
			Loading.hide.delay(2000,Loading);
		}else{
			Loading.set('Error!','error');
		}
	},

	onUserLogin: function(response){
		if(response=='true'){
			redirect(return_url);
		}else if(response=='false'){
			Loading.set('El email y/o clave incorrectos.','error');
			Loading.hide.delay(2000,Loading);
		}
	},

	// TOOL TIP
	showTip: function(l,msg){
		$clear(this.tip_hide);
		this.tipl = l;
		this.inputs[l].addClass('fail');
		var ref = this.inputs[l].getParent().getCoordinates();
		var increment = Browser.Engine.trident4 ? document.body.scrollTop : 0;
		this.tip.set('html','<div><h3>Error!</h3><p>'+msg+'</p></div><div class="foot"></div>').setStyles({'left':(ref.right+4)+'px','top':(ref.bottom+increment-48)+'px'}).removeClass('hidden');
		this.tip_hide = this.hideTip.delay(10000,this);
		window.scroll(0,ref.top+increment-50);
		this.inputs[this.tipl].focus();
	},

	hideTip: function(){
		$clear(this.tip_hide);
		this.tip.addClass('hidden');
		if(this.tipl){
			this.inputs[this.tipl].removeClass('fail');
		}
	}
};

window.addEvent('domready',Login.initialize.bind(Login));

/*Date: Wed, 08 Sep 2010 12:49:13 GMT */