var Login = {
	init: function(){
		this.inputs = $$('#form input').associate(['email','clave','recordar']);

		//on key "enter" -> send form
		['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);

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

		this.title = $('form').getElement('h1');
		this.loginTitle = this.title.innerHTML;
		this.restoreTitle = _lng.restore;

		this.tip = new Element('div',{'class':'iVtip hidden'}).addEvent('click',this.hideTip.bind(this)).inject(document.body);
		this.info = new Element('div',{'id':'info','class':'hidden'}).injectBefore(this.inputs.email.getParent().getParent());

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

		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.title.innerHTML = mode ? this.restoreTitle : this.loginTitle;

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

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

	send: function(){
		if(this.checkData()){
			Loading.show();
			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':'');
			}
			else
			    data += '&leng=' + lenguaje;
			this.request.send({data:data});
		}
	},

	onUserRestore: function(response){
		if(['true','false'].contains(response)){
			if(response=='true'){
				this.toggle(false);
				this.showInfo('success',_lng.enviadoclave);
				this.inputs.clave.set('value','').focus();
			}else{
				this.showInfo('error',_lng.cuentamal);
			}
			Loading.hide();
		}
	},

	onUserLogin: function(response){
		if(response=='true'){
			return_url = return_url ? return_url : false;
			redirect(return_url || micuenta);
		}else if(response=='false'){
			Loading.hide();
			this.showInfo('error',_lng.emailyoclavemal);
		}
	},

	// INFO
	showInfo: function(klass,msg){
		this.info.set('class',klass).set('html',msg).setOpacity(0).removeClass('hidden').fade('in');
	},

	hideInfo: function(){
		this.info.addClass('hidden');
	},

	// TOOL TIP
	showTip: function(input,msg){
		var position = input.getCoordinates();
		this.tip.set('html','<div><h3>Error!</h3><p>'+msg+'</p></div><div class="foot"></div>').setStyles({'left':(position.right-10)+'px','top':(position.bottom-32)+'px'}).removeClass('hidden');
		window.scroll(0,position.top-50);
		input.focus();
	},

	hideTip: function(){
		if(this.tip){
			this.tip.addClass('hidden');
		}
	}
};

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