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


function activaPopupResponder(){
	//Carga el pop si tiene display none
	if(jQuery("#caja-responder").css("display")=='none'){
			jQuery("#box").css({
				"opacity": "0.7"
			});
		var sitio=jQuery(document).scrollTop()+100+"px";	
		jQuery("#caja-responder").css({'top':sitio})
		jQuery("#box").fadeIn("slow", function (){
			jQuery("#caja-responder").fadeIn("slow");	
		});
	}
}

function cierraPopupResponder(){
	//Cierra el pop si tiene display block
	if(jQuery("#caja-responder").css("display")=='block'){
			jQuery("#caja-responder").fadeOut(400, function (){
				jQuery("#box").fadeOut(300);
				jQuery("#response-anuncio").css('display', 'none');

				//Vacia el formulario
				jQuery("#formulario-responder-anuncio input.mail").val('');
				jQuery("#formulario-responder-anuncio textarea").val('');

				jQuery("#formulario-responder-anuncio").css('display', 'block');

			});
			jQuery('#formulario-responder').show();
			jQuery('#response').hide();
	}

}

function centraPopupResponder(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#caja-responder").height();
	var popupWidth = jQuery("#caja-responder").width();
	jQuery("#caja-responder").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(".anuncio-tablon .boton").click(function(){
		jQuery("#ref-anuncio").text("Ref:"+jQuery(this).attr("name"));
		jQuery("#form-responder-anuncio input[name='id']").val(jQuery(this).attr("name"));
		centraPopupResponder();
		activaPopupResponder();
		return false;
	});
	//Cerrando pop con el aspa
	jQuery("#caja-responder .solicitar-cerrar span").click(function(){
		cierraPopupResponder();
	});
	//Cerrando pop con el haciendo click fuera de la caja
	jQuery("#box").click(function(){
		cierraPopupResponder();
	});
	//Cerrando pop con la tecla escape
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && jQuery("#caja-responder").css("display")=='block'){
			cierraPopupResponder();
		}
	});
	// TODO validacion del suscribirse
	jQuery('#form-responder-anuncio').validate( {
		rules: {
			email: {email:true, required: true},
			nombre: { minlength: 2, required: true},
			mensaje: {minlength: 5, required: true, maxlength: 250}
		},
		messages: {
			email: 'Introduzca un correo electr&oacute;nico v&aacute;lido',
			nombre: '<div class="errorcondiciones">Por favor, indique su nombre.</div>',
			mensaje: '<div class="errorcondiciones">Por favor escriba alguna consulta.</div>'
		},
		submitHandler: function() {
			var id = jQuery("#form-responder-anuncio input[name='id']").val();
			var email = jQuery("#form-responder-anuncio input[name='email']").val();
			var tlf = jQuery("#form-responder-anuncio input[name='tlf']").val();
			var mensaje = jQuery("#form-responder-anuncio textarea[name='mensaje']").val();
			var nombre = jQuery("#form-responder-anuncio input[name='nombre']").val();

			jQuery('#formulario-responder-anuncio').hide();
			jQuery('#response-anuncio').html('<strong>Por favor espere...</strong><br /><br />');
			jQuery('#response-anuncio').show();
			jQuery('#response-anuncio').load('/es/tablon-respuesta.html .response', {email: email, nombre: nombre, tlf: tlf, mensaje: mensaje, id:id});
		}
	});
});

