// JavaScript Document

/* NH Hoteles
 * Javascript de comportamiento de interfaz.  Funciones comunes.  Hacen uso de jQuery 1.4
 *
 * Version - 0.1
 * Fecha - 16/06/2010
 * Autor - Miguel Alvarez
 *
 */
var j = jQuery.noConflict();
// Do something with jQuery

var tipActivo = 0;
var intervalo;
var itemActual = 0;

function reloj() {
	itemActual = (itemActual + 1) % (j(".carrusel_img .item").length);		
	var id_lnk = "img_item0"+(itemActual+1);
	j(".carrusel_img .botonera li").removeClass('activo');
	j(".carrusel_img .botonera li ."+id_lnk).parent().addClass('activo');
	j(".carrusel_img .item").fadeOut('fast');
	j("#"+id_lnk).fadeIn('fast');
}

j(document).ready(function() {
	j(".jshide").hide();
	
	/* Top destination 01 */
	j(".js_search_location").hide();
	j(".js_search_term:first").show();
	j(".input-term").click(function(e) {			
		j('.js_search_location').hide();															
		j('.js_search_term').show();
	});
	j(".input-location").click(function(e) {
		j('.js_search_location').show();															
		j('.js_search_term').hide();
	});
	
	/* Top destination 02 */
	j(".js_search").hide(); //Esconde todo el contenido
	j(".ulsearch li:first").addClass("active").show(); //Activa la primera tab
	j(".js_search:first").show(); //Muestra el contenido de la primera tab
	j(".ulsearch li a").click(function(e) {
		e.preventDefault();
		j(".ulsearch li").removeClass("active"); //Elimina las clases activas
		j(this).parent().addClass("active"); //Agrega la clase activa a la tab seleccionada
		j(".js_search").hide(); //Esconde todo el contenido de la tab
		var activeTab = j(this).attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido
		j(activeTab).show(); //Mostrar el contenido activo
		j(".ulsearch li").removeClass('activelast');	
	});
		
	if (j(".layer_hab").length) {
		j(".layer_hab").click(function(e) {
			e.preventDefault();														 
			j(".layer_habitat").show();	
			j(".calen").hide();
			j(".layer_destination").hide();
		});
		j(".layer_habitat .lnk_cerrar").click(function(e) {
			e.preventDefault();																								 
			j(".layer_habitat").hide();	
		});
		
	}

	if (j(".destination").length) {
		j(".destination .uldestination li a").click(function(e) {
			e.preventDefault();			
			j(".layer_destination").hide();
			j(".calen").hide();
			j(".layer_habitat").hide();
			j(this).next().show();	
		});
		j(".layer_destination .lnk_cerrar").click(function(e) {
			e.preventDefault();																								 
			j(".layer_destination").hide();	
		});
		
	}


	j(".ulsearch li:last").click(function(e) {
		j(this).addClass('activelast');	
	});
	
	/* Top destination 02 */
	j(".top-search03 ul li ul").hide();
	j(".js_topdestino").click(function(e) {
		e.preventDefault();
		if (!j(this).parent().hasClass('active')) {
			j(".top-search03 ul li").removeClass("active"); //Elimina las clases activas
			j(this).parent().addClass("active"); //Agrega la clase activa a la tab seleccionada
			j(".top-search03 ul li ul").hide(); //Esconde todo el contenido de la tab
			j(this).next().show(); //Muestra todo el contenido de la tab		
		} else {
			j(".top-search03 ul li").removeClass("active"); //Elimina las clases activas
			j(".top-search03 ul li ul").hide(); //Esconde todo el contenido de la tab
		}
	});
	j(".js_searched").click(function(e) {
		e.preventDefault();
		if (!j(this).parent().hasClass('active')) {
			j(".top-search03 ul li").removeClass("active"); //Elimina las clases activas
			j(this).parent().addClass("active"); //Agrega la clase activa a la tab seleccionada
			j(".top-search03 ul li ul").hide(); //Esconde todo el contenido de la tab
			j(this).next().show(); //Muestra todo el contenido de la tab
		} else {
			j(".top-search03 ul li").removeClass("active"); //Elimina las clases activas
			j(".top-search03 ul li ul").hide(); //Esconde todo el contenido de la tab
		}

	});
	
	// Calendario 
	/*j(".ico-calendar").click(function(e) {
		e.preventDefault();		
		j(".calen").hide(); //Esconde todo el contenido de la tab
		var activeCalen = j(this).attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido
		j(activeCalen).show(); //Mostrar el contenido activo		
		j(".layer_habitat").hide();
		j(".layer_destination").hide();
	});
	*/
	
	// 
	j(".hint")
		.each(function(i, e) {
		 var hint = j(e).attr("title");
		 j(e)
			.data("hint", hint)
			.focus(function() {
			 if(j(this).val()==j(this).data("hint"))
				j(this).val("").removeClass("hint");
			})
			.blur(function() {
			 if(j(this).val()=="")
				j(this).val(j(this).data("hint")).addClass("hint");
			});
		 if(hint && j(e).val()=="") {
			j(e).val(hint)
		 } else {
			j(e).removeClass("hint");
		 }
		});
	
	/* Preferencias Join NH */
	j(".preferencias").hide();
	j("a.a-preferencias").click(function(e) {
		if (!j(this).hasClass('active')) {
			e.preventDefault();
			j(this).addClass("active"); //Agrega la clase activa a la tab seleccionada
			j(this).parent().next().show();
		} else {
			e.preventDefault();
			j(this).removeClass("active"); //Agrega la clase activa a la tab seleccionada
			j(this).parent().next().hide();
		}
	});

	/* Cotitular */
	j(".cotitular").hide();
	j(".a-cotitular").click(function(e) {											
		j(this).toggleClass("active");														
		j(".cotitular").toggle();
	});
		
	
	/* PESTANAS */
	j(".tabs_content").hide(); //Esconde todo el contenido
	j("ul.tabs li:first").addClass("active").show(); //Activa la primera tab
	j(".tabs_content:first").show(); //Muestra el contenido de la primera tab
	j(".pestanas .tabs_content:first").show(); //Muestra el contenido de la primera tab
	//On Click Event
	j("ul.tabs li:not(.notabs) a").click(function(e) {
		e.preventDefault();
		j("ul.tabs li").removeClass("active"); //Elimina las clases activas
		j(this).parent().addClass("active"); //Agrega la clase activa a la tab seleccionada
		if (j(this).parents(".pestanas").length) {
			j(this).parents(".pestanas").find('.tabs_content').hide(); //Esconde todo el contenido de la tab
		} else {
			j('.tabs_content').hide(); //Esconde todo el contenido de la tab		
		}
		var activeTab = j(this).attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido
		j(activeTab).show(); //Mostrar el contenido activo		
	});
	j("ul.tabs li .capapdf").hide();
	j("ul.tabs li.a-capapdf a").click(function(e) {
		e.preventDefault();
		j(this).parent().find('.capapdf').show(); //Muestra la capapdf
	});
	
	j("ul.tabs li .capapdf .lnk_cerrar").click(function(e) {
		e.preventDefault();
		j(this).parents('.capapdf').hide(); //Muestra la capapdf
	});

	
	/* TIPS */
	//On Click Event
	if (j("a.tip_lnk").length) {
		j("a.tip_lnk").click(function(e) {
			e.preventDefault();
			j(".tip_layer").hide(); //Esconde todo el contenido de la tab
			j(this).next(".tip_layer").show(); //Muestra todo el contenido de cada tip
			tipActivo = 1;	
			j(".layer_destination").hide();
			j(".calen").hide();		
		});
		j(".tip_layer a.lnk_cerrar").click(function(e) {
			e.preventDefault();
			j(this).parent().hide();
		});
		j("body").click(function(e) {
			if (tipActivo==2) {
				tipActivo = 0;	
				j(".tip_layer").hide(); //Esconde todo el contenido
			} else if(tipActivo==1) {
				tipActivo = 2;
			}
		});
	}
	
	/* Mostrar contenido ocultos pestanas, enlaces... */
	if(j(".lnk_mostrar").length) {
		j(".lnk_ocultar").hide(); //Esconde todo el contenido
		j(".pestanas").hide(); //Esconde todo el contenido	
		j(".capaenlaces").hide(); //Esconde todo el contenido	
		j(".capareservas").hide(); //Esconde todo el contenido	
		j("ul.tabs li:first").addClass("active").show(); //Activa la primera tab
		j(".tabs_content:first").show(); //Muestra el contenido de la primera tab
		//On Click Event
		j(".lnk_mostrar").click(function(e) {
			e.preventDefault();
			j(".calen").hide();
			j(this).hide(); //Ocultar lnk_mostrar
			j(this).prev(".lnk_ocultar").show(); //Mostrar lnk_ocultar
			var target = j(this).attr("href"); //Encuentra el valor del atributo href para identificar el contenido
			j(target).show(); //Mostrar el contenido activo	
		});
		j(".lnk_ocultar").click(function(e) {
			e.preventDefault();
			j(".calen").hide();
			j(this).hide(); //Ocultar lnk_mostrar
			j(this).next(".lnk_mostrar").show(); //Mostrar lnk_mostrar
			var target = j(this).attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido
			j(target).hide(); //Ocultar el contenido activo		
		});	
	}

	/* Mostrar contenido ocultos pestanas, detalle del hotel... */
	if(j("#caledarioprecios").length) {
		j(".lnk_ocultar").hide(); //Esconde todo el contenido
		j(".pestanas").hide(); //Esconde todo el contenido	
		j(".capaenlaces").hide(); //Esconde todo el contenido	
		j(".capareservas").hide(); //Esconde todo el contenido	
		j("ul.tabs li:first").addClass("active").show(); //Activa la primera tab
		j(".tabs_content:first").show(); //Muestra el contenido de la primera tab
		//On Click Event
		j(".lnk_mostrar").click(function(e) {
			e.preventDefault();
			j(".calen").hide();
			j(this).hide(); //Ocultar lnk_mostrar
			j(this).prev(".lnk_ocultar").show(); //Mostrar lnk_ocultar
			var target = j(this).attr("href"); //Encuentra el valor del atributo href para identificar el contenido
			if (target == "#caledarioprecios") {			
				var display = j("#capareser").css('display');
				if (display == "block"){
					j("#capareser").hide();
					j(".indent .left .lnk_ocultar").hide(); //Ocultar lnk_ocultar
					j(".indent .left .lnk_mostrar").show(); //Mostrar lnk_mostrar					
				}
			}
			if (target == "#capareser") {
				var display = j("#caledarioprecios").css('display');
				if (display == "block"){
					 j("#caledarioprecios").hide();
					 j(".roomprice .lnk_ocultar").hide(); //Ocultar lnk_ocultar
					 j(".roomprice .lnk_mostrar").show(); //Mostrar lnk_mostrar					
				}
			}
			j(target).show(); //Mostrar el contenido activo	
		});
		j(".lnk_ocultar").click(function(e) {
			e.preventDefault();
			j(".calen").hide();
			j(this).hide(); //Ocultar lnk_mostrar
			j(this).next(".lnk_mostrar").show(); //Mostrar lnk_mostrar
			var target = j(this).attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido
			j(target).hide(); //Ocultar el contenido activo		
		});	
	}
	
	/* Mostrar contenido ocultos pestanas, enlaces... */
	if(j(".estancias").length) {
		j(".lnk_ocultar02").hide(); //Esconde todo el contenido
		//On Click Event
		j(".lnk_mostrar02").click(function(e) {
			e.preventDefault();
			j(this).hide(); //Ocultar this
			j(this).prev(".lnk_ocultar02").show(); //Mostrar lnk_ocultar02
			j(this).parent().prev().find('li.jshide').show();
		});
		j(".lnk_ocultar02").click(function(e) {
			e.preventDefault();
			j(this).hide(); //Ocultar this
			j(this).next(".lnk_mostrar02").show(); //Mostrar lnk_mostrar02
			j(this).parent().prev().find('li.jshide').hide();
		});	
	}	

	/* Clicktocall */
	if(j(".clicktocall").length) {
		j(".clicktocall").hide(); //Esconde todo el contenido
		//On Click Event
		j(".ico_telefono").click(function(e) {
			e.preventDefault();
			j(".clicktocall").show();			
		});
		j(".ico_telefono02").click(function(e) {
			e.preventDefault();
			j(".clicktocall").show();		
		});
		j(".clicktocall #cerrar").click(function(e) {
			e.preventDefault();
			j(".clicktocall").hide();
		});		
	}
	
	// Desplegar listado de puntos en los paises //
	if(j(".listpaises").length) {
		//On Click Event
		j(".listpaises li a").click(function(e) {
			e.preventDefault();
			var display = j(this).next().css('display');
			if (display == "none"){
				j(this).next().show();  
			} else {
				j(this).next().hide();  
			}
		});
	}

	// Desplegar listado de puntos en los paises //
	if(j(".cajaempresa").length) {
		j(".cajaempresa").hide();
		j(".lnk_ocultar03").hide();
		//On Click Event
		j(".lnk_mostrar03").click(function(e) {
			e.preventDefault();
			j(".cajaempresa").show();
			j(this).hide();
			j(".lnk_ocultar03").show();
		});
		j(".lnk_ocultar03").click(function(e) {
			e.preventDefault();
			j(".cajaempresa").hide();
			j(this).hide();
			j(".lnk_mostrar03").show();
		});		
	}
	
	// Desplegar listado del directorio de paises //
	if(j(".directorio .list01").length) {
		//On Click Event
		j(".directorio .list01 .lnk_vermas").click(function(e) {
			e.preventDefault();
			var display = j(this).next().css('display');
			if (display == "none"){
				j(this).next().show();  
				j(this).removeClass('lnk_vermas');
				j(this).addClass('lnk_quitar');
			} else {
				j(this).next().hide();  
				j(this).removeClass('lnk_quitar');
				j(this).addClass('lnk_vermas');
			}
		});
	}

	// Desplegar listado del directorio de paises //
	if(j(".busca").length) {
		//On Click Event
		j("#home_searchbox").hide();
		j(".busca").mouseenter(function(e) {
			j("#home_searchbox").show();
			j(this).addClass('busca-activo');
		});
		/*j("#home_searchbox").mouseleave(function(e) {
			e.preventDefault();
			j("#home_searchbox").hide();
			j(".busca").removeClass('busca-activo');
			j(".tip_layer").hide();
			j(".calen").hide();
		});	*/	
		j("#home_searchbox").mouseenter(function(e) {
			e.preventDefault();
			j("#home_searchbox").show();
			j(".busca").addClass('busca-activo');
			e.stopPropagation();
		});
		j("#home_searchbox").click(function(e) {
			e.preventDefault();
			e.stopPropagation();
		});
		j(".mod_home").mouseleave(function(e) {
			e.preventDefault();
			j("#home_searchbox").hide();
			j(".busca").removeClass('busca-activo');
			j(".tip_layer").hide();
			j(".calen").hide();
			e.stopPropagation();
		});
		j(document).click(function(e) {
			e.preventDefault();
			j("#home_searchbox").hide();
			j(".busca").removeClass('busca-activo');
			j(".tip_layer").hide();
			j(".calen").hide();
		});	
		j("#deco").click(function(e) {
			e.preventDefault();
			j("#home_searchbox").hide();
			j(".busca").removeClass('busca-activo');
			j(".tip_layer").hide();
			j(".calen").hide();
		});			

	}
	
	// Desplegar listado del directorio de paises //
	if(j(".favorito").length) {
		//On Click Event
		j(".hotels_favoritos").hide();
		j(".favorito").mouseenter(function(e) {
			j(".hotels_favoritos").show();
			j(this).addClass('favorito-activo');
		});
		j(".hotels_favoritos").mouseleave(function(e) {
			e.preventDefault();
			j(".hotels_favoritos").hide();
			j(".favorito").removeClass('favorito-activo');
		});		
		j(".hotels_favoritos").mouseenter(function(e) {
			e.preventDefault();
			j(".hotels_favoritos").show();
			j(".favorito").addClass('favorito-activo');
		});
		j(".mod_home").mouseleave(function(e) {
			e.preventDefault();
			j(".hotels_favoritos").hide();
			j(".favorito").removeClass('favorito-activo');
		});		
	}

	/* .carrusel_texto  */
	if (j(".carrusel_texto").length) {	
		j(".carrusel_texto")
			.css('position','relative')
			.find(".item")
				.css({position: 'absolute', top: 0}).fadeOut(1);
		j(".carrusel_texto .item:first").fadeIn(1);
		j(".carrusel_texto .botonera")
			.show()
			.find("li a")
				.click(function(e) {
					e.preventDefault();
					j(".carrusel_texto .botonera li").removeClass('activo');
					j(this).parent().addClass('activo');
					var idlnk = j(this).attr('class');
					j(".carrusel_texto .item").fadeOut('fast');
					j("#"+idlnk).fadeIn('fast');
				});
	}
	
	/* .carrusel_img  */
	if (j(".carrusel_img").length) {
		intervalo = setInterval("reloj()",8000);		
		j(".carrusel_img")
			.css('position','relative')
			.find(".item")
				.css({position: 'absolute', top: 0}).fadeOut(1);
		j(".carrusel_img .item:first").fadeIn(100);
		j(".carrusel_img .botonera")
			.show()
			.find("li a")
				.click(function(e) {
					e.preventDefault();
					j(".carrusel_img .botonera li").removeClass('activo');
					j(this).parent().addClass('activo');
					var idlnk = j(this).attr('class');
					j(".carrusel_img .item").fadeOut('fast');
					j("#"+idlnk).fadeIn('fast');
					clearInterval(intervalo);
				});
	
	}
	var espacio = 40; // Separación vertical inicial entre imágenes
	var desplazamiento = 182 - 40; // Desplazamiento vertical cuando quiero mostrar una imagen (se puede calcular como altura_imagen - espacio)
	j(".imageMenu a").css('position', 'absolute');
	var items = j(".imageMenu a");
	for(i=0; i<items.length; i++) {
		// Poner posicion inicial
		var item = items[i];
		j(item).css("top", i*espacio).data("pos", i*espacio);

		// Eventos de ratón
		j(item).mouseenter(function(e) {
			// Llevamos a todas las anteriores a su posición original
			var anteriores = j(this).prevAll().add(j(this));
			for(h=0; h<anteriores.length; h++) {
				var anterior = anteriores[h];
				var pos = j(anterior).data("pos");
				j(anterior).animate({"top": pos}, 200);
			}
			// Llevamos a todas las posteriores a su posicion desplazada
			var posteriores = j(this).nextAll();
			for(h=0; h<posteriores.length; h++) {
				var posterior = posteriores[h];
				var pos = j(posterior).data("pos") + desplazamiento;
				j(posterior).animate({"top": pos}, 200);
			}
		});
	}
	
	j(window).resize();
	
	/*Bajar contenido*/
	j("#aboutCityRelocated").replaceWith(j("#aboutCity"));
	j("#positionButtomRelocated").replaceWith(j("#positionButtom"));

	
	// Filtros mapa
	j(".filtrosmap .filtrosmap-cont").hide();
	j(".filtrosmap .a-afina").click(function(e) {																			 
		e.preventDefault();
		j(this).toggleClass("activo");
		j(this).next(".filtrosmap-cont").slideToggle("fast");
	});		

	// Tabla de hoteles
	//j(".listtablehotels .tablehotels .trciudad").hide().nextAll("tr:not(.trpais)").hide();
	j(".listtablehotels .tablehotels .trpais").find("td.col1").addClass('activo');
	j(".listtablehotels .tablehotels .trpais").click(function(e) {																			 
		e.preventDefault();
		j(this).find("td.col1").toggleClass("activo");
		j(this).nextUntil(".trpais").slideToggle("slow")
	});	
	
	// Simulacion Cargando al clickar en el chebox de actualizar mapa.
	// El funcionamiento deberia ser cuando mueves el mapa apareciera el cargando si has activado  el mapa, o culaquier interacion sobre el listado por ajax
	j(".actualizarmap input").click(function(e) {	
		if (j(this).is(":checked")) {
			j(".lnk_refresh").addClass('lnk_refresh_disabled');
			j(".overlaylisttablehotels").css('position','absolute');
			j(".overlaylisttablehotels").css('height',j(".cont-overlay").height());
			j(".overlaylisttablehotels").fadeTo("slow", 0.95);
		} else {
			j(".overlaylisttablehotels").fadeTo("slow", 0);
			j(".overlaylisttablehotels").hide();
			j(".lnk_refresh").removeClass('lnk_refresh_disabled');
		}
	});	
	
	/* Subscribe */
	j(".a-subscribes").click(function(e) {	
		e.preventDefault();
		j(this).toggleClass("lnk_rss_activo").parent().find(".box-subscribes").slideToggle('slow', function() {
    	// Animation complete.
  	})
	});				
		
});

// Ajuste de dimensiones de la capa "deco" para el Flash de fondo
j(window).resize(function() {	
	if (j(window).height()>288) {
		j('#deco').height(j(document).height());
	} else {
		j('#deco').height(j(window).height());
	}
	if (j(window).height()<j(document).height()) {
		j('#deco').height(j(document).height());
	} else {
		j('#deco').height(j(window).height());
	}
});



function focusDefaultVal(inputName) {
	var searchInput = jQuery("#"+inputName);
	var titleSearch = searchInput.attr("title");
	if(searchInput.val() == titleSearch) {
		searchInput.val("").removeClass("defaultValue");
	}
}

function blurDefaultVal(inputName) {
	var searchInput = jQuery("#"+inputName);
	var titleSearch = searchInput.attr("title");
	if(searchInput.val() == "") {
		searchInput.val(titleSearch).addClass("defaultValue");
	}
}

