/***********
** Version 0.05
** 23 mayo 2009
** Autor Fernando
** Créditos a http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/
************/


function activaPopup(){
	//Carga el pop si tiene display none
	if(jQuery("#caja-news").css("display")=='none'){
			jQuery("#box").css({
				"opacity": "0.7"
			});
	
		jQuery("#box").fadeIn("slow", function (){
			jQuery("#caja-news").fadeIn("slow");	
		});
	}
}

function cierraPopup(){
	//Cierra el pop si tiene display block
	if(jQuery("#caja-news").css("display")=='block'){
			jQuery("#caja-news").fadeOut(400, function (){
				jQuery("#box").fadeOut(300);
			});
			jQuery('#formulario').show();
			jQuery('#response').hide();
	}
}

function centraPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#caja-news").height();
	var popupWidth = jQuery("#caja-news").width();
	jQuery("#caja-news").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
	});

	//solo para IE6 que es un lerdo
	jQuery("#box").css({
			"height": jQuery(document).height()
	});
}

jQuery(function() {
	
		jQuery("#enlace-suscribir").click(function(){
			centraPopup();
			activaPopup();
			return false;
		});
		//Cerrando pop con el aspa
		jQuery(".solicitar-cerrar span").click(function(){
			cierraPopup();
		});
		//Cerrando pop con el haciendo click fuera de la caja
		jQuery("#box").click(function(){
			cierraPopup();
		});
		//Cerrando pop con la tecla escape
		jQuery(document).keypress(function(e){
			if(e.keyCode==27 && jQuery("#caja-news").css("display")=='block'){
				cierraPopup();
			}
		});
		// validacion del suscribirse
 		jQuery('#suscripcionform').validate( {
                        rules: {
	                        email: {email:true, required: true},
                                confirmado: "required"
                        },
                        messages: {
                                email: 'Introduzca un correo electr&oacute;nico v&aacute;lido',
                                confirmado: '<div class="errorcondiciones">Por favor, acepte nuestras condiciones legales.</div>'
                        },
                        submitHandler: function() {
                                var sexo = jQuery("#suscripcionform input[name='sexo']").val();
                                var email = jQuery("#suscripcionform input[name='email']").val();
                                var edad =  jQuery("#suscripcionform select[name='edad']").val();
                                jQuery('#formulario').hide();
                                jQuery('#response').html('<strong>Por favor espere...</strong><br /><br />');
                                jQuery('#response').show();
                                jQuery('#response').load('/suscripcion.html .response', {email: email, sexo: sexo, edad: edad})
                        }
     		});
	
});
