$(document).ready(function(){
	
	/**************************/
	/* ACCIONES INPUT NOMBRE */
	/**************************/
	$('input#ingresar-nombre_txt').focus(function() {
		var value=$(this).val();
		if( value == 'E-mail' )
			$(this).val('');
	}).blur(function() {
		var value=$(this).val();
		if(value == '') $(this).val('E-mail');
	});
	
	
	/**************************/
	/* ACCIONES INPUT PASSWORD */
	/**************************/
	$('input#ingresar-pass_txt').focus(function() {
		var value = escape($(this).val());
		if( value == 'contrase%F1a' )
			$(this).val('');
	}).blur(function() {
		var value=$(this).val();
		if(value == '') $(this).val(unescape('contrase%F1a'));
	});
	
	/********************************/
	/* ACCIONES INPUT PASSWORD LOST */
	/********************************/
	$('input#olvidar-input').focus(function() {
		var value=$(this).val();
		if( value == 'Escribe tu e-mail' )
			$(this).val('');
	}).blur(function() {
		var value=$(this).val();
		if(value == '') $(this).val('Escribe tu e-mail');
	});
	
	$('#ingresar_aviso').css({ display: 'none', visibility: 'visible' });
	
	/***********************************/
	/* ULTIMOS USUARIOS	*/
	/***********************************/
	var params = { wmode: "transparent" };
	swfobject.embedSWF("flash/includes/ingresar/usuarios.swf", "ultimosUsuarios-Thumb-flash", "282.35", "59", "8", "http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b75", {}, params, {});

});

/*******************************/
/* MOSTRAR EL MENSAJE DE AVISO */
/*******************************/
function ingresarAviso(forzar, msg)
{
	var estadoActual = 'none';
	if( forzar == true ) estadoActual = "block";
	$('#ingresar_aviso_msg').html(msg);
	if( estadoActual == "none" ) {
		if($.browser.msie) // si es IE
			$('#ingresar_aviso').show();
		else
			$('#ingresar_aviso').fadeIn("slow");
	} else {
		if($.browser.msie) // si es IE
			$('#ingresar_aviso').hide();
		else
			$('#ingresar_aviso').fadeOut("slow");
	}
}
/***********************************/
/* VERIFICA QUE EXISTA EL USUARIO */
/***********************************/
function loginAjax()
{
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var email = $('input#ingresar-nombre_txt').val();
	var password = $('input#ingresar-pass_txt').val();
	if( email == "" || email == "Email" || password == "" || escape(password) == "contrase%F1a" )
	{
		ingresarAviso(false, 'Llena todos los campos.');
	} else if( !emailReg.test(email) ) {
		ingresarAviso(false, 'E-mail Incorrecto.');
	} else {
		$.ajax({
				type: "POST",
				url: "includes/ingresarAjax.php",
				data: "email="+email+"&password="+password,
				success: function(datos){
					if( datos.substr(0, 5) == "Error" )
					{
						ingresarAviso(false, datos.substr(7));
					} else {
						window.location.reload();
					}
			  }
		});
	}
}
/**********************/
/* RECUPERAR PASSWORD */
/**********************/
function olvidarHideShow(forzar)
{
	var estadoActual = $('#olvidar-contrasena').css("display");
	if( forzar == true ) estadoActual = "block";
	if( estadoActual == "none" ) {
		if($.browser.msie) // si es IE
			$('#olvidar-contrasena').show();
		else
			$('#olvidar-contrasena').fadeIn("slow");
	} else {
		if($.browser.msie) // si es IE
			$('#olvidar-contrasena').hide();
		else
			$('#olvidar-contrasena').fadeOut("slow");
	}
}
function recuperarPass()
{
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var email = $('input#olvidar-input').val();
	if( email == "" || email == "Escribe tu Email" )
	{
		ingresarAviso(false, 'Llena todos los campos.');
	} else if( !emailReg.test(email) ) {
		ingresarAviso(false, 'E-mail Incorrecto.');
	} else {
		$.ajax({
				type: "POST",
				url: "includes/pwdPerdido.php",
				data: "email="+email,
				success: function(datos){
					if( datos.substr(0, 5) == "Error" )
					{
						ingresarAviso(false, datos.substr(7));
					} else {
						olvidarHideShow(true);
					}
			  }
		});
	}
}
$('#olvidasteBtn, #olvidar-cerrar').click(function(){
	olvidarHideShow(false);
});
$('#olvidar-enviar').click(function() {
	recuperarPass();
});