﻿
/*****************

  (c) 2006 Q42 B.V.

  The contents of this file, partially or in whole, may not be reproduced
  without prior written permission by Q42 B.V.

*****************/

function Accommodation()
{
	Spif.DOMEvents.attach(window, "load", this.doLoad, this);
}

var photoZoomDragger = null;

Accommodation.prototype =
{
  doLoad: function() {
    var accommodationMenuItems = document.getElementById("accommodation-menu").childNodes;
    for (var i = 0; i < accommodationMenuItems.length; i++)
      if (accommodationMenuItems[i].nodeType == 1)
      Spif.DOMEvents.attach(accommodationMenuItems[i].firstChild, "click", this.loadAccommodation);

    this.attachPhotosPreviewThumbsMouseover();

    // If the photos are already present, attach the zoom functions
    this.attachOpenZoomMethods();
    Spif.DOMEvents.attach(document.getElementById("zoom-close"), "click", this.closeZoom);
    Spif.DOMEvents.attach(document.body, "click", this.closeZoomHandler);

    //variables needed for dragging - mail-a-friend
    var dialog1 = document.getElementById("mail-a-friend-box");
    var handle1 = document.getElementById("mail-a-friend-dragger");
    new Dragger(dialog1, handle1);

    //variables needed for dragging - photozoom
    photoZoomDragger = new Dragger(document.getElementById("zoom-box"), document.getElementById("zoom-dragger"));

    // blur/return false the reserve button  
    Spif.DOMEvents.attach(document.getElementById('prices-and-availability-summary-submit'), "focus",
    function() {
      this.blur();
    });
    Spif.DOMEvents.attach(document.getElementById('prices-and-availability-summary-submit'), "click", function(evt) {
      if (/\bdisabled/.test(evt.subject.className)) {
        Spif.ClassNameAbstraction.replace(document.getElementById("prices-and-availability-summary-error"), "donotshow", "show");
        if (evt.preventDefault)
          evt.preventDefault();
        return false;
      }
    });

    var acco = resources.messages.clicktracker.ct_acco.replace('##name##', document.getElementById("accommodationId-box").getElementsByTagName('input')[1].value);
    ClickTracker.trackClick(new Click(acco));
  },

  loadProduct: function(evt, el, self, id) {
    if (!self)
      var self = this;

    var tabId = 'nav_accommodations';
    var boxId = 'accommodations';

    var tabEl = accommodation.deselectAllAndGetTab(tabId);
    var boxEl = accommodation.deselectAllAndGetBox(boxId);

    Spif.ClassNameAbstraction.replace(tabEl.parentNode, "deselected", "selected", Spif.Utils.FIREEVENTS);
    accommodation.toh = Spif.Utils.setTimeoutHandler(function() { Spif.ClassNameAbstraction.replace(boxEl, "deselected", "selected", Spif.Utils.FIREEVENTS); }, 0);

    Spif.ClassNameAbstraction.replace(boxEl, "empty", "loading");

    Spif.Utils.setTimeoutHandler(function() { accommodation.showLoadingIndicator(boxEl); }, 299);

    var productId = el.getAttribute('value');
    var accommodationId = document.getElementById("accommodationId-box").getElementsByTagName('input')[0].value;

    xmlhttp.request("/Accommodation/Subpage.aspx?accommodationId=" + accommodationId + "&subpageId=" + boxId + "&productId=" + productId, null, true, "accommodation.insertSubpage");
  },

  loadAccommodation: function(evt, el, self, id) {

    if (!self) {
      var self = this;
      if (evt)
        evt.preventDefault();
    }

    var tabId = self.id;
    var boxId = tabId.replace(/nav_/, "");

    var tabEl = accommodation.deselectAllAndGetTab(tabId);
    var boxEl = accommodation.deselectAllAndGetBox(boxId);

    Spif.ClassNameAbstraction.replace(tabEl.parentNode, "deselected", "selected", Spif.Utils.FIREEVENTS);
    accommodation.toh = Spif.Utils.setTimeoutHandler(function() { Spif.ClassNameAbstraction.replace(boxEl, "deselected", "selected", Spif.Utils.FIREEVENTS); }, 300);

    // document.getElementById('accommodation-tab-selection').value = boxId + (id == null ? "-0" : "-" + id);

    // if (Spif.ClassNameAbstraction.contains(boxEl, "empty") || /accommodations/.test(boxId))
    if (Spif.ClassNameAbstraction.contains(boxEl, "empty")) {
      Spif.ClassNameAbstraction.replace(boxEl, "empty", "loading");
      Spif.Utils.setTimeoutHandler(function() { accommodation.showLoadingIndicator(boxEl); }, 299);
      var accommodationId = document.getElementById("accommodationId-box").getElementsByTagName('input')[0].value;
      var boxName = self.href.substring(self.href.lastIndexOf("/") + 1);
      var tab = resources.messages.clicktracker.ct_acco_tab.replace('##name##', document.getElementById("accommodationId-box").getElementsByTagName('input')[1].value) + boxName;
      ClickTracker.trackClick(new Click(tab));

      xmlhttp.request("/Accommodation/Subpage.aspx?accommodationId=" + accommodationId + "&subpageId=" + boxId, null, true, "accommodation.insertSubpage");
    }

    return false;

  },
  loadSubpage: function(evt, el, self, id) {
    if (!self) {
      self = this;
      if (evt)
        evt.preventDefault();
    }

    var tabId = self.id;
    var boxId = tabId.replace(/nav_/, "");

    var tabEl = accommodation.deselectAllAndGetTab(tabId);
    var boxEl = accommodation.deselectAllAndGetBox(boxId);

    Spif.ClassNameAbstraction.replace(tabEl.parentNode, "deselected", "selected", Spif.Utils.FIREEVENTS);
    accommodation.toh = Spif.Utils.setTimeoutHandler(function() {
      Spif.ClassNameAbstraction.replace(boxEl, "deselected", "selected", Spif.Utils.FIREEVENTS);
    }, 300);

    // if (Spif.ClassNameAbstraction.contains(boxEl, "empty") || /accommodations/.test(boxId))
    if (Spif.ClassNameAbstraction.contains(boxEl, "empty")) {
      Spif.ClassNameAbstraction.replace(boxEl, "empty", "loading");
      Spif.Utils.setTimeoutHandler(function() { accommodation.showLoadingIndicator(boxEl); }, 299);
      var accommodationId = document.getElementById("accommodationId-box").getElementsByTagName('input')[0].value;
      xmlhttp.request(
			  "/Accommodation/Subpage.aspx?accommodationId=" + accommodationId + "&subpageId=" + boxId,
			  null, true, "accommodation.insertSubpage");
    }

    return false;

  },

  insertSubpage: function() {
    var req = xmlhttp.req["accommodation.insertSubpage"];
    if (req.readyState != 4)
      return;
    if (req.status != 200)
      return;

    var response = req.responseXML.documentElement;
    var subpageId = response.getElementsByTagName("id")[0].firstChild.nodeValue;
    var content = response.getElementsByTagName("content")[0].firstChild.nodeValue;
    var boxes = document.getElementById("subpages-box").childNodes;
    var thisBox;

    for (var i = 0; i < boxes.length; i++) {
      if (boxes[i].nodeType == 1) {
        if (boxes[i].id.indexOf(subpageId) != -1) {
          thisBox = boxes[i];
          break;
        }
      }
    }

    if (thisBox != null) {
      clearTimeout(accommodation.toh);
      accommodation.toh = null;
      Spif.ClassNameAbstraction.remove(thisBox, "loading");

      if (Spif.ClassNameAbstraction.contains(thisBox, "selected")) {
        Spif.Utils.setTimeoutHandler(function() {
          Spif.ClassNameAbstraction.replace(thisBox, "selected", "deselected", Spif.Utils.FIREEVENTS);
        }, 0);
      }

      Spif.Utils.setTimeoutHandler(function() { thisBox.innerHTML = content; }, 300);
      Spif.Utils.setTimeoutHandler(function() {
        Spif.ClassNameAbstraction.replace(thisBox, "deselected", "selected", Spif.Utils.FIREEVENTS);
      }, 400);
      Spif.Utils.setTimeoutHandler(accommodation.attachOpenZoomMethods, 400);
      Spif.Utils.setTimeoutHandler(accommodation.attachPhotosPreviewThumbsMouseover, 1500);

      if (subpageId == "accommodations") {
        Spif.Utils.setTimeoutHandler(pricesAndAvailability.updateAccommodationSelection, 500);
      }

      if (subpageId == "map") {
        Spif.Utils.setTimeoutHandler(GoogleMapDetail.load, 500);
      }
    }
  },

  // currently not in use
  showLoadingIndicator: function(boxEl) {
    if (boxEl) {
      boxEl.innerHTML = '<p class="loading-text">' + resources.messages.loading + '...</p>';
    }
  },

  // Select the tab and returns that element (21.11.2006 Bart)
  deselectAllAndGetTab: function(tabId) {
    tabEl = document.getElementById(tabId);
    var accommodationMenuItems = document.getElementById("accommodation-menu").childNodes;
    for (var i = 0; i < accommodationMenuItems.length; i++) {
      if (accommodationMenuItems[i].nodeType == 1) {
        Spif.ClassNameAbstraction.replace(accommodationMenuItems[i], "selected", "deselected", Spif.Utils.FIREEVENTS);
      }
    }
    return tabEl;
  },


  // Select the box and returns that element (21.11.2006 Bart)
  deselectAllAndGetBox: function(boxId) {

    clearTimeout(accommodation.toh);
    accommodation.toh = null;

    var boxes = document.getElementById("subpages-box").childNodes;
    for (var i = 0; i < boxes.length; i++) {
      if (boxes[i].nodeType == 1) {
        Spif.ClassNameAbstraction.replace(boxes[i], "selected", "deselected", Spif.Utils.FIREEVENTS);
        if (boxes[i].id.indexOf(boxId) != -1) {
          boxEl = boxes[i];
        }
      }
    }
    return boxEl;
  },


  loadThumb: function(evt) {
    if (!evt || !evt.subject || !evt.subject.src)
      return;

    var fileNameStartPos = evt.subject.src.lastIndexOf("/") + 1;
    var fileName = evt.subject.src.substr(fileNameStartPos);

    var el = document.getElementById("photos-preview-large");

    el.src = el.src.substr(0, el.src.lastIndexOf("/") + 1) + fileName;
    el.alt = evt.subject.alt;
    el.title = evt.subject.alt;

    var newPhotomatches = evt.subject.className.match(/\bphoto-\d+\b/);
    if (newPhotomatches)
      var newPhoto = newPhotomatches[0];

    var zoom = document.getElementById("general-preview-zoom");
    var oldPhotomatches = zoom.className.match(/\bphoto-\d+\b/);
    if (oldPhotomatches) {
      Spif.ClassNameAbstraction.replace(zoom, oldPhotomatches[0], newPhoto);
      Spif.ClassNameAbstraction.replace(el, oldPhotomatches[0], newPhoto);
    }
  },

  attachOpenZoomMethods: function() {
    // General panel
    var generalPreviewZoom = document.getElementById("general-preview-zoom");
    if (generalPreviewZoom != null)
      Spif.DOMEvents.attach(generalPreviewZoom, "click", accommodation.openZoom);
    var photosPreviewLarge = document.getElementById('photos-preview-large');
    if (photosPreviewLarge) {
      Spif.DOMEvents.attach(photosPreviewLarge, "click", accommodation.openZoom);
      if (navigator.userAgent.indexOf("MSIE 5.5") == -1) {
        Spif.DOMEvents.attach(photosPreviewLarge, "mouseover", function() { this.style.cursor = 'pointer'; });
        Spif.DOMEvents.attach(photosPreviewLarge, "mouseout", function() { this.style.cursor = ''; });
      }
    }

    // Photos panel
    var photosBox = document.getElementById("photos");
    if (photosBox != null) {
      var links = photosBox.getElementsByTagName("a");
      for (var i = 0; i < links.length; i++)
        if (Spif.ClassNameAbstraction.contains(links[i], "zoom"))
        Spif.DOMEvents.attach(links[i], "click", accommodation.openZoom);
    }

    // Area panel
    var areaPhotoZoom = document.getElementById("area-photo-zoom");
    if (areaPhotoZoom != null)
      Spif.DOMEvents.attach(areaPhotoZoom, "click", accommodation.openZoom);

    // Photos subpanel in Accommodations panel
    var photosBox = document.getElementById("accommodations-photos");
    if (photosBox != null) {
      var links = photosBox.getElementsByTagName("a");
      for (var i = 0; i < links.length; i++) {
        if (Spif.ClassNameAbstraction.contains(links[i], "zoom"))
          Spif.DOMEvents.attach(links[i], "click", accommodation.openZoom);
      }
    }
  },
  openZoom: function(evt) {
    evt.preventDefault();
    var src = document.getElementById("zoom-iframe-src").value;
    var photomatches = this.className.match(/\bphoto-(\d+)\b/);
    var productmatches = this.className.match(/\bproduct-\d+\b/);

    if (photomatches) {
      Spif.Utils.setTimeoutHandler(function() {
        var product = productmatches ? product = parseInt(productmatches[0].substring(8)) : "";
        var photo = parseInt(photomatches[0].substring(6));

        if (Spif.ClassNameAbstraction.contains(document.getElementById("zoom-box"), "zoom-hidden")) {
          if (document.getElementById("prices-and-availability-number-of-people-value") && document.getElementById("prices-and-availability-number-of-people-select"))
            document.getElementById("prices-and-availability-number-of-people-value").innerHTML = " " + document.getElementById("prices-and-availability-number-of-people-select").options[document.getElementById("prices-and-availability-number-of-people-select").selectedIndex].value;
          Spif.ClassNameAbstraction.replace(document.getElementById("zoom-box"), "zoom-hidden", "zoom-shown");
          Spif.ClassNameAbstraction.add(document.body, "hideSelectBoxen");
        }

        Spif.Utils.setTimeoutHandler(function() {
          document.getElementById("zoom-iframe").contentWindow.photoZoom.reloadMe(src + "?photo=" + photo + "&product=" + product);
        }, 500);

        accommodation.movePopupBelowScroll();
      }, 0);
    }
    return false;
  },
  movePopupBelowScroll: function() {
    setTimeout(function() {
      //Always reposition scroll window
      document.getElementById('zoom-box').style.top = (100 + document.documentElement.scrollTop) + 'px';
    }, 500);
  },
  openProductZoom: function(evt) {
    evt.preventDefault();
    var src = document.getElementById("zoom-iframe-src").value;
    var photomatches = this.className.match(/\bphoto-\d+\b/);
    if (photomatches)
      var photo = parseInt(photomatches[0].substring(6));
    var productmatches = this.className.match(/\bproduct-\d+\b/);
    if (productmatches)
      var product = parseInt(productmatches[0].substring(8));
    Spif.ClassNameAbstraction.replace(document.getElementById("zoom-box"), "zoom-hidden", "zoom-shown");
    document.getElementById("zoom-iframe").contentWindow.photoZoom.reloadMe(src + "?photo=" + photo + "&product=" + product);
    return false;
  },
  closeZoomHandler: function(event) {
    if (Spif.ClassNameAbstraction.contains(document.getElementById("zoom-box"), "zoom-shown")) {
      var el = event.subject;
      while (el != document.body) {
        if (el.id == "zoom-box")
          return;
        el = el.parentNode;
      }

      accommodation.closeZoom();
    }
  },
  closeZoom: function() {
    Spif.ClassNameAbstraction.replace(document.getElementById("zoom-box"), "zoom-shown", "zoom-hidden");
    document.body.className = document.body.className.replace(/hideSelectBoxen/, '');

    document.getElementById("zoom-iframe").contentWindow.photoZoom.reloadMe(document.getElementById("zoom-iframe-src").value);
  },

  openMAFScreen: function(number) {
    document.getElementById("mail-a-friend-box").style.display = "block";
    window.frames["mail-a-friend-box"].document.getElementById("accommodation-id").value = number;
    return false;
  },

  closeMAFScreen: function() {
    document.getElementById("mail-a-friend-frame").src = "/Accommodation/MailAFriend.aspx";
    document.getElementById("mail-a-friend-box").style.display = "none";
  },

  attachPhotosPreviewThumbsMouseover: function() {
    var photosPreviewThumbs = document.getElementById("photos-preview-thumbs");
    if (photosPreviewThumbs != null) {
      var accommodationThumbs = photosPreviewThumbs.childNodes;
      for (var i = 0; i < accommodationThumbs.length; i++)
        if (accommodationThumbs[i].nodeType == 1)
        Spif.DOMEvents.attach(accommodationThumbs[i], "mouseover", accommodation.loadThumb);
    }
  },

  expandFirstSubAccommodationPanel: function() {
    if (document.getElementById('accommodation_detail_sections') != null) {
      var sections = document.getElementById('accommodation_detail_sections').getElementsByTagName('li');
      for (i = 0; i < sections.length; i++) {
        var s = sections[i];
        if (i == 0 && Spif.ClassNameAbstraction.contains(s, "panel-collapsed")) {
          Spif.ClassNameAbstraction.replace(s, "panel-collapsed", "panel-expanded");
          break;
        }
      }
    }
  },

  expandAll: function(el) {
    var id = el.parentNode.id.replace('toggle_', '');
    var allEl = document.getElementById(id);
    var lis = allEl.getElementsByTagName('li');
    for (var i = 0; i < lis.length; i++) {
      Spif.ClassNameAbstraction.replace(lis[i], "panel-collapsed", "panel-expanded");
    }
    el.innerHTML = resources.messages.collapseAll;
    el.onclick = function() { accommodation.collapseAll(el); };
    el.blur();
    return false;
  },

  collapseAll: function(el) {
    var id = el.parentNode.id.replace('toggle_', '');
    var allEl = document.getElementById(id);
    var lis = allEl.getElementsByTagName('li');
    for (var i = 0; i < lis.length; i++) {
      Spif.ClassNameAbstraction.replace(lis[i], "panel-expanded", "panel-collapsed");
    }
    el.innerHTML = resources.messages.expandAll;
    el.onclick = function() { accommodation.expandAll(el); };
    el.blur();
    return false;
  },

  printPage: function() {
    var paaa = document.getElementById("prices-and-availability-accommodations");
    var radios = paaa.getElementsByTagName("input");
    for (var i = 0; i < radios.length; i++) {
      if (radios[i].checked) {
        break;
      }
    }
    var selected_productid = document.getElementById(radios[i].id).value;
    var printWindow = window.open("?print=1&productid=" + selected_productid, "printWindow", "height=500,width=700,scrollbars=yes,resizable=yes,menubar=yes");
    if (!printWindow) {
      alert("Uw browser staat niet toe dat het printvenster geopend wordt.\n" +
        "Verander a.u.b. de instellingen van uw browser zodat popups mogelijk zijn.")
      return false;
    }
    //printWindow.document.title = "Print";
    return false;
  },

  highlightSecondaryContent: function() {
    Spif.Utils.setTimeoutHandler(function() {
      var box = document.getElementById("content-box");
      if (box.className == 'content-box morph')
        Spif.ClassNameAbstraction.replace(box, "content-box", "content-box-highlighted", Spif.Utils.FIREEVENTS);
      else
        Spif.ClassNameAbstraction.replace(box, "normal", "highlighted", Spif.Utils.FIREEVENTS);
    }, 0);
    Spif.Utils.setTimeoutHandler(function() {
      Spif.ClassNameAbstraction.replace(document.getElementById("content-box"), "highlighted", "normal", Spif.Utils.FIREEVENTS);
    }, 500);
    return false;
  }
}

var accommodation = new Accommodation();
