// Variables
tiempoFade = 400;

/* Para abrir fotos */

function view(url) { 
	// abre un popup con las caracter�sticas de la p�gina y la url pasada como par�metro.
	window.open(url,'','scrollbars=1,width=600,height=590') ; 
}


/* Funciones gen�ricas */

function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function dar_alto(){
	/* Function modificated from 
	   Lightbox JS: Fullsize Image Overlays 
	   by Lokesh Dhakar - http://www.huddletogether.com */
	
	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		yScroll = document.body.scrollHeight;
	} else {
		yScroll = document.body.offsetHeight;
	}
	
	if (self.innerHeight) {
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll > windowHeight){
		alto = yScroll;
	} else { 
		alto = windowHeight;
	}	
	return alto;
}
function dar_ancho(){
	/* Function modificated from 
	   Lightbox JS: Fullsize Image Overlays 
	   by Lokesh Dhakar - http://www.huddletogether.com */
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
	} else {
		xScroll = document.body.offsetWidth;
	}
	
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
	}	
	
	if(xScroll > windowWidth){	
		ancho = xScroll;
	} else {
		ancho = windowWidth;
	}
	return ancho;
}



///////////////////////////////////////////////////
//       		Parte de contacto    			 //
///////////////////////////////////////////////////

function contacto(){
	divFondo = document.getElementById('popup_layout');
	divPopup = document.getElementById('contenedor_contacto');
	
	//divContenedor = document.getElementById('contenedor');
	
	//arrayPageSize = getPageSize();
	
	divFondo.style.height = (dar_alto() + 'px');
	divFondo.width = "100%";
	divFondo.style.display = "block";
	//$(divFondo).fadeIn(tiempoFade, null);
	
	//setTimeout('continuaAbrir()', tiempoFade);
	continuaAbrir();
}
function continuaAbrir(){
	divPopup.style.top   = "150px";
	divPopup.style.left = (((dar_ancho() - 470) / 2)+"px");
	divPopup.style.display = "block";
}

function contactar() {
	email = document.formulario_contacto.email.value;
	consulta = document.formulario_contacto.consulta.value;
	
	divMensajes = document.getElementById('mensajes');
	//divBotones = document.getElementById('botones');
	divFormulario = document.getElementById('formulario_contacto');
	
	if (email==""){
		divMensajes.innerHTML = '<span class="fuente_error">&iexcl;D&eacute;janos tu direcci&oacute;n para que podamos contestarte!</span>';
		//new Effect.Highlight(divMensajes, { startcolor: '#ffff99', endcolor: '#ffffff' });
		divMensajes.style.backgroundColor = "#FFFFFF";
		return;
	}
	if (consulta==""){
		divMensajes.innerHTML = '<span class="fuente_error">&iquest;Seguro que no quieres decir nada m&aacute;s?</span>';
		//new Effect.Highlight(divMensajes, { startcolor: '#ffff99', endcolor: '#ffffff' });	
		divMensajes.style.backgroundColor = "#FFFFFF";
		return;
	}
	
	//divBotones.innerHTML = 'Enviando...';
	
	ajax=objetoAjax();
	
	ajax.open("POST", "js/contactar.php", true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if (ajax.responseText=="0"){
				// enviado ok
				divMensajes.innerHTML ="";
				divFormulario.innerHTML = '<br/><strong>&iexcl;Se ha enviado tu consulta!</strong><br/><br/>Lo antes posible nos pondremos en contacto contigo.<br/><br/><br/>';					
			} else if (ajax.responseText=="1"){
				// ha habido error
				divMensajes.innerHTML = 'Ha ocurrido un error, por favor vuelve a intentarlo!';
				//new Effect.Highlight(divMensajes, { startcolor: '#ffff99', endcolor: '#ffffff' });	
			}
		}
	}	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("email="+email+"&consulta="+consulta);
	
	document.getElementById('botonContacto').innerHTML = "enviando...";
}

function cerrarContacto(){
	divPopup = document.getElementById('contenedor_contacto');
	divPopup.style.display = "none";
	
	divFondo = document.getElementById('popup_layout');
	//$(divFondo).fadeOut(tiempoFade, null);
	divFondo.style.display = "none";
	
	
	//para que si se vuelve a abrir despues de cerrarla aparezca bien!
	divContenido = document.getElementById('contacto');
	divContenido.innerHTML = '<div class="titulo_popup">Contacta con nosotros</div><br />Si quieres ponerte en contacto con nosotros env&iacute;a este formulario y te contestaremos lo antes posible.<br /><br /><div id="mensajes">&nbsp;</div><div id="formulario_contacto"><form action="javascript:contactar()" name="formulario_contacto"><table width="200" border="0" align="center"><tr><td>email:</td><td><input name="email" type="text" class="input_b" /></td></tr><tr><td>consulta:</td><td><textarea name="consulta" cols="45" rows="6" class="input_b"></textarea></td></tr><tr><td>&nbsp;</td><td><div align="right"><a href="javascript:contactar()" class="link_negro">enviar</a>&nbsp;&nbsp;&nbsp;</div></td></tr></table></form>';
	
	//divContenido.innerHTML = "<span class=\"Estilo2\">TOOZHINK Newsletter!</span><br/><br/>Puedes recibir las &uacute;ltimas noticias y promociones en tu correo, simplemente ap&uacute;ntate al newsletter!<br/><br/><form id=\"newsletterForm\" name=\"newsletterForm\" action=\"javascript:subscribir()\"><input name=\"dircorreo\" id=\"dircorreo\" type=\"text\" value=\"tu@email.com\"></form><div align=\"right\"><a href=\"javascript:subscribir()\" class=\"link\"><br /><br />enviar</a>&nbsp;&nbsp;&nbsp;</div>";
}


///////////////////////////////////////////////////
//       		Parte de newsletter    			 //
///////////////////////////////////////////////////

function newsletter(){
	divFondo = document.getElementById('popup_layout');
	//divContenedor = document.getElementById('contenedor');
		
	divFondo.style.height = (dar_alto() + 'px');
	divFondo.width = "100%";
	divFondo.style.display = "block";
	//$(divFondo).fadeIn(tiempoFade, null);
	//setTimeout('continuaAbrirNews()', tiempoFade);
	
	continuaAbrirNews();
}
function continuaAbrirNews(){
	divPopup = document.getElementById('contenedor_newsletter');
	divPopup.style.top   = "150px";
	divPopup.style.left = (((dar_ancho() - 470) / 2)+"px");
	divPopup.style.display = "block";
	
	divContenido = document.getElementById('contenidoNewsletter');
	divContenido.innerHTML = '<div class=\"titulo_popup\">TOOZHINK Newsletter!</div><br/><br/><br/><br/>enviando...';
	
	correo = document.getElementById('dircorreo').value;	
	
	// comprobar mediante expresion regular que la direccion es valida
	re=/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})$/;
    if(!re.exec(correo))    {
		divContenido.innerHTML = "<div class=\"titulo_popup\">TOOZHINK Newsletter</div><br/><br/><br/><br/>Introduce una direcci&oacute;n v&aacute;lida!<br/><br/><br/><br/><div align=\"right\"><a href=\"javascript:cerrarNewsletter()\" class=\"link_negro\">cerrar</a>&nbsp;&nbsp;&nbsp;</div>";
		return;
    }
	
	
	if (correo=="" || correo=="tu@email.com"){
		divContenido.innerHTML = "<div class=\"titulo_popup\">TOOZHINK Newsletter</div><br/><br/>&iexcl;Introduce una direcci&oacute;n v&aacute;lida!<br/><br/><br/><br/><div align=\"right\"><a href=\"javascript:cerrarNewsletter()\" class=\"link_negro\">cerrar</a>&nbsp;&nbsp;&nbsp;</div>";
		return;
	}else{
		//parte para conexion con script php
		ajax=objetoAjax();
		
		ajax.open("POST", "js/subscribir.php", true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				if (ajax.responseText=="0"){
					divContenido.innerHTML = "<div class=\"titulo_popup\">TOOZHINK Newsletter</div><br/><br/>Te hemos enviado un correo a la dirección proporcionada, simplemente pincha en el link que encontraras para confirmar tu direccion.<br/><br/><div align=\"right\"><br/><br/><br/><br/><a href=\"javascript:cerrarNewsletter()\" class=\"link_negro\">cerrar</a>&nbsp;&nbsp;&nbsp;</div>";
				}else if(ajax.responseText=="2"){
					divContenido.innerHTML = "<div class=\"titulo_popup\">TOOZHINK Newsletter</div><br/><br/>Esta dirección ya está apuntada!<br/><br/><br/><br/><div align=\"right\"><a href=\"javascript:cerrarNewsletter()\" class=\"link_negro\">cerrar</a>&nbsp;&nbsp;&nbsp;</div>";
				}
				else{
					divContenido.innerHTML = "<div class=\"titulo_popup\">TOOZHINK Newsletter</div><br/><br/>Ha ocurrido un error, por favor int&eacute;ntelo m&aacute;s tarde.<br/><br/><br/><br/><div align=\"right\"><a href=\"javascript:cerrarNewsletter()\" class=\"link_negro\">cerrar</a>&nbsp;&nbsp;&nbsp;</div>";
				}
			}
		}
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send("correo="+correo)
	}
}

function cerrarNewsletter(){
	divPopup = document.getElementById('contenedor_newsletter');
	divPopup.style.display = "none";
	
	divFondo = document.getElementById('popup_layout');
	divFondo.style.display = "none";
	//$(divFondo).fadeOut(tiempoFade, null);
}


///////////////////////////////////////////////////
//       		Parte de camps 	    			 //
///////////////////////////////////////////////////

function camps(){
	divFondo = document.getElementById('popup_layout');
	//divContenedor = document.getElementById('contenedor');
		
	divFondo.style.height = (dar_alto() + 'px');
	divFondo.width = "100%";
	//divFondo.style.display = "block";
	$(divFondo).fadeIn(tiempoFade, null);
	
	setTimeout('continuaAbrirCamps()', tiempoFade);
}
function continuaAbrirCamps(){
	divPopup = document.getElementById('popupCamps');
	divPopup.style.top   = "70px";
	divPopup.style.left = (((dar_ancho() - 670) / 2)+"px");
	divPopup.style.display = "block";
}


function cerrarCamps(){
	divPopup = document.getElementById('popupCamps');
	divPopup.style.display = "none";

	divFondo = document.getElementById('popup_layout');
	$(divFondo).fadeOut(tiempoFade, null);
}




///////////////////////////////////////////////////
//       		Parte genérica 	    			 //
///////////////////////////////////////////////////

function showPopup(id){
	// Muestra el popup cargando el contenido segun parametro.
	
	// 1. Mostrar fondo
	divFondo = document.getElementById('popup_layout');		
	divFondo.style.height = (dar_alto() + 'px');
	divFondo.width = "100%";
	divFondo.style.display = "block";
	
	// 2. Mostrar contenedor popup
	divPopup = document.getElementById('contenedorPopup');
	divPopup.style.top   = "150px";
	divPopup.style.left = (((dar_ancho() - 470) / 2)+"px");
	divPopup.style.display = "block";
	
	// 3. Cargar contenido
	divContenido = document.getElementById('contenidoPopup');
	divContenido.innerHTML = "loading...";
	
	// introducir contenido
	divContenido.innerHTML = $.ajax({
			url: "js/popups/"+id+".php",
			async: false
		}).responseText;
}


function cerrarPopup(){
	// cierra el popup
	divPopup = document.getElementById('contenedorPopup');
	divPopup.style.display = "none";
	
	// quitar fondo
	divFondo = document.getElementById('popup_layout');
	$(divFondo).fadeOut(tiempoFade, null);
	
	// Vaciar el popup
	document.getElementById('contenidoPopup').innerHTML = "loading...";
}


