var Login = {
	init: function(){
		this.info = {};
		this.container = $('userinfo').getFirst();
		this.inputs = this.container.getElements('input').associate(['email','clave']);
		this.buttons = this.container.getElements('button,b').associate(['enviar','cancelar','recuperar']);

		$each(this.inputs,function(el){el.addEvent('keyup',function(e){var ev=new Event(e);if(ev.key=='enter'){this.submit();}}.bind(this))},this);

		this.buttons.enviar.addEvent('click',this.submit.bind(this));
		this.buttons.cancelar.addEvent('click',this.toggleMode.bind(this,[false])).addClass('hidden');
		this.buttons.recuperar.addEvent('click',this.toggleMode.bind(this,[true]));

		this.msgs = [
			'Error!\nemail incorrecto',
			'Error!\nemail/clave incorrectos',
			'Error!\nNo se pudo recuperar su clave',
			'Se le ha enviado su clave al email proporcionado.'
		];

		this.request = new Request({url:base+'usuario.ajax',onSuccess:function(r){this['on'+this.request.action](r);}.bind(this),onFailure:onError});
	},

	checkData: function(){
		this.buttons.enviar.focus();

		$each(this.inputs,function(el,l){this.info[l] = el.value = el.value.trim()},this);

		if(!this.info.email.isMail() || (!this.recuperar && !this.info.clave.isPassword())){
			alert(this.recuperar ? this.msgs[0] : this.msgs[1]);
			return false;
		}
		return true;
	},

	toggleMode: function(mode){
		this.recuperar = mode;
		this.inputs.clave.getParent()[(mode?'add':'remove')+'Class']('hidden');
		this.buttons.recuperar[(mode?'add':'remove')+'Class']('hidden');
		this.buttons.cancelar[(mode?'remove':'add')+'Class']('hidden');
	},

	submit: function(){
		if(this.checkData()){
			loading.show($('cont2'));
			this.request.action = this.recuperar ? 'Restore' : 'Login';
			this.request.send({data:'action='+this.request.action+'&email='+this.info.email+'&clave='+this.info.clave});
		}
	},

	onLogin: function(response){
		if(response!=='false'){
			var info = JSON.decode(response);
			if(['ERROR_CLAVE','NO_EXISTE'].contains(info.status)){
				alert(this.msgs[1]);
				this.inputs.clave.set('value','');
				loading.remove();
			}else{
				this.container.innerHTML = 'Hola <strong>'+info.nombre+'</strong> - <a href="/'+info.link+'">Editar</a> - <a href="/'+salir+'">Cerrar sesión</a>';
			}
			if(this.onComplete){
				this.onComplete(info.data,info.status);
			}else{
				loading.remove();
			}
		}
	},

	onRestore: function(response){
		if(['true','false'].contains(response)){
			alert(this.msgs[response=='true'?3:2]);
			if(response=='true'){
				this.toggleMode(false);
				this.inputs.clave.set('value','').focus()
			}
			loading.remove();
		}else{
			alert(response);
		}
	}
};

window.addEvent('domready',function(){
	new sIFR('#title h2','css/sifr.swf',{fontSize:22,letterspacing:1});

	$$('#nav1 li ul').each(function(block){
		var toggler = block.getPrevious();
		toggler.ref = block.set('opacity',0);
		toggler.addEvent('mouseenter',function(){this.ref.fade('in');this.getParent().addClass('active hover')});
		toggler.addEvent('mouseleave',function(){(function(){if(!this.prevent) this.ref.fade('out')}).delay(100,this);this.getParent().removeClass('active')});
		block.addEvents({
			'mouseenter':function(){this.getPrevious().prevent=true;this.getParent().addClass('active hover')},
			'mouseleave':function(){this.getPrevious().prevent=false;this.fade('out');this.getParent().removeClass('active hover')}
		});
	});

	$('textsearch').addEvents({
		'focus':function(){if(!this.readOnly){this.value='';}},
		'blur':function(){if(!this.readOnly){if(!this.value.isMail()) this.value='- Ingrese su Email -';}}
	});

	$$('.blank').addEvent('click',function(){return !window.open(this.href);});

	var animacion = new SWFObject('/css/cabecera.swf', "srcvid", "700", "125", "6", "#FFFFFF");
	animacion.addParam("wmode", "transparent");
	animacion.write("flash");

	if(!loged){
		Login.init();
	}
});