var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
// Hide those layers that should be made visible through Javascript (this way, they will be visible if javascript is disabled)
document.write("<style type='text/css'>.closed, .njs { display: none }</style>");
var T0 = (new Date()).getTime();

// Global function declarations
var GeneralFuncs = {
  langCurrChanged: function(ev) {
    location.href = location.href.split("?")[0] + ("?{name}={value}".substitute(ev.target));
  },
  destroy: function(i) {
    i.destroy();
  },
  externalize: function(elem) {
    elem.set("target", "_blank");
  },

  fixImageHeight: function(img) {
    // Asegura que se respeta la altura mínima de las imágenes en las vistas "foto"
    var parent = img.getParent();
    var minh = parent.getSize().y - 2; /* Substract border size */
    if (img.height < minh) img.height = minh;
  },
  
  initGALinks: function(elem) {
    elem.addEvent("click", GeneralFuncs.gaLinkClick);
  },
  
  initAjaxLinks: function(elem) {
    var _width=500;
    var _height;
    var re = /fbw:(\d*)/;
    var rel = elem.get("rel");

    if (elem.hasClass("sub1")) { _width=620; _height=280; }
    if (rel && rel.test(re)) _width = parseInt(rel.match(re)[1], 0);
    
    elem._clickOpts = { width: _width, height: (_height)?_height:150, autoHeight: (_height==undefined)};
    elem.addEvent("click", GeneralFuncs.ajaxLinkClick);
  },
  
  ajaxLinkClick: function(event) {
    event.stop();
    FxBox.open(event.target.href, event.target._clickOpts);
  },
  
  gaLinkClick: function(event) {
    var url = event.target.href;
    if (url !== null) {
      TrackingActions.link(url);
      return false;
    }
    return true;
  },
  
  popupLinkClick: function(event) { 
    event.stop();
    FxBox.showContent( $(event.target.rel).get("html"), event.target._opts);
  }
};

window.addEvent("domready", function() {
  var t0 = (new Date()).getTime();
  log("domready start delayed " + (t0 - T0) + "msecs");

  // -------------------------------------------------------------------------
  // Priority: Critical

  // -------------------------------------------------------------------------
  // Priority: High

  // -------------------------------------------------------------------------
  // Priority: Normal

  // Languages and currencies
  if ($("langcur")) {
    $$("#langcur select").addEvent("change", GeneralFuncs.langCurrChanged);
  }
  
  Abstract.init();

  // Links to open in FxBox
  $$("a.ajxload").each(GeneralFuncs.initAjaxLinks);

  // Show hidden div in FxBox
  $$(".showfxb").each(function(i) {
    i._opts = {width: 400, autoHeight: true};
    i.addEvent("click", GeneralFuncs.popupLinkClick);
  });
  // Comments
  $$(".commentThings").each(function(i) {
    i._opts = {width: 400, autoHeight: true};
    i.addEvent("click", function(e) { 
      FxBox.showContent( $("commentTemplate").get("html"), e.target._opts); 
      $("fbContent").getElement("input[name=idComments]").value = e.target.rel;
    });
  });
  // Youtube
  $$(".ytvideo").each(function(i) {
    i._opts = {width: 475, height: 406};
    i.addEvent("click", GeneralFuncs.popupLinkClick);
  });

  // Destinations autocomplete
  if ($("buscaDestino")) {
    new Wick("destSearchText", {
      ajaxUrl: "/Generator/FreeTexts/FreeText_[l]_" + window.siteLang + "_[lang].json",
      iconField: "destSearchIcon",
      hiddenField: "IdSearcher",
      gaEventCategory: "Buscador",
      gaEventAction: "Free-Text Destinos"
    });
    $("buscaDestino").addEvent("submit", function(ev) {
      ev.stop();
      var wk = $("destSearchText").wick;
      if (!wk.valid) return false;
      var list = wk.collection.filter(function(i) { return i.I == $("IdSearcher").value;});
      if (list.length > 0) {
        var reg = list.pop();
        location.href = serverPath + "/{url}/".replace(/{url}/, reg.U);
      };
      return true;
    });
  }

	// Auto show-hide
	$$(".sh_hd").addEvent("click", function(ev) {
		ev.stop();
		this.className.split(" ").each(function(i) {
			if (i.test(/^sh:/)) $(i.match(/sh:(.*)/)[1]).removeClass("nodis");
			if (i.test(/^hd:/)) $(i.match(/hd:(.*)/)[1]).addClass("nodis");
		});
	});
	$$(".first_line").addEvent("click", function(ev) {ev.target.removeClass("first_line").set("title", "").removeEvents("click");});


	if ($("paypal-logo")) {
		$("paypal-logo").addEvent("click", function() {
			var lang = document.html.getAttribute("lang").substring(0, 2);
			window.open('https://www.paypal.com/' + lang + '/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside',
				'olcwhatispaypal',
				'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');
		});
	}

        $$(".ga_linked").each(GeneralFuncs.initGALinks);

        // Footer newsletter form initialization
        NewsletterForm.init();


  // -------------------------------------------------------------------------
  // Priority: Low
  $$("a[rel=external]").each(GeneralFuncs.externalize);

  var t1 = (new Date()).getTime();
  log("base.domready finished in " + (t1 - t0) + "msecs");
});

window.addEvent("load", function() {
  var t0 = (new Date()).getTime();
  log("load start delayed " + (t0 - T0) + "msecs");

  $$("img.bImg, img.sImg").each(function(i) {
    i.addEvent("error", imageError);
    if (!i.complete || i.naturalWidth==0) brokenImage(i);
  });
  if ($("errorMessage")) {
    setTimeout(function() {FxBox.showContent("<span class='error_msg'>" + $("errorMessage").get("html") + "</span>")}, 100);
  }
  if ($("flashtext")) {
    setTimeout(function() {FxBox.showContent("<span class='flash_text'>" + $("flashtext").get("html") + "</span>")}, 100);
  }

  window.picshow = new PicShow("picshow");

  // Booking access form tips
  FormTips.init("my_booking");
  
  $$(".njs").each(GeneralFuncs.destroy);

  if ($("photos")) {
    $$(".bImg").each(GeneralFuncs.fixImageHeight);
  }

  PhotoPopup.init();

  var t1 = (new Date()).getTime();
  log("base.load finished in " + (t1 - t0) + "msecs");
});

var NewsletterForm = {
  init: function() {
    NewsletterForm.form = $("sidebar_newsletter");
    if (NewsletterForm.form) {
      NewsletterForm.email = $("nl_email");
      NewsletterForm.accept = $("nl_accept");
      NewsletterForm.button = $("nl_button");
      
      if (NewsletterForm.email.value.trim() === '') {
        $("nl_lbl_email").addEvent("click", NewsletterForm.destroyLabel);
        NewsletterForm.email.addEvent("click", NewsletterForm.destroyLabel);
      } else {
        $("nl_lbl_email").destroy();
      }

      NewsletterForm.email.addEvent("keyup", NewsletterForm.validate);
      NewsletterForm.accept.addEvent("change", NewsletterForm.validate);
      NewsletterForm.form.addEvent("submit", NewsletterForm.submit);
      NewsletterForm.validate();
    }
  },
  destroyLabel: function(event) {
    NewsletterForm.email.removeEvent("click", NewsletterForm.destroyLabel);
    $("nl_lbl_email").destroy();
  },
  validate: function() {
    var error = false;
    if (!NewsletterForm.email.value.trim().test(/.+@.+\.[^\.]{2,3}$/)) {
      error = true;
    }
    if (!NewsletterForm.accept.checked) {
      error = true;
    }
    
    if (error) {
      NewsletterForm.button.set({ "disabled": "disabled", "src": ssp("{ssp}img/minisendd.gif") });
    } else {
      NewsletterForm.button.set({"disabled": "", "src": ssp("{ssp}img/minisend.gif") });
    }
  },
  submit: function(event) {
    var jsonRequest = new Request.JSON({
      url: "/info/newsletter.aspx",
      onSuccess: function(data) {
        NewsletterForm.form.reset();
        NewsletterForm.validate();
        FxBox.showContent("<span class='info_msg'>" + unescape(data.message) + "</span>")
      },
      onFailure: function(xhr) {
        // Performs the form submit
        NewsletterForm.form.submit();
      }
    }).post(NewsletterForm.form);

    return false;
  }
};

var PhotoPopup = {
  enabled: true,
  init: function() {
    var smallImages = $$('.sImg');
    if (smallImages === null || smallImages.length === 0) return

    this.layer = $('photoPopup');
    if (this.layer === null) {
      this.layer = new Element('div', { 'id':'photoPopup'});
      this.image = new Element('img');
      this.text  = new Element('div');
      
      this.layer.adopt(this.image);
      this.layer.adopt(this.text);
      $(document.body).adopt(this.layer);
    }
    smallImages.each(function(img) {
      img.addEvent('mouseover', PhotoPopup.onMouseOver);
      img.addEvent('mouseout', PhotoPopup.onMouseOut);
    });
  },
  clear: function() {
    $$('.sImg').each(function(img) {
      img.removeEvent('mouseover', PhotoPopup.onMouseOver);
      img.removeEvent('mouseout', PhotoPopup.onMouseOut);
    });
  },
  onMouseOver: function(event) {
    if (!PhotoPopup.enabled) return false;

    try {
      var url = $(event.target).src.replace('small/', '');

      // El popup no se debe mostrar cuando no hay foto, ni cuando el hotel es confidencial
      if (!url.contains("nofoto_sm.jpg") && !url.contains("selloconf.jpg")) {
        var pos = $(event.target).getPosition();
        var txt = $(event.target).alt;
        PhotoPopup.image.src = url;
        PhotoPopup.text.innerHTML = txt;
        PhotoPopup.layer.setStyles({
          left: pos.x + 75 + (Browser.Engine.trident ? 0 : 1),  /* Offsets are different for IE */
          top:  pos.y + (Browser.Engine.trident ? 0 : 1),
          opacity:0,
          display:'block'
        });
        PhotoPopup.layer.tween('opacity', 1);
      }
    } catch (err) {
      log(err);
    }
    return false;
  },
  onMouseOut: function(event) {
    PhotoPopup.