window.onload = function() {
	init();
}


/**
 * Variables Globales 
 */
 
var plantilla1;
var plantillaResultado;

/***
 * Se ejecuta al inicializar la página web
 */ 

function init() {
	// Carga Plantillas de las Fichas
	pedirAjax("plantillas/ficha1.html?" + Math.random(), llegoPlantilla, null, tipo.TEXTO, null);


	// Carga Módulos
	izq1Cargar();	
	izq2Cargar();
	paq1Cargar();
	paq2Cargar();
	destCargar();
	ofeCargar();
	aereoCargar();
	bannersCargar();
	verificarPopup();
	
	// Hace que el enter funcione en el cuadro de búsqueda
/*	$("busqueda").onkeyup = function(tecla) {
		alert(tecla)
		if(tecla.keyCode==13) {
			buscar();	
		};
	}*/
	
//	new Draggable('layerFicha',{});	
}

function llegoPlantilla(plantilla) {
	plantilla1 = plantilla;
	pedirAjax("plantillas/ficha_aereo.html?" + Math.random(), llegoPlantillaAerea, null, tipo.TEXTO, null);
	// Se fija si tiene que levantar una ficha en particular
	var query = document.location.search;
	if (query.substring(1, 5) == "paq=") {
		var id = document.location.search.substring(5);
		verPaquete(id);
	}
}

function llegoPlantillaAerea(plantilla) {
	plantillaAereo = plantilla;
	pedirAjax("plantillas/resultado.html?" + Math.random(), llegoPlantillaBusqueda, null, tipo.TEXTO, null);
}

function llegoPlantillaBusqueda(plantilla) {
	plantillaResultado = plantilla;
/*	if (document.location.search!="") {
		// Hay una búsqueda que viene desde otra página
		$("busqueda").value = unescape(document.location.search.substring(10));
		buscar();
	}*/
}


function verPaquete(id) {
	// Hace petición Ajax
	setVisible("layerLoading", true);
	pedirAjax("paquete.php?id=" + id, llegoPaquete, null, tipo.JSON, {funcionError: verError});		
}

function llegoPaquete(paq) {
	setVisible("overlay", true);
	setVisible("layerLoading", false);
	
	// Llena Parámetros
	var html;
	html = plantilla1;
	
	html = reemplazar(html,"{oferta}", paq.oferta);
	html = reemplazar(html,"{descripcion}", paq.descripcion);		
	html = reemplazar(html,"{precio}", paq.precio);		
	html = reemplazar(html,"{salidas}", paq.salidas);		
	html = reemplazar(html,"{vigencia}", "desde " + fechaMySQL(paq.desdeVigencia) + " hasta " +  fechaMySQL(paq.hasta));		
	
	// Imágenes
	var imgs = "<!-- imgs -- !>";
	var arrayImgs;
	if (paq.imagenFicha!=null) {
		arrayImgs = paq.imagenFicha.split(";");		
	} else  {
		arrayImgs = new Array();
		arrayImgs.push(paq.imagenHome);
		arrayImgs.push(paq.imagen);		
	}
	for (var i=0; i<arrayImgs.length; i++) {
		imgs += "<img src='images/paq/" + arrayImgs[i] + "' width='110' height='81' hspace='5' vspace='5' />";
	}	
	// Lo hace 4 veces, una por cada pestaña
	for (var i=0; i<8; i++) {
		html = reemplazar(html,"{nombre}", paq.nombre);
		html = reemplazar(html,"{id}", paq.id);		
		html = reemplazar(html,"{imagenes}", imgs);
	}		
	
	// Hoteles
	var hoteles = "";
	for (var i=0; i<paq.hoteles.length; i++) {
		hoteles += "<table width='100%'>";
		hoteles += "<tr><td width='130' align='left' valign='top'>";
		// Imagenes del Hotel
		var arrayImgs;
		var imgsH = "";
		if (paq.hoteles[i].imagenes!=null) {
			arrayImgs = paq.hoteles[i].imagenes.split(";");		
			for (var j=0; j<arrayImgs.length; j++) {
				imgsH += "<img src='images/hoteles/" + arrayImgs[j] + "' width='110' height='81' hspace='5' vspace='5' />";
			}	
			hoteles += imgsH;			
		}		
		hoteles += "</td><td  align='left'  valign='top'>";
		hoteles += "<font size=3><strong>" + paq.hoteles[i].nombre + " ";
		for (var k = 0; k<paq.hoteles[i].estrellas; k++)  // Estrellas del Hotel
			hoteles += "*";
		hoteles += "</strong></font><br />";
		hoteles += "Categoría: " + paq.hoteles[i].categoria + " <br /> <br />";
		hoteles += paq.hoteles[i].descripcion + "<br /><br />";
		hoteles += "<b>Habitaciones</b><br />" + paq.hoteles[i].habitaciones + "<br /><br />";
		hoteles += "<b>Comodidades</b><br />" + paq.hoteles[i].comodidades + "<br /><br />";
		hoteles += "</td></tr></table>"
	}	
	html = reemplazar(html,"{hoteles}", hoteles);	

	// Incluye
	var incluye = "";
	for (var i=0; i<paq.parametros.length; i++) {
		if (paq.parametros[i].tipoId == 1) {
			incluye += paq.parametros[i].texto + "<br />";
		}
	}
	incluye += paq.incluye;
	html = reemplazar(html,"{incluidos}", incluye);		
	
	// No Incluye
	var noincluye = "";
	for (var i=0; i<paq.parametros.length; i++) {
		if (paq.parametros[i].tipoId == 2) {
			noincluye += paq.parametros[i].texto + "<br />";
		}
	}
	noincluye += paq.noIncluye;
	html = reemplazar(html,"{noincluidos}", noincluye);		

	// Observaciones
	var obs = "";
	for (var i=0; i<paq.parametros.length; i++) {
		if (paq.parametros[i].tipoId == 3) {
			if (i%2==0) {
				obs += "<div style='width:100%; padding: 2px;'>";
			} else {
				obs += "<div style='width:100%; background: #fff; padding: 2px;'>";				
			}
			obs += paq.parametros[i].texto + "</div>";
		}
	}
	obs += paq.noIncluye;
	html = reemplazar(html,"{observaciones}", obs);		

	$("layerFicha").style.visibility = "visible";
	$("layerFicha").innerHTML  = html;
	
	$("ficha_tab1").onclick = verFichaTab1;
	$("ficha_tab2").onclick = verFichaTab2;
	$("ficha_tab3").onclick = verFichaTab3;
	$("ficha_tab4").onclick = verFichaTab4;		
	
	// Si hay hoteles, habilita el segundo tab 
	setVisible("ficha_tab2", (paq.hoteles.length > 0))
	if (paq.hoteles.length == 0) {
		$("ficha_tab2").style.display = "none";
	}
		
	new Effect.Appear("layerFicha", {duration: 0.25});
	scroll(0,0);		
	
}



function verFichaTab1() {
	fichaApagarTabs();
	$("ficha_tab1").src = "images/ficha_tab1_on.gif"	;	
	new Effect.Appear("ficha_tabla1");
	setVisible("ficha_tabla1", true);		
}

function verFichaTab2() {
	fichaApagarTabs();
	$("ficha_tab2").src = "images/ficha_tab2_on.gif"	;	
	new Effect.Appear("ficha_tabla2");
	setVisible("ficha_tabla2", true);		
}

function verFichaTab3() {
	fichaApagarTabs();
	$("ficha_tab3").src = "images/ficha_tab3_on.gif"	;		
	new Effect.Appear("ficha_tabla3");
	setVisible("ficha_tabla3", true);		
}

function verFichaTab4() {
	fichaApagarTabs();
	$("ficha_tab4").src = "images/ficha_tab4_on.gif"	;			
	new Effect.Appear("ficha_tabla4");
	setVisible("ficha_tabla4", true);		
}

function fichaApagarTabs() {
	setVisible("ficha_tabla1", false);
	setVisible("ficha_tabla2", false);
	setVisible("ficha_tabla3", false);		
	setVisible("ficha_tabla4", false);		
	
	$("ficha_tab1").src = "images/ficha_tab1_off.gif"	;
	$("ficha_tab2").src = "images/ficha_tab2_off.gif"	;
	$("ficha_tab3").src = "images/ficha_tab3_off.gif"	;		
	$("ficha_tab4").src = "images/ficha_tab4_off.gif"	;	
}



function verAereo(id) {
	// Hace petición Ajax
	setVisible("layerLoading", true);
	pedirAjax("aereo.php?id=" + id, llegoAereo, null, tipo.JSON, {funcionError: verError});		
}

function llegoAereo(aer) {
	setVisible("overlay", true);
	setVisible("layerLoading", false);
	
	// Llena Parámetros
	var html;
	html = plantillaAereo;
	
	html = reemplazar(html,"{precio}", aer.precio);
	html = reemplazar(html,"{compania}", aer.compania);		
	html = reemplazar(html,"{observaciones}", aer.observaciones);		
	html = reemplazar(html,"{salidas}", aer.salidas);		
	html = reemplazar(html,"{estadia}", aer.estadia);		
	html = reemplazar(html,"{tarifa}", aer.tarifa);		
	html = reemplazar(html,"{penalidades}", aer.estadia);		
	html = reemplazar(html,"{restricciones}", aer.restricciones);		
	html = reemplazar(html,"{vigencia}", "desde " + fechaMySQL(aer.desde) + " hasta " +  fechaMySQL(aer.hasta));		
	
	// Lo hace 4 veces, una por cada pestaña
	for (var i=0; i<8; i++) {
		html = reemplazar(html,"{destino}", aer.destino);
		html = reemplazar(html,"{id}", aer.id);		
	}		
	

	// Incluye
	html = reemplazar(html,"{incluidos}", aer.incluye);		
	
	// No Incluye
	html = reemplazar(html,"{noincluidos}", aer.noIncluye);		


	$("layerFicha").style.visibility = "visible";
	$("layerFicha").innerHTML  = html;
	
	$("aereo_tab1").onclick = verAereoTab1;
	$("aereo_tab2").onclick = verAereoTab2;
	$("aereo_tab3").onclick = verAereoTab3;
			
	new Effect.Appear("layerFicha", {duration: 0.25});
	scroll(0,0);		
	
}




function verAereoTab1() {
	aereoApagarTabs();
	$("aereo_tab1").src = "images/aereo_tab1_on.gif"	;	
	new Effect.Appear("aereo_tabla1");
	setVisible("aereo_tabla1", true);		
}

function verAereoTab2() {
	aereoApagarTabs();
	$("aereo_tab2").src = "images/aereo_tab2_on.gif"	;	
	new Effect.Appear("aereo_tabla2");
	setVisible("aereo_tabla2", true);		
}

function verAereoTab3() {
	aereoApagarTabs();
	$("aereo_tab3").src = "images/aereo_tab3_on.gif"	;		
	new Effect.Appear("aereo_tabla3");
	setVisible("aereo_tabla3", true);		
}


function aereoApagarTabs() {
	setVisible("aereo_tabla1", false);
	setVisible("aereo_tabla2", false);
	setVisible("aereo_tabla3", false);		
	
	$("aereo_tab1").src = "images/aereo_tab1_off.gif"	;
	$("aereo_tab2").src = "images/aereo_tab2_off.gif"	;
	$("aereo_tab3").src = "images/aereo_tab3_off.gif"	;		
}


function verError() {
	setVisible("overlay", true);
	setVisible("layerError", true);
}

function cerrarError() {
	setVisible("overlay", false);
	setVisible("layerError", false);	
	setVisible("layerLoading", false);	
}

function reemplazar(html, etiqueta, valor) {
	if (html != undefined)
		if (valor!=null) {
			return html.replace(etiqueta, valor);
		} else {
			return html.replace(etiqueta, "");
		}
}

function cerrarFicha() {
	if (resultadoBusqueda == null) {
		new Effect.Fade("layerFicha", {duration: 0.25});
		setVisible("overlay", false);			
	} else {
		// Vuelve al resultado 
		mostrarResultado();
	}
}

/***
 * 
 *  MODULO IZQUIERDA 1
 * 
 */

var izq1Desde = 0; // desde cuál está mostrando

/***
 * Llena los valores del módulo Izquierda 1
 */
var izq1HtmlAbierto;

function izq1Cargar() {
	izq1MostrarDestinos();
	setVisible("izq1_mas", (destinos.length > 6));			
	// Guarda el HTML de los links
	izq1HtmlAbierto = $("izq1_paquetes").innerHTML;
	
}

function izq1Mas() {
	new Effect.Fade("izq1_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: izq1MasFinish});
}

function izq1MasFinish() {
	// Limpia los textos actuales	
	for (var i=0; i<6; i++) $("izq1_opcion" + (i+1)).innerHTML = "";
	if (izq1Desde+6 > destinos.length) { // No hay más
		izq1Desde = 0;
	} else {
		izq1Desde += 6;
	}
	izq1MostrarDestinos();	
	new Effect.Fade("izq1_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	izq1Abrir(1); // Pone por defecto abierto el primero
	
}

// Abre los links del destino seleccionado
function izq1Abrir(indice) {
	for (var i=1; i<7; i++) {
		// Cierra los anteriores
		$("izq1_celda" + i).innerHTML = "";
	}
	$("izq1_celda" + indice).innerHTML = izq1HtmlAbierto;
	$("izq1_celda" + indice).bgColor = "#EEF4F0";
	izq1MostrarPaq(indice+izq1Desde-1);	
}

function izq1MostrarDestinos() {
	for (var i=0; i<min(6, destinos.length-izq1Desde); i++) {
		$("izq1_opcion" + (i+1)).innerHTML = "<a href='javascript:izq1Abrir(" + (i+1)+ ")'>" + destinos[i+izq1Desde].nombre + "</a>" ;   //generarLinkPaq(izq1[i+izq1Desde]);
	}
	izq1MostrarPaq(0);
}

function izq1MostrarPaq(i) {
	// Pone los links a los 4 paquetes que se ven abiertos
	//alert(destinos[i].paquetes.length);
	if (destinos[i].paquetes.length>=1) 
		$("izq1_paq1").innerHTML = generarLinkPaqPrecio(destinos[i].paquetes[0]);
	  else
	    $("izq1_paq1").innerHTML = "&nbsp;";
	
	if (destinos[i].paquetes.length>=2) 
		$("izq1_paq2").innerHTML = generarLinkPaqPrecio(destinos[i].paquetes[1]);
	  else
	    $("izq1_paq2").innerHTML = "&nbsp;";
	
	if (destinos[i].paquetes.length>=3) 
		$("izq1_paq3").innerHTML = generarLinkPaqPrecio(destinos[i].paquetes[2]);
	  else
	    $("izq1_paq3").innerHTML = "&nbsp;";
	
	if (destinos[i].paquetes.length>=4) 
		$("izq1_paq4").innerHTML = generarLinkPaqPrecio(destinos[i].paquetes[3]);		
	  else
	    $("izq1_paq4").innerHTML = "&nbsp;";
}

/***
 * 
 *  FIN MODULO IZQUIERDA 1
 * 
 */



/***
 * 
 *  MODULO IZQUIERDA 2
 * 
 */

var izq2Desde = 0; // desde cuál está mostrando

/***
 * Llena los valores del módulo Izquierda 1
 */
function izq2Cargar() {
	izq2MostrarPaq();
	setVisible("izq2_mas", (izq2.length > 4));			
}

function izq2Mas() {
	new Effect.Fade("izq2_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: izq2MasFinish});
}

function izq2MasFinish() {
	// Limpia los textos actuales	
	for (var i=0; i<4; i++) {
		$("izq2_titulo" + (i+1)).innerHTML = "";
		$("izq2_oferta" + (i+1)).innerHTML = "";		
		$("izq2_precio" + (i+1)).innerHTML = "";		
	}
	if (izq2Desde+4 > izq2.length) { // No hay más
		izq2Desde = 0;
	} else {
		izq2Desde += 4;
	}
	izq2MostrarPaq();	
	new Effect.Fade("izq2_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function izq2MostrarPaq() {

	for (var i=0; i<4; i++) {
		if (i+izq2Desde < izq2.length) {
			setVisible("izq2_flecha" + (i+1), true);
			$("izq2_titulo" + (i+1)).innerHTML = generarLinkPaq(izq2[i+izq2Desde]);
			$("izq2_oferta" + (i+1)).innerHTML = izq2[i+izq2Desde].oferta;			
			$("izq2_precio" + (i+1)).innerHTML = izq2[i+izq2Desde].precio;						
			$("izq2_precio" + (i+1)).style.paqid = izq2[i+izq2Desde].id;
			$("izq2_flecha" + (i+1)).onclick = function() { verPaquete(izq2[izq2Desde+ultimoCaracter(this.id)-1].id); };						

		} else {
			setVisible("izq2_flecha" + (i+1), false);
		}
	}
}

/***
 * 
 *  FIN MODULO IZQUIERDA 2
 * 
 */


/***
 * 
 *  MODULO PAQUETES1
 * 
 */

var paq1Desde = 0; // desde cuál está mostrando
var paq1TabActual = 1;

/***
 * Llena los valores del módulo Paq1
 */
function paq1Cargar() {
	paq1MostrarPaq();
	setVisible("paq1_mas", (paq1_tab1.length > 4));			
	setVisible("paq1_tab2", (paq1_tabs > 1));			
	setVisible("paq1_tab3", (paq1_tabs > 2));		
	
	$("paq1_tab1").innerHTML = "&nbsp;" + paq1_titulo1;
	$("paq1_tab2").innerHTML = "&nbsp;" + paq1_titulo2;
	$("paq1_tab3").innerHTML = "&nbsp;" + paq1_titulo3;
}	

function paq1Tab(tab) {
	paq1TabActual = tab;
	new Effect.Fade("paq1_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: paq1TabFinish});
	
}

function paq1TabFinish() {
	if (paq1TabActual==1) {
		setVisible("paq1_mas", (paq1_tab1.length > 4));
		$("paq1_tab1").className = "tab_activo";
		$("paq1_tab2").className = "tab_pasivo";
		$("paq1_tab3").className = "tab_pasivo";
	}
	if (paq1TabActual==2) {
		setVisible("paq1_mas", (paq1_tab2.length > 4));
		$("paq1_tab1").className = "tab_pasivo";
		$("paq1_tab2").className = "tab_activo";
		$("paq1_tab3").className = "tab_pasivo";	
	}
	if (paq1TabActual==3) {
		setVisible("paq1_mas", (paq1_tab3.length > 4));
		$("paq1_tab1").className = "tab_pasivo";
		$("paq1_tab2").className = "tab_pasivo";
		$("paq1_tab3").className = "tab_activo";	
	}		
	paq1Desde = 0;
	paq1MostrarPaq();	
	new Effect.Fade("paq1_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function paq1Mas() {
	new Effect.Fade("paq1_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: paq1MasFinish});
}

function paq1MasFinish() {
	// Limpia los textos actuales	
	paq1 = paq1_PaqueteActual();
	for (var i=0; i<4; i++) {
		$("paq1_titulo" + (i+1)).innerHTML = "";
		$("paq1_oferta" + (i+1)).innerHTML = "";		
		$("paq1_precio" + (i+1)).innerHTML = "";		
	}
	if (paq1Desde+4 >= paq1.length) { // No hay más
		paq1Desde = 0;
	} else {
		paq1Desde += 4;
	}
	paq1MostrarPaq();	
	new Effect.Fade("paq1_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function paq1MostrarPaq() {

	var paq1;
	paq1 = paq1_PaqueteActual();	
	
	for (var i=0; i<4; i++) {
		if (i+paq1Desde < paq1.length) {
			setVisible("paq1_flecha" + (i+1), true);
			$("paq1_titulo" + (i+1)).innerHTML = generarLinkPaq(paq1[i+paq1Desde]);
			$("paq1_oferta" + (i+1)).innerHTML = paq1[i+paq1Desde].oferta;			
			$("paq1_precio" + (i+1)).innerHTML = paq1[i+paq1Desde].precio;			
			var imagen;
			if (paq1[i+paq1Desde].imagen=="") 
				imagen = "images/top_ribbon.jpg";
			else
				imagen = "images/paq/" + paq1[i+paq1Desde].imagen;
			$("paq1_imagen" + (i+1)).src = imagen;			
			setVisible("paq1_flecha" + (i+1), true);
			setVisible("paq1_imagen" + (i+1), true);
			if (paq1TabActual==1) {
				$("paq1_imagen" + (i+1)).onclick = function() { verPaquete(paq1_tab1[paq1Desde+ultimoCaracter(this.id)-1].id); };			
				$("paq1_flecha" + (i+1)).onclick = function() { verPaquete(paq1_tab1[paq1Desde+ultimoCaracter(this.id)-1].id); };			
			}
			if (paq1TabActual==2) {
				$("paq1_imagen" + (i+1)).onclick = function() { verPaquete(paq1_tab2[paq1Desde+ultimoCaracter(this.id)-1].id); };			
				$("paq1_flecha" + (i+1)).onclick = function() { verPaquete(paq1_tab2[paq1Desde+ultimoCaracter(this.id)-1].id); };			
			}
			if (paq1TabActual==3) {
				$("paq1_imagen" + (i+1)).onclick = function() { verPaquete(paq1_tab3[paq1Desde+ultimoCaracter(this.id)-1].id); };			
				$("paq1_flecha" + (i+1)).onclick = function() { verPaquete(paq1_tab3[paq1Desde+ultimoCaracter(this.id)-1].id); };			
			}
			
		} else {
			setVisible("paq1_flecha" + (i+1), false);
			setVisible("paq1_imagen" + (i+1), false);
			$("paq1_titulo" + (i+1)).innerHTML = "";
			$("paq1_oferta" + (i+1)).innerHTML = "";		
			$("paq1_precio" + (i+1)).innerHTML = "";				
		}
	}
}

function paq1_PaqueteActual() {
	if (paq1TabActual==1) return paq1_tab1;
	if (paq1TabActual==2) return paq1_tab2;
	if (paq1TabActual==3) return paq1_tab3;		
}

/***
 * 
 *  FIN MODULO PAQUETES1
 * 
 */



/***
 * 
 *  MODULO PAQUETES2
 * 
 */

var paq2Desde = 0; // desde cuál está mostrando
var paq2TabActual = 1;

/***
 * Llena los valores del módulo paq2
 */
function paq2Cargar() {
	paq2MostrarPaq();
	
	setVisible("paq2_mas", (paq2_tab1.length > 4));			
	setVisible("paq2_tab2", (paq2_tabs > 1));			
	setVisible("paq2_tab3", (paq2_tabs > 2));		
	
	$("paq2_tab1").innerHTML = "&nbsp;" + paq2_titulo1;
	$("paq2_tab2").innerHTML = "&nbsp;" + paq2_titulo2;
	$("paq2_tab3").innerHTML = "&nbsp;" + paq2_titulo3;	
}	

function paq2Tab(tab) {
	paq2TabActual = tab;
	new Effect.Fade("paq2_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: paq2TabFinish});
	
}

function paq2TabFinish() {
	if (paq2TabActual==1) {
		setVisible("paq2_mas", (paq2_tab1.length > 4));
		$("paq2_tab1").className = "tab_activo";
		$("paq2_tab2").className = "tab_pasivo";
		$("paq2_tab3").className = "tab_pasivo";
	}
	if (paq2TabActual==2) {
		setVisible("paq2_mas", (paq2_tab2.length > 4));
		$("paq2_tab1").className = "tab_pasivo";
		$("paq2_tab2").className = "tab_activo";
		$("paq2_tab3").className = "tab_pasivo";	
	}
	if (paq2TabActual==3) {
		setVisible("paq2_mas", (paq2_tab3.length > 4));
		$("paq2_tab1").className = "tab_pasivo";
		$("paq2_tab2").className = "tab_pasivo";
		$("paq2_tab3").className = "tab_activo";	
	}	
	paq2Desde = 0;
	paq2MostrarPaq();	
	new Effect.Fade("paq2_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function paq2Mas() {
	new Effect.Fade("paq2_contenedor", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: paq2MasFinish});
}

function paq2MasFinish() {
	// Limpia los textos actuales	
	paq2 = paq2_PaqueteActual();
	for (var i=0; i<4; i++) {
		$("paq2_titulo" + (i+1)).innerHTML = "";
		$("paq2_precio" + (i+1)).innerHTML = "";		
	}
	if (paq2Desde+4 >= paq2.length) { // No hay más
		paq2Desde = 0;
	} else {
		paq2Desde += 4;
	}
	paq2MostrarPaq();	
	new Effect.Fade("paq2_contenedor", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function paq2MostrarPaq() {

	var paq2;
	paq2 = paq2_PaqueteActual();	
	
	for (var i=0; i<4; i++) {
		if (i+paq2Desde < paq2.length) {
			$("paq2_titulo" + (i+1)).innerHTML = generarLinkPaq(paq2[i+paq2Desde]);
			$("paq2_precio" + (i+1)).innerHTML = paq2[i+paq2Desde].precio;			
			var imagen;
			if (paq2[i+paq2Desde].imagen=="") 
				imagen = "images/top_ribbon.jpg";
			else
				imagen = "images/paq/" + paq2[i+paq2Desde].imagen;
			$("paq2_imagen" + (i+1)).src = imagen;			
			setVisible("paq2_imagen" + (i+1), true);
			if (paq2TabActual==1) {
				$("paq2_imagen" + (i+1)).onclick = function() { verPaquete(paq2_tab1[paq2Desde+ultimoCaracter(this.id)-1].id); };			
			}
			if (paq2TabActual==2) {
				$("paq2_imagen" + (i+1)).onclick = function() { verPaquete(paq2_tab2[paq2Desde+ultimoCaracter(this.id)-1].id); };			
			}
			if (paq2TabActual==3) {
				$("paq2_imagen" + (i+1)).onclick = function() { verPaquete(paq2_tab3[paq2Desde+ultimoCaracter(this.id)-1].id); };			
			}
		} else {
			setVisible("paq2_imagen" + (i+1), false);
			$("paq2_titulo" + (i+1)).innerHTML = "";
			$("paq2_precio" + (i+1)).innerHTML = "";							
		}
	}
}

function paq2_PaqueteActual() {
	if (paq2TabActual==1) return paq2_tab1;
	if (paq2TabActual==2) return paq2_tab2;
	if (paq2TabActual==3) return paq2_tab3;		
}

/***
 * 
 *  FIN MODULO PAQUETES2
 * 
 */

/***
 * 
 *  MODULO DESTACADOS
 * 
 */


/***
 * Llena los valores del módulo Destacados
 */
function destCargar() {
	var filas = "<table background='white' width='97%' border='0' align='center' cellpadding='2' cellspacing='0'><tbody>";
	for (var i=0; i<dest.length; i++) {
		var temp = destacadoPlantilla;
		temp = temp.replace("{titulo}", "<a href=\"javascript:verPaquete('" + dest[i].id  + "')\">" + 
				dest[i].nombre + "</a>");
		temp = temp.replace("{precio}", dest[i].precio);
		temp = temp.replace("{imagen}", "onclick=\"verPaquete('" + dest[i].id  + "')\" ");
		if (i%2==0) {
			temp = temp.replace("{fondo}", "#FFFFFF");
		} else {
			temp = temp.replace("{fondo}", "#EEF2FF");			
		}		
		filas += temp;
	}
	filas += "</tbody></table>"
	$("destacados_tabla").innerHTML = filas;
}

/***
 * 
 *  FIN MODULO DESTACADOS
 * 
 */


/***
 * 
 *  MODULO AEREOS
 * 
 */


/***
 * Llena los valores del módulo Aereos
 */
 
var aereoDesde = 0;

function aereoCargar() {
	aereoMostrar();
	setVisible($("aereo_mas"), aereos.length>10);
	$("aereo_mas").onclick = aereoMas;
}


function aereoMas() {
	new Effect.Fade("destacados_aereo", {duration: 0.1, from: 1.0, to: 0.1, afterFinish: aereoMasFinish});
}

function aereoMasFinish() {
	// Limpia los textos actuales	
	if (aereoDesde+10 > aereos.length) { // No hay más
		aereoDesde = 0;
	} else {
		aereoDesde += 10;
	}
	aereoMostrar();	
	new Effect.Fade("destacados_aereo", {duration: 0.1, from: 0.1, to: 1.0});
	
}

function aereoMostrar() {
	var filas = "<table background='white' width='97%' border='0' align='center' cellpadding='2' cellspacing='0'><tbody>";
	for (var i=0; i<min(aereos.length+aereoDesde,10); i++) {
		var temp = aereoPlantilla;
		if (aereos[i+aereoDesde]!=undefined) {
			temp = temp.replace("{destino}", "<a href=\"javascript:verAereo('" + aereos[i+aereoDesde].id  + "')\">" + 
					aereos[i+aereoDesde].destino + "</a>");
			temp = temp.replace("{tarifa}", aereos[i+aereoDesde].tarifa);
			temp = temp.replace("{imagen}", "onclick=\"verAereo('" + aereos[i+aereoDesde].id  + "')\" ");
		} else {
			temp = temp.replace("{destino}", "&nbsp;");
			temp = temp.replace("{compania}", " ");
			temp = temp.replace("{tarifa}", " ");
			temp = temp.replace("{imagen}", "style='visibility:hidden'");
		}
		if (i%2==0) {
			temp = temp.replace("{fondo}", "#FFFFFF");
		} else {
			temp = temp.replace("{fondo}", "#EEF2FF");			
		}
		filas += temp;
	}
	filas += "</tbody></table>"
	$("destacados_aereo").innerHTML = filas;		
}

/***
 * 
 *  FIN MODULO AEREOS
 * 
 */



/***
 * 
 *  MODULO OFERTAS 1
 * 
 */


/***
 * Llena los valores del módulo Izquierda 1
 */
function ofeCargar() {

	for (var i=0; i<3; i++) {
		if (i < ofe.length) {
			setVisible("ofe_imagen" + (i+1), true);
			$("ofe_titulo" + (i+1)).innerHTML = generarLinkPaq(ofe[i]);
			$("ofe_oferta" + (i+1)).innerHTML = ofe[i].oferta;			
			$("ofe_precio" + (i+1)).innerHTML = ofe[i].precio;			
			$("ofe_imagen" + (i+1)).src = "images/paq/" + ofe[i].imagen;
			$("ofe_imagen" + (i+1)).onclick = function() { verPaquete(ofe[ultimoCaracter(this.id)-1].id); };			
			
		} else {
			setVisible("ofe_imagen" + (i+1), false);
		}
	}
}

/***
 * 
 *  FIN MODULO IZQUIERDA 2
 * 
 */


function bannersCargar() {
	if ($("banner_paq1")!=undefined) {
		if (typeof(banner_paq1)=="string") {
			$("banner_paq1").onclick = function() { verPaquete(banner_paq1) };
		} else {
			if (banner_paq1.tipo==1) {
				// Banner
				$("banner_paq1").onclick = function() { verPaquete(banner_paq1.banner) };				
			} else {
				$("banner_paq1").onclick = function() { verGrupoPaq(banner_paq1.grupo, banner_paq1.plantilla) };				
			}
		}
		
	}
	if ($("banner_destino1")!=undefined) {
		if (typeof(banner_destino1)=="string") {
			$("banner_destino1").onclick = function() { verPaquete(banner_destino1) };
		} else {
			if (banner_destino1.tipo==1) {
				// Banner
				$("banner_destino1").onclick = function() { verPaquete(banner_destino1.banner) };				
			} else {
				$("banner_destino1").onclick = function() { verGrupoPaq(banner_destino1.grupo, banner_destino1.plantilla) };				
			}
		}
		
	}	
	if ($("banner_destino2")!=undefined) {
		if (typeof(banner_destin21)=="string") {
			$("banner_destino2").onclick = function() { verPaquete(banner_destino2) };
		} else {
			if (banner_destino2.tipo==1) {
				// Banner
				$("banner_destino2").onclick = function() { verPaquete(banner_destino2.banner) };				
			} else {
				$("banner_destino2").onclick = function() { verGrupoPaq(banner_destino2.grupo, banner_destino2.plantilla) };				
			}
		}
		
	}		
/*	
	if ($("banner_destino1")!=undefined)
		$("banner_destino1").onclick = function() { verPaquete(banner_destino1) };
	if ($("banner_destino2")!=undefined)
		$("banner_destino2").onclick = function() { verPaquete(banner_destino2) };
*/		
}


var plantillaGrupo = false;
var grupoPaquetes;
var grupoPaquetesId;

function verGrupoPaq(id, plantilla) {
	grupoPaquetesId = id;
	// Pide plantilla
	pedirAjax("plantillas/" + plantilla, recibirPlantillaGrupo, null, tipo.TEXTO);
	
	setVisible("overlay", false);
	setVisible("layerLoading", true);	
}

function recibirPlantillaGrupo(plantilla) {
	// Pide datos
	pedirAjax("grupopaq.php?id=" + grupoPaquetesId, recibirGrupo, null, tipo.JSON, {funcionError: verError});		

	plantillaGrupo = plantilla;
}

function recibirGrupo(grupo) {
	grupoPaquetes = grupo;
	// Ya está todo
	mostrarGrupoPaquetes();		
}

function mostrarGrupoPaquetes() {
	setVisible("overlay", true);
	setVisible("layerLoading", false);	
	
	var html;
	html = plantillaGrupo;
    var resultadoFila = "<tr><td height='25' bgcolor='{fondo}'><div align='left'><strong>{destino}</strong> - {oferta} <strong>{precio}</strong> </div></td><td width='16' height='25'><div align='right'><img src='images/arrow_small.jpg' width='13' height='13' {imagen} style='cursor:pointer' /></div></td></tr>";	
	
	html = reemplazar(html,"{nombre}", grupoPaquetes.nombre); 
	
	// Reemplaza los destacados
	
	for (var i=1; i<4; i++ ) {
		html = reemplazar(html, "{paq" + i + "_nombre}", grupoPaquetes["destacado" + i].nombre);
		html = reemplazar(html, "{paq" + i + "_oferta}", grupoPaquetes["destacado" + i].copete);
		html = reemplazar(html, "{paq" + i + "_precio}", grupoPaquetes["destacado" + i].precio);
	}
	
	// Reemplaza todos los resultados
	var tabla = "<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">";
	for (var i=0; i<grupoPaquetes.paquetes.length; i++) {
		var fila = resultadoFila;
		fila = reemplazar(fila, "{destino}", grupoPaquetes.paquetes[i].nombre);
		fila = reemplazar(fila, "{oferta}", grupoPaquetes.paquetes[i].copete);
		fila = reemplazar(fila, "{precio}", grupoPaquetes.paquetes[i].precio);
		fila = reemplazar(fila, "{imagen}", "onclick=\"verPaquete('" + grupoPaquetes.paquetes[i].id  + "')\" ");
		
		if (i%2==1) {
			fila = reemplazar(fila, "{fondo}", "#EEF2FF");			
		} else {
			fila = reemplazar(fila, "{fondo}", "#FFFFFF");						
		}
		tabla += fila;
	}
	tabla += "</tabla>";
	html = reemplazar(html, "{resultadoPaquetes}", tabla);	
	
	$("layerFicha").style.visibility = "visible";
	$("layerFicha").innerHTML  = html;
	
	// Define los links e imagenes de los destacados que ahora están en el DOM
	$("grupoImg1").src = "images/paq/" + grupoPaquetes.destacado1.imagen;
	$("grupoImg2").src = "images/paq/" + grupoPaquetes.destacado2.imagen;
	$("grupoImg3").src = "images/paq/" + grupoPaquetes.destacado3.imagen;
	$("grupoImg1").onclick = function() { verPaquete(grupoPaquetes.destacado1.id) };
	$("grupoImg2").onclick = function() { verPaquete(grupoPaquetes.destacado2.id) };
	$("grupoImg3").onclick = function() { verPaquete(grupoPaquetes.destacado3.id) };
	$("grupoLink1").onclick = function() { verPaquete(grupoPaquetes.destacado1.id) };
	$("grupoLink2").onclick = function() { verPaquete(grupoPaquetes.destacado2.id) };
	$("grupoLink3").onclick = function() { verPaquete(grupoPaquetes.destacado3.id) };
	

	new Effect.Appear("layerFicha", {duration: 0.25});
	
	
	scroll(0,0);		
}


var keywordBusqueda = "";

function buscar() {
	var keyword = $("busqueda").value;
	keywordBusqueda = keyword;
	if ((keyword=="en elviajeperfecto.com") || (keyword=="")) {
		alert("Debe especificar un destino de búsqueda");
	} else {
		// Hace petición Ajax
		setVisible("layerLoading", true);
		pedirAjax("buscar.php?key=" + keyword, llegoBuscar, null, tipo.JSON, {funcionError: verError});		
	}
}

var resultadoBusqueda = null;

function llegoBuscar(resultado) {
	setVisible("overlay", true);
	setVisible("layerLoading", false);
	
	resultadoBusqueda = resultado;
	mostrarResultado();
	
		
		
}

function mostrarResultado() {
	// Llena Parámetros
	var html;
	html = plantillaResultado;
    var resultadoFila = "<tr><td height='25' bgcolor='{fondo}'><div align='left'><strong>{destino}</strong> - {oferta} <strong>{precio}</strong> </div></td><td width='16' height='25'><div align='right'><img src='images/arrow_small.jpg' width='13' height='13' {imagen} style='cursor:pointer' /></div></td></tr>";	
	
	html = reemplazar(html,"{key}", keywordBusqueda);
	html = reemplazar(html,"{key}", keywordBusqueda);
	html = reemplazar(html,"{key}", keywordBusqueda);
	html = reemplazar(html,"{cantPaq}", resultadoBusqueda.paquetes.length);		
	html = reemplazar(html,"{cantAereo}", resultadoBusqueda.aereos.length);		
	html = reemplazar(html,"{cantNotas}", 0);			

	// Pone Aereos
	if (resultadoBusqueda.aereos.length > 0) {
		var tabla = "<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">";
		for (var i=0; i<resultadoBusqueda.aereos.length; i++) {
			var fila = resultadoFila;
			fila = reemplazar(fila, "{destino}", resultadoBusqueda.aereos[i].destino);
			fila = reemplazar(fila, "{oferta}", resultadoBusqueda.aereos[i].compania);
			fila = reemplazar(fila, "{precio}", resultadoBusqueda.aereos[i].tarifa);
			fila = reemplazar(fila, "{imagen}", "onclick=\"verAereo('" + resultadoBusqueda.aereos[i].id  + "')\" ");
			
		if (i%2==1) {
				fila = reemplazar(fila, "{fondo}", "#EEF2FF");			
			}  else {
				fila = reemplazar(fila, "{fondo}", "#FFFFFF");						
			}
			tabla += fila;
		}
		tabla += "</tabla>";
		html = reemplazar(html, "{resultadoAereos}", tabla);
	}

	if (resultadoBusqueda.paquetes.length > 0) {	
		// Pone Paquetes
		var tabla = "<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"0\">";
		for (var i=0; i<resultadoBusqueda.paquetes.length; i++) {
			var fila = resultadoFila;
			fila = reemplazar(fila, "{destino}", resultadoBusqueda.paquetes[i].nombre);
			fila = reemplazar(fila, "{oferta}", resultadoBusqueda.paquetes[i].copete);
			fila = reemplazar(fila, "{precio}", resultadoBusqueda.paquetes[i].precio);
			fila = reemplazar(fila, "{imagen}", "onclick=\"verPaquete('" + resultadoBusqueda.paquetes[i].id  + "')\" ");
			
			if (i%2==1) {
				fila = reemplazar(fila, "{fondo}", "#EEF2FF");			
			} else {
				fila = reemplazar(fila, "{fondo}", "#FFFFFF");						
			}
			tabla += fila;
		}
		tabla += "</tabla>";
		html = reemplazar(html, "{resultadoPaquetes}", tabla);
	}		
	
	// Pone Notas
	html = reemplazar(html, "{resultadoNotas}", "");

	$("layerFicha").style.visibility = "visible";
	$("layerFicha").innerHTML  = html;

	if (resultadoBusqueda.paquetes.length > 0) {
		document.getElementById("tablaResultadoPaquetes").style.display = "block";		
	} else {
		$("tablaResultadoPaquetes").style.display = "none";				
	}
	if (resultadoBusqueda.aereos.length > 0) {
		$("tablaResultadoAereos").style.display = "block";		
	} else {
		$("tablaResultadoAereos").style.display = "none";				
	}				
	if (resultadoBusqueda.paquetes.length == 0 && resultadoBusqueda.aereos.length == 0) {
		$("resultadoNada").style.display = "block";		
	} else {
		$("resultadoNada").style.display = "none";		
	}
	new Effect.Appear("layerFicha", {duration: 0.25});
	
	
	scroll(0,0);			
	
}

function verificarPopup() {
	/** Abre popup */
	if ((popup!=undefined) && (popup!=false)) {
		$("popup").style.width = popup.ancho + "px";
		$("popup").style.height = popup.alto + "px";
		$("popup").style.top = popup.y + "px";
		$("popup").style.left = popup.x + "px";	
		$("popup").style.display = "block";
		new Draggable("popup");
	}
}

/**
 * Cerrar el Popup 
 */
function cerrarPopup() {
	if ($("popup")!=undefined) {
		new Effect.Fade("popup", {duration: 1});
	}
}


/***
 * 
 *  FUNCIONES UTILES
 * 
 */


function generarLinkPaq(paquete) {
	return "<a href=\"javascript:verPaquete('" + paquete.id + "')\">" 
											+ paquete.nombre + "</a>";
}


function generarLinkPaqPrecio(paquete) {
	return "<div style='float: left'><a href=\"javascript:verPaquete('" + paquete.id + "')\">" 
			+ paquete.nombre + "</a></div><div style='float: right; text-align: right; padding-right: 2px;'>" + paquete.precio + "</div>";
}


function setVisible(id, visible) {
	if (visible) {
		$(id).style.visibility = "visible";
	} else {
		$(id).style.visibility = "hidden";		
	}
}

function max(a, b) {
	if (a > b) 
		return a;
  	  else 
  	    return b;
}

function ultimoCaracter(id) {
	return parseInt(id.substring(id.length-1));
}

function min(a, b) {
	if (a < b) 
		return a;
  	  else 
  	    return b;
}

var nombreMeses = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]

function fechaMySQL(fecha) {
	if ((fecha=="null") || (fecha==null) || (fecha=="00000000")) {
		return "  "
	} else {
		return fecha.substring(8, 10) + "-" + nombreMeses[fecha.substring(5, 7)-1] + "-" + fecha.substring(0, 4);		
	}
}

function abrir(url) { //v2.0
  window.open(url,'_blank', 'scrollbars=yes,width=530,height=580');
}