// JavaScript Document


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

var enlacesNinotsCabecera;
var ninot;
var i;

var matrizTabs = new Array();
var matrizAlturasTabs = new Array();

function init() {
	
	// Resaltamos el ninot de la pagina donde nos encontramos.
	if(document.getElementById('ninotActivo-' + pag)) {
		document.getElementById('ninotActivo-' + pag).style.display = 'block';
		document.getElementById('ninotActivo-' + pag).parentNode.className = 'enlaceApartadoCabeceraActivo';
	}
	
	enlacesNinotsCabecera = getElementsByClassName('tdEnlaceApartadoCabecera');
	
	for(i = 0; i < enlacesNinotsCabecera.length; i++) {
		ninot = enlacesNinotsCabecera[i];
	//	$('#' + ninot.id).mouseover =
			ninot.onmouseover = function() {
				jQuery('#' + this.children[0].children[0].children[1].id).fadeIn('fast');
				for(var a = 0; a < enlacesNinotsCabecera.length; a++) {
				//	alert(enlacesNinotsCabecera[a].children[0].children[0].children[1].id);
					if(enlacesNinotsCabecera[a] != this && enlacesNinotsCabecera[a].children[0].children[0].children[1] != document.getElementById('ninotActivo-' + pag)) {
						jQuery('#' + enlacesNinotsCabecera[a].children[0].children[0].children[1].id).fadeOut('fast');
					}
				}
			};
			
	/*		ninot.onmouseout = function() {
				$('#' + this.children[0].children[0].children[1].id).fadeOut('fast');
			};*/
	}
	
	document.getElementById('tdSupCabecera').onmouseover = document.getElementById('tdInfCabecera').onmouseover = document.getElementById('tdIzqCabecera').onmouseover = 
		document.getElementById('tdDerCabecera').onmouseover = document.getElementById('wrapperContenido').onmouseover = function() {
			
		for(var a = 0; a < enlacesNinotsCabecera.length; a++) {
			if(enlacesNinotsCabecera[a] != this && enlacesNinotsCabecera[a].children[0].children[0].children[1] != document.getElementById('ninotActivo-' + pag)) {
				jQuery('#' + enlacesNinotsCabecera[a].children[0].children[0].children[1].id).fadeOut('fast');
			}	
		}
		
	}
	
/*	document.getElementById('tablaCabecera').onmouseout = function() {
		for(var a = 0; a < enlacesNinotsCabecera.length; a++) {
			$('#' + enlacesNinotsCabecera[a].children[0].children[0].children[1].id).fadeOut('fast');	
		}
	}
	
	document.getElementById('wrapperTablaCabecera').onmouseout = function() {
		for(var a = 0; a < enlacesNinotsCabecera.length; a++) {
			enlacesNinotsCabecera[a].children[0].children[0].children[1].style.display = 'none';	
		}
	}*/
	
	if(document.getElementById('enlaceReserva')) {
		var int = setInterval('resaltaEnlaceReservas()', 3000);
		
		document.getElementById('enlaceReserva').onmouseover = function() {
			clearInterval(int);
			jQuery('#enlaceReserva').stop();
		//	document.getElementById('enlaceReserva').style.color = 'red';
			jQuery('#enlaceReserva').animate( { color: 'red' }, 10)
		}
		document.getElementById('enlaceReserva').onmouseout = function() {
			int = setInterval('resaltaEnlaceReservas()', 3000);
		//	document.getElementById('enlaceReserva').style.color = 'red';
		}
	}
	
	// Si en la pagina hay desplegables, nos apuntamos sus nombres y sus alturas y luego las reducimos a cero.
	if(getElementsByClassName('tabsDesplegables').length > 0) {
	/*	var foo = getElementsByClassName('tabsDesplegables');
		for(var e = 0; e < foo.length; e++) {
			matrizTabs[e] = foo[e].id;
		}
		alert(matrizTabs.length);
		for(var i = 0; i < matrizTabs.length; i++) {
			matrizAlturasTabs[i] = document.getElementById(matrizTabs[i]).offsetHeight;
			document.getElementById(matrizTabs[i]).style.height = '0px';
			document.getElementById(matrizTabs[i]).style.display = 'none';
		}*/
		
		matrizTabs = getElementsByClassName('tabsDesplegables');
	
		for(var i = 0; i < matrizTabs.length; i++) {
			matrizAlturasTabs[i] = matrizTabs[i].offsetHeight;
			matrizTabs[i].style.height = '0px';
		//	matrizTabs[i].style.display = 'none';
		}
	}
}

function despliegaTab(ev) {
//	e.style.color = '#000';
	if (!ev) var ev = window.event;
	var relTarg = ev.relatedTarget || ev.fromElement;
	
	var elementos = document.getElementsByTagName('*');
	var posCabecera = 0;
	for(var i = 0; i < elementos.length; i++) {
		if(elementos[i] == ev)
			posCabecera = i;
	}
	// Encontramos la tabla (que de hecho es un div).
	var tabla;
	for(var a = posCabecera; a < elementos.length; a++) {
		if(elementos[a].className.indexOf('tabsDesplegables') != -1) {
			tabla = elementos[a];
			tabla.id = 'tabla' + a;
			break;
		}
	}
	// Encontramos su altura.
	var altura = 0;
	for(var e = 0; e < matrizTabs.length; e++) {
		if(matrizTabs[e] == tabla)
			altura = matrizAlturasTabs[e];
	}
//	alert(altura);

	// Escondemos las demas tablas.
	for(var o = 0; o < matrizTabs.length; o++) {
		if(matrizTabs[o] != tabla) {
			matrizTabs[o].id = 'foo' + o;
			jQuery('#' + matrizTabs[o].id).animate({ 
				height: '0px',
			}, 1000);
		}
	}
	
	if(tabla.style.height == '0px') {
		jQuery('#' + tabla.id).animate({ 
			height: altura + 'px',
			filter: "alpha(opacity=100)",
			opacity: "1"
		}, 1000);
	}
	else {
		jQuery('#' + tabla.id).animate({ 
			height: '0px',
		}, 1000);
	}
	
	// Modificamos los iconos de desplegar
	var matrizCabeceras = getElementsByClassName('tablaEncabezamiento1');
	for(var u = 0; u < matrizCabeceras.length; u++) {
		if(matrizCabeceras[u] != ev.children[0]) {
		//	alert(matrizCabeceras[u].children[0].children[0].children[1].children[0].src);
			matrizCabeceras[u].children[0].children[0].children[1].children[0].src = 'img/icono-desplegar-1.gif';
		}
		else {
			if(matrizCabeceras[u].children[0].children[0].children[1].children[0].src.indexOf('img/icono-desplegar-1.gif') != -1)
				matrizCabeceras[u].children[0].children[0].children[1].children[0].src = 'img/icono-replegar-1.gif';
			else
				matrizCabeceras[u].children[0].children[0].children[1].children[0].src = 'img/icono-desplegar-1.gif';
		}
	}
}

function resaltaEnlaceReservas() {
	var color1 = '#ff9900';
	
	if(location.href.indexOf('sabados') != -1)
		color1 = '#007016';
		
	if(location.href.indexOf('escuelas') != -1)
		color1 = '#008b7e';
		
	if(location.href.indexOf('vacaciones-escolares') != -1)
		color1 = '#c21900';
	
	jQuery('#enlaceReserva').animate( { color: color1 }, 1000)
    .animate( { color: 'white' }, 1000);
	
	jQuery('#wrapperReserva').animate( { backgroundColor: 'white' }, 1000)
    .animate( { backgroundColor: color1 }, 1000);
}


function redireccionaAlSite2010() {
	// Las URLs antes de la actualizacion del 2010 eran de la siguiente forma: "home_ct.html?pag=http://www.cuinapernens.com/info_ct.html&ninot=pera" mientras que después de esa
	// actualizacion son de la forma "cuina-per-nens-eulalia-fargas.php?idioma=es_ES&pag=info-general". Este script funciona en las antiguas "home_ct.html" y "home_es.html" 
	// (esta en javascript en lugar de en php porque las paginas originales eran html) y redirecciona a la pagina actualizada correspondiente dependiendo de la URL.
	
	var direccion = document.location.href;
	
	var idi = 'es_ES';
	if(direccion.indexOf('_ct') != -1)
		idi = 'ca_ES';
		
	var p = 'info';
	
	if(direccion.indexOf('info') != -1)
		p = 'info-general';
	if(direccion.indexOf('talleresAbiertos') != -1)
		p = 'sabados';
	if(direccion.indexOf('escuelas') != -1)
		p = 'escuelas';
	if(direccion.indexOf('tallerCasal') != -1)
		p = 'vacaciones-escolares';
	if(direccion.indexOf('ademas') != -1)
		p = 'ademas';
	if(direccion.indexOf('reserva') != -1)
		p = 'reserva';
		
	document.location.href = 'cuina-per-nens-eulalia-fargas.php?idioma=' + idi + '&pag=' + p;
}


// QueryString
// Call function by x = querystring("variable") returns variable=x

function getQuerystring(key, default_)
{
  if (default_==null) default_=false;
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

function incrustaFlash(ruta, ancho, alto, transp) {
	
//	document.write('<div class="flash">');
	document.write('<object id="flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + ancho + '" height="' + alto + '">');
      document.write('<param name="movie" value="' + ruta + '" />');
      document.write('<param name="quality" value="high" />');
	  if(transp) {
	  	document.write('<param name="wmode" value="transparent">');
		document.write('<embed swliveconnect="true" name="flash" src="' + ruta + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + ancho + '" height="' + alto + '" wmode="transparent"></embed>');
	  }
	  else
     	 document.write('<embed src="' + ruta + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + ancho + '" height="' + alto + '"></embed>');
    document.write('</object>');
//	document.write('</div>');
	
	
}


function traduce(idioma, ninotActivo) {
	
/*	if(idioma == '_ct')
		location.href = 'home_ct.html?pag=' + document.getElementById('iframeContenido').src.substring(0, document.getElementById('iframeContenido').src.lastIndexOf('_') ) + idioma + '.html' + '&ninot=' + ninotActivo;
		
	else 
		location.href = 'home_es.html?pag=' + document.getElementById('iframeContenido').src.substring(0, document.getElementById('iframeContenido').src.lastIndexOf('_') ) + idioma + '.html' + '&ninot=' + ninotActivo;*/
		
	if(idioma == '_ct')
		location.href = 'home_ct.html?pag=' + frames['iframeContenido'].location.href.substring(0, frames['iframeContenido'].location.href.lastIndexOf('_') ) + idioma + '.html' + '&ninot=' + ninotActivo;
		
	else 
		location.href = 'home_es.html?pag=' + frames['iframeContenido'].location.href.substring(0, frames['iframeContenido'].location.href.lastIndexOf('_') ) + idioma + '.html' + '&ninot=' + ninotActivo;
	
	
}

function revisaContenido() {
	
	if(getQuerystring('pag')) {
		frames['iframeContenido'].location.href = getQuerystring('pag');
	}
}




var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		jQuery(".modal-window").remove();
		jQuery(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		jQuery(this.parent).append(modal);

		jQuery(".modal-window").append("<a class=\"close-window\">Close / Fermer</a>");
		jQuery(".close-window").click(function(){modalWindow.close();});
		jQuery(".modal-overlay").click(function(){modalWindow.close();});
	}
};




var openMyModal = function(source)
{
	modalWindow.windowId = "myModal";
	modalWindow.width = 850;
	modalWindow.height = 500;
	modalWindow.content = "<iframe width='850' height='500' frameborder='0' scrolling='auto' allowtransparency='true' src='" + source + "'></iframe>";
	modalWindow.open();
};










