var e = {
	addEvent : function(obj, evType, fn, useCapture){
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		}else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		}else {
			return false;
		}
	}
}

var links={
	print:{
		footer:function(){
			var obj = $("#footerLinks");
			var print = $("<li><a href='javascript:window.print()'>Imprimir</a></li>");		
			$(obj).find("li").eq(1).after(print);
		}
	}
}
/*
var behaviours = {	
	borraCampo:function(id){
		document.getElementById(id).onfocus=function(){
			document.getElementById(id).value=""
		}
	}
}
*/

var behaviours = {	
	borraCampo:function(id){
		document.getElementById(id).onfocus=function(){
			document.getElementById(id).value=""
		}
	},
	
	collapse:{

		detailLinea:{

			lista:null,

			titulos:null,

			init:function(){

				var _this = null;

				behaviours.collapse.listas = $(".cont");

				behaviours.collapse.titulos = $(".wrapActionTitle");

				behaviours.collapse.listas.each(function(){ $(this).hide(); })

				behaviours.collapse.titulos.each(function(i){ 

					_this = $(this);

					_this.append(behaviours.collapse.createAction(literal["vermas"])); 

					_this.find("a").eq(0).click(function(){

						behaviours.collapse.detailLinea.action(i);

						return false;

					})

				})

				behaviours.collapse.detailLinea.resetPosition();

			},	

			action:function(i){

				var titulo = $(behaviours.collapse.titulos.eq(i));

				var lista = $(behaviours.collapse.listas.eq(i));

				if (titulo.hasClass("show")){

					titulo.toggleClass("show");

					titulo.find("a").eq(0).html(literal["cerrar"])

					lista.show();

				}else{

					titulo.addClass("show");

					titulo.find("a").eq(0).html(literal["vermas"])

					lista.hide();

				}

				behaviours.collapse.detailLinea.resetPosition();				

			},

			resetPosition:function(){

				jQuery("#footer").css("position", "static");

				jQuery("#footer").css("position", "relative");

			}

		},

		createAction:function(txt){

			var aElement = $("<a href='javascript:void(null)'>"+txt+"</a>");

			return aElement;

		}

	}

}






var carruseles = {	
	mycarousel_initCallback:function(carousel) {	
		var clicks = 1;
	    var pages = parseInt(carousel.size()) - 1;  	
		if($("#prev")) $("#prev").css("visibility", "hidden");
	    jQuery('#next').bind('click', function() {
	        carousel.next();
			clicks++;
			carruseles.updatePaging(clicks,pages);
	        return false;
	    });
				
	    jQuery('#prev').bind('click', function() {
	        carousel.prev();
			clicks--;
			carruseles.updatePaging(clicks,pages);
	        return false;
	    });
	},
	updatePaging:function(clicks, pages){
		var prev = $("#prev");		
		var next = $("#next");
		if(clicks == 1) {
			$(prev).css("visibility", "hidden");
			$(next).css("visibility", "visible");
		}
		else if(clicks != pages) {		
			$(prev).css("visibility", "visible");
			$(next).css("visibility", "visible");
		}
		else if(clicks == pages) {		
			$(prev).css("visibility", "visible");
			$(next).css("visibility", "hidden");
		}		
	},		
	galleries:{
		clicks:1,
		items:0,
		pages:0,
		mycarousel_initCallback:function(carousel) {
			carruseles.galleries.items = carousel.size()
			carruseles.galleries.pages = parseInt(carruseles.galleries.items) - 2; 						
		    jQuery('#next').bind('click', function() {
		        carousel.next();														
				carruseles.galleries.clicks++;
				carruseles.updatePaging(carruseles.galleries.clicks,carruseles.galleries.pages);				
		        return false;
		    });						
		    jQuery('#prev').bind('click', function() {
		        carousel.prev();
				carruseles.galleries.clicks--;					
				carruseles.updatePaging(carruseles.galleries.clicks,carruseles.galleries.pages);				
		        return false;
		    });
		},			
		init:function(){			
			//var txt = ($("#press").hasClass("fotos")) ? "Foto" : "Video";			
			var number = $("#thumb").parent().find(".number").eq(0);
			$("#thumb").toggleClass("noJv");
			number.toggleClass("noJv");	
			$("#prev").css("visibility","hidden");
			var obj = $(".jcarousel-scroll.clearFix");
			if($("#thumb").find("li").length > 4){
				number.toggleClass("alone");	
				var pag = $("<ul><li id='mycarousel-prev'><a id='prev' href='#'><img src='/images/Hotel/flecha_left.gif' /></a></li><li id='mycarousel-next'><a id='next' href='#'><img src='/images/Hotel/flecha_right.gif' /></a></li></ul>");			
				obj.append(pag);
			}
			carruseles.updatePaging(carruseles.galleries.clicks,carruseles.galleries.pages);			
		   jQuery("#thumb").jcarousel({
	    	    scroll: 1,
	        	initCallback: carruseles.galleries.mycarousel_initCallback,		        								
				buttonNextHTML: null,
		        buttonPrevHTML: null				
	    	});
		}
	}
}






var gallery = {
	media:null,
	history:null,
	init:function(){
		var obj = $("#thumb");
		var aElements = $(obj).find("a");
		gallery.media = $("#detailItem").find(".innerDetail");
		for(var i = 0; i < aElements.length; i++) {
			aElements[i].onclick = function(i){
				return function(e){
					if(gallery.history) $(gallery.history).toggleClass("current");
					$(this).toggleClass("current");					
					gallery.showMedia(this, i);
					gallery.history = this;
					return false;
				};
			}(i);
		}
	},
	showMedia:function(obj,idx){
		var item = galleryItems[idx].src;
		var downloadMedia = $("#detailItem .downloadMedia a");
		$(gallery.media).empty();		
		if(item.indexOf("http") == -1){
			$(gallery.media).append(gallery.createImg(galleryItems[idx]));
		}else{
			gallery.createSwf(galleryItems[idx]);	
		}
		$("#text").empty();
		$("#text").html("<p>" + galleryItems[idx].txt + "</p>");
		downloadMedia.attr("href",galleryItems[idx].src);
		downloadMedia.html("("+galleryItems[idx].ext+" - "+galleryItems[idx].size+" kb)")				
	},
	createImg:function(data){
		return $("<img src='"+data.src+"' width='508' height='320' alt='"+data.alt+"' />");
	},

	createSwf:function(data){
		var s1 = null;
		if(data.src.indexOf(".swf") != -1){
			s1 = new SWFObject("/swf/mediaplayer.swf","single","415","344","7");					
		}else{
			s1 = new SWFObject(data.src,"single","415","344","7");					
		}
		s1.addParam("allowfullscreen","true");
		s1.addVariable("file",data.src);
		s1.addVariable("image",data.image);
		s1.addVariable("width","415");
		s1.addVariable("height","344");
		s1.addVariable("wmode", "transparent");
		s1.addVariable("bgcolor", "#ffffff");
		s1.write("player1");				
	}
}


var load={
	existeId:function(cid){	
		if(document.getElementById(cid)) return true;		
		return false;
	},
	setEvents:function(){		
						
	}
}




var formsValidations = {
	setMsgError:function(txt, form){
	
		var parentForm = form.parent();
		var msgError = parentForm.find(".msgError");
		var divElement = (msgError.length != 0) ? msgError.eq(0) : document.createElement("div");		
		var ulElement = document.createElement("ul");
		var liElement = null;		
		var errors = txt.split("|");				
		jQuery(divElement).attr("class", "msgError");
		jQuery(divElement).attr("tabIndex","-1");
		if(jQuery(divElement).find("ul").length != 0) jQuery(divElement).empty();
		for(var i = 0; i < errors.length - 1; i++){
			liElement = document.createElement("li");
			liElement.appendChild(document.createTextNode(errors[i]));
			ulElement.appendChild(liElement);
		}
		jQuery(divElement).append(ulElement);		
		if(msgError.length == 0) form.before(jQuery(divElement));
		// rodeo para los IE y evitar el solapamiento del pie cuando se producen errores en los formularios superiores
		jQuery("#footer").css("position", "static");
		jQuery("#footer").css("position", "relative");
		jQuery(divElement).focus();
	},
	
	//VALIDACION HOME
	validaFormHomeReservas:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		
		if(!f.find("select.diaEntradaHome").attr("value") || !f.find("select.mesEntradaHome").attr("value") || !f.find("select.anioEntradaHome").attr("value")  ) errorTxt += literal["contact"][15];
		else
		{
			if(!regularExpressions.esFecha(f.find("select.diaEntradaHome").attr("value") + "/" +  f.find("select.mesEntradaHome").attr("value") + "/" + f.find("select.anioEntradaHome").attr("value")))  errorTxt += literal["contact"][16];
		}
			

		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else return true;
	},
	
	validaFormReservas:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		if(f.find("input.condicionesInput[@checked]").length == 0) errorTxt += literal["contact"][6];
		if(!f.find("input.DireccionInput").attr("value")) errorTxt += literal["contact"][11];
		if(!f.find("input.paisInput").attr("value")) errorTxt += literal["contact"][13];
		if(!f.find("input.provinciaInput").attr("value")) errorTxt += literal["contact"][10];
	
	
	//FECHAS VALIDACIONES
		if(!f.find("select.DiasEntradaSelect").attr("value") || !f.find("select.MesesEntradaSelect").attr("value") || !f.find("select.AniosEntradaSelect").attr("value")  ) errorTxt += literal["contact"][15];
		else
		{
			if(!regularExpressions.esFecha(f.find("select.DiasEntradaSelect").attr("value") + "/" +  f.find("select.MesesEntradaSelect").attr("value") + "/" + f.find("select.AniosEntradaSelect").attr("value")))  errorTxt += literal["contact"][16];
		}
		
		
		if(!f.find("select.DiasSalidaSelect").attr("value") || !f.find("select.MesesSalidaSelect").attr("value") || !f.find("select.AniosSalidaSelect").attr("value")  ) errorTxt += literal["contact"][17];
		else
		{
			if(!regularExpressions.esFecha(f.find("select.DiasSalidaSelect").attr("value") + "/" +  f.find("select.MesesSalidaSelect").attr("value") + "/" + f.find("select.AniosSalidaSelect").attr("value")))  errorTxt += literal["contact"][18];
			
		}
		
		//Comparando si la fecha de entrada es mayor que la fecha de salida
		
		if(f.find("select.DiasEntradaSelect").attr("value") && f.find("select.MesesEntradaSelect").attr("value") && f.find("select.AniosEntradaSelect").attr("value") && f.find("select.DiasSalidaSelect").attr("value") && f.find("select.MesesEntradaSelect").attr("value") && f.find("select.AniosEntradaSelect").attr("value") ) 
		{
			if((regularExpressions.esFecha(f.find("select.DiasEntradaSelect").attr("value") + "/" +  f.find("select.MesesEntradaSelect").attr("value") + "/" + f.find("select.AniosEntradaSelect").attr("value"))) && (regularExpressions.esFecha(f.find("select.DiasSalidaSelect").attr("value") + "/" +  f.find("select.MesesSalidaSelect").attr("value") + "/" + f.find("select.AniosSalidaSelect").attr("value"))))
			{
				if (f.find("select.AniosEntradaSelect").attr("value") + f.find("select.MesesEntradaSelect").attr("value") + f.find("select.DiasEntradaSelect").attr("value") >= f.find("select.AniosSalidaSelect").attr("value") + f.find("select.MesesSalidaSelect").attr("value") + f.find("select.DiasSalidaSelect").attr("value")) errorTxt += literal["contact"][21];
			}
		}
		
		//COMPROBACIÓN SI FECHA DE ENTRADA ES MENOR A ACTUAL
		
		//HOY
		hoy = new Date(); 
		//Año
		anio = hoy.getFullYear(); 
		//Mes
		if (hoy.getMonth() == "1" || hoy.getMonth() == "2" || hoy.getMonth() == "3" || hoy.getMonth() == "4" || hoy.getMonth() == "5" || hoy.getMonth() == "6" || hoy.getMonth() == "7" || hoy.getMonth() == "8" || hoy.getMonth() == "9")
		{
			mes = "0" + hoy.getMonth() + 1;
		}
		else
		{
			mes = hoy.getMonth() + 1; 
		}		
		//Día
		dia = hoy.getDate();
		
		if(f.find("select.DiasEntradaSelect").attr("value") && f.find("select.MesesEntradaSelect").attr("value") && f.find("select.AniosEntradaSelect").attr("value"))
		{
			if((regularExpressions.esFecha(f.find("select.DiasEntradaSelect").attr("value") + "/" +  f.find("select.MesesEntradaSelect").attr("value") + "/" + f.find("select.AniosEntradaSelect").attr("value"))))
			{
				//Si la fecha que se introduce es menor ke la fecha de hoy, salta el ERROR
				if (f.find("select.AniosEntradaSelect").attr("value") + "" + f.find("select.MesesEntradaSelect").attr("value") + "" + f.find("select.DiasEntradaSelect").attr("value") <= anio + "" + mes + "" + dia) errorTxt += literal["contact"][22];
			}
		}
		
		
		//Comprobacion de Numero de habitaciones y numero de personas por habitacion
		if(!f.find("select.NumHabSelect").attr("value")) errorTxt += literal["contact"][19];
		if(!f.find("select.NumPersonSelect").attr("value")) errorTxt += literal["contact"][20];


		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else return true;
	}, 
	validaFormClubAmigos:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		if(!f.find("input.paisInput").attr("value")) errorTxt += literal["contact"][13];
		if(!f.find("input.provinciaInput").attr("value")) errorTxt += literal["contact"][10];
	
	
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else return true;
	},
	
	validaFormContacto:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		if(f.find("input.condicionesInput[@checked]").length == 0) errorTxt += literal["contact"][6];
		
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else 
		{
			tracker('/contacto/');
			return true;
		}
	},
	
	validaFormContactoPrensa:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		if(!f.find("input.medioInput").attr("value")) errorTxt += literal["contact"][8];	
		if(f.find("input.condicionesInput[@checked]").length == 0) errorTxt += literal["contact"][6];
		
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else 
		{
			tracker('/contacto/prensa');
			return true;
		}
	},
	
	validaFormTienda:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else 
		{
			tracker('/contacto/tienda');
			return true;
		}
	},
	
	validaFormSolicitud:function(obj){
		var errorTxt = "";
		var f = jQuery(obj);
		
		//Datos personales
		if(!f.find("input.nombreInput").attr("value")) errorTxt += literal["contact"][0];
		if(!f.find("input.apellidosInput").attr("value")) errorTxt += literal["contact"][1];		
		
		if(!f.find("input.telefonoInput").attr("value")) errorTxt += literal["contact"][12];	
		else
		{
			if(!regularExpressions.esTelefono(f.find("input.telefonoInput").attr("value")))  errorTxt += literal["contact"][14];
		}
		
		if(!f.find("input.emailInput").attr("value")) errorTxt += literal["contact"][2];
		else{
			if(!regularExpressions.isValidEmail(f.find("input.emailInput").attr("value")))  errorTxt += literal["contact"][3];
		}	

		if(f.find("input.condicionesInput[@checked]").length == 0) errorTxt += literal["contact"][6];
		if(!f.find("input.paisInput").attr("value")) errorTxt += literal["contact"][13];
		if(!f.find("input.provinciaInput").attr("value")) errorTxt += literal["contact"][10];
	
		
		if(errorTxt != ""){	
			formsValidations.setMsgError(errorTxt, f);
			return false;
		}else return true;
	}
	
}


/* expresiones regulares para validar formularios */
var regularExpressions = {	
	isValidEmail:function (str){
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		return (filter.test(str));
	},
	esCadena:function(c) { return /^[0-9A-Za-z-\/��?�?�������������?����,'/\\t\n\r\s]+$/.test(c); },
	esAlfabetico:function(c){return /^([a-zA-Z])+$/.test(c);},
	esNumero:function(c){return /^[0-9]+$/.test(c);},
	esTelefono:function(c){return /^[0-9\s\+\-)(]+$/.test(c)},
	
	esFecha:function(c){
		if(!/^([0-2]{1}[1-9]{1}|10|20|30|31)\/(0[1-9]{1}|10|11|12)\/([0-9]{4})+$/.test(c))return false;
		var fch=c.split("/")
		var bisiesto=((fch[2] % 4 == 0 && fch[2] % 100 != 0) || (fch[2] % 400 == 0))? 29 : 28;
		var diasMes=[31,bisiesto,31,30,31,30,31,31,30,31,30,31];
		if(fch[0]>diasMes[fch[1]-1]) return false;
		return true;
	}
	
	
}




jQuery(document).ready(function() {	
	links.print.footer();
		
	if( jQuery(".wrapActionTitle").length != 0)  behaviours.collapse.detailLinea.init();
	
	if (load.existeId("press") && load.existeId("thumb")){
		 gallery.init(); 	
		 carruseles.galleries.init();	
	}
	if (load.existeId("tabsPress")) fixes.setTabSel();	
	
	if( jQuery("form.Reservas").length != 0)  jQuery("form.Reservas").submit(function(){return formsValidations.validaFormReservas( jQuery(this))});
	if( jQuery("form.SolicitudPre").length != 0)  jQuery("form.SolicitudPre").submit(function(){return formsValidations.validaFormSolicitud( jQuery(this))});
	if( jQuery("form.clubAmigos").length != 0)  jQuery("form.clubAmigos").submit(function(){return formsValidations.validaFormClubAmigos( jQuery(this))});
	if( jQuery("form.Contacto").length != 0)  jQuery("form.Contacto").submit(function(){return formsValidations.validaFormContacto( jQuery(this))});
	if( jQuery("form.ContactoPrensa").length != 0)  jQuery("form.ContactoPrensa").submit(function(){return formsValidations.validaFormContactoPrensa( jQuery(this))});
	if( jQuery("form.Tienda").length != 0)  jQuery("form.Tienda").submit(function(){return formsValidations.validaFormTienda( jQuery(this))});
	//if( jQuery("form.formHomeReservas").length != 0)  jQuery("form.formHomeReservas").submit(function(){return formsValidations.validaFormHomeReservas( jQuery(this))});
	if( jQuery(".botones").length != 0)  CambiaTracker(this);
});

function FlashSeo(tipo,url)
{
	final = '/fichas/'+tipo;
	_gaq.push(['_trackPageview', final]);
	//pageTracker._trackPageview(final);
	document.location.href=url;	
}

function CambiaTracker(valor)
{
	
	var js = "javascript:_gaq.push(['_trackPageview', '/contacto/']);";
	var js3 = "javascript:_gaq.push(['_trackPageview', '/contactoprensa/']);";
	
	var newclick = new Function(js);
	var newclick3 = new Function(js3);
	
	//contacto
	$("#ctl00_ctl00_ctl00_ContentPlaceHolderDefault_BodyContentPlaceHolder_Contacto_Hotel_5_ImageButton1").attr('onclick','');
	$("#ctl00_ctl00_ctl00_ContentPlaceHolderDefault_BodyContentPlaceHolder_Contacto_Hotel_5_ImageButton1").attr('onclick',newclick);
	//prensa
	$("#ctl00_ctl00_ctl00_ContentPlaceHolderDefault_BodyContentPlaceHolder_SalaPrensaContacto_Hotel_8_ImageButton1").attr('onclick','');
	$("#ctl00_ctl00_ctl00_ContentPlaceHolderDefault_BodyContentPlaceHolder_SalaPrensaContacto_Hotel_8_ImageButton1").attr('onclick',newclick3);
	
	

	

}

function tracker(texto)
{
	_gaq.push(['_trackPageview', texto]);
}
