$(document).ready(function(){
		$("#container").pngFix();
		$("#escondido").hide();
		$('a').tipsy({gravity: 's', html: true }); 
                $('#ingresar').click(function(){
                        var valido = '';
                        var msg = ' es obligatorio.';
                        var username = $("#username").val(); 
                        var password = $("#password").val();
			var archivo = $("#url").val();
			$("#respuesta").animate({ opacity: '1'}, 200);
                        if (username.length<1) { valido += 'El Usuario'+msg; $('#username').addClass('error'); } else{ $('#username').removeClass('error'); }
                        if (password.length<1) { valido += '<br />La Contrase&ntilde;a'+msg; $('#password').addClass('error'); } else{ $('#password').removeClass('error'); }
                        if (valido!='') {
			$("#respuesta").animate({ height: '50px'}, {queue:false, duration:600, easing: 'easeOutBounce'});
                        $("#respuesta").html(valido);
                        setTimeout('$("#respuesta").animate({ height: "0px"}, {queue:false, duration:600, easing: "easeOutBounce"}).animate({opacity: ".0" }, 200)',3000);	
                        } else {
                        var datastr ='username=' + username + '&password=' + password;
			$("#respuesta").animate({ height: '20px'}, {queue:false, duration:600, easing: 'easeOutBounce'});
                        $("#respuesta").html("Validando ...&nbsp; <img src='imagenes/ajax-loader.gif'>");
			setTimeout('$("#respuesta").animate({ height: "0px"}, {queue:false, duration:600, easing: "easeOutBounce"}).animate({opacity: ".0" }, 200)',4000);
                        $.ajax({
                                type: "POST",
                                url: "ingreso.php",
                                data: datastr,
                                cache: false,
                                success: function(html){
				var cookie = $.cookies.get('USER');
                    		if(!cookie){
				$("#respuesta").html(html);
				document.flogin.password.value = "";
				document.flogin.password.focus();
                    		}else{
                                $("#respuesta").html(html);
                                $("div#login").fadeOut("slow");
				$("#cargando").hide();
				$(location).attr('href', archivo);
                                }
                                }
                                });
                        }
                        return false;
                });

		$('#ir_sistema').click(function(){
			var archivo = $("#url").val();
			$(location).attr('href', archivo);
			return false;
		});
	
		$('#lrecuperar').click(function(){
                        $("div#login").fadeOut("slow");
                        $("div#escondido").fadeIn("slow");
			document.frecuperar.mail.focus();
			return false;
                });
	
		$('#cerrar').click(function(){
                        $("div#login").fadeIn("slow");
                        $("div#escondido").fadeOut("slow");
			document.flogin.username.focus();
			return false;
                });


		$('#recuperar').click(function(){
                        $("#cargandom").fadeTo("slow", 1.0);
                        setTimeout('$("#cargandom").fadeTo("fast", 0.0)', 2000);
                        var valido = '';
                        var msg = ' es obligatorio.';
                        var mail = $("#mail").val();
                        if (mail.length<1) { valido += 'El Mail'+msg; $('#mail').addClass('error'); } else{ $('#mail').removeClass('error'); }
                        if (valido!='') {
                        $("#respuestam").fadeIn("slow");
                        $("#respuestam").html(valido);
			setTimeout('$("#respuestam").fadeOut("slow")', 2000);
                        } else {
                        var datastr ='mail=' + mail;
                        $("#respuestam").fadeIn("slow");
			$("#cargandom").fadeTo("slow", 0.0);
                        $("#respuestam").html("Validando ...&nbsp; <img src='imagenes/ajax-loader.gif'>");
                        $.ajax({
                                type: "POST",
                                url: "mail.php",
                                data: datastr,
                                cache: false,
                                success: function(html){
                                var cookie = $.cookies.get('MAIL');
                                if(!cookie){
                                $("#respuestam").html(html);
                                document.frecuperar.mail.value = "";
				document.frecuperar.mail.focus();
                                }else{
                                $("#respuestam").html(html);
                                setTimeout('$("div#escondido").fadeOut("slow")', 4000);
                                $("#cargandom").hide();
                                setTimeout('window.location.replace("index.php")',10000);
                                }}
                                });
                        }
                        return false;
                });
	
		$('a[rel*=facebox]').facebox();

		$('#cerrar-box').click($.facebox.close);

		$(':input[type!=submit]').each(function(){
			$(this).tipsy({gravity: 'w', html: true });
		});

		$('#mail').focus(function(){
			$(this).tipsy({gravity: 'n', html: true });
		});

        	$('.menu').addClass('manita'); 

});

