﻿// Aquapura Hotels and Villas - Javascript
//var MyNextAreaId = "0";
var ELEMENT_NODE = 1;
var options = { path: '/', expires: 365 };

// ChangeLanguage
try {
  // Internet Explorer
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
catch(e) {
  try {
    // Other Browsers
    xmlDoc = document.implementation.createDocument("","",null);
  }
  catch(e) {
    alert(e.message);
  }
}
var xmlObj;
var global_DestinationLang;
var global_TheCurrentAreaId;

function loadXML(xmlFile)  {
  xmlDoc.async="false";
  if (window.ActiveXObject) {
    xmlDoc.onreadystatechange = verify;
    xmlDoc.load(xmlFile);
    xmlObj = xmlDoc.documentElement;
  }
  else {

    window.location = "Default.aspx?areaID=" + MyNextAreaId;
    //xmlDoc.onload = ProcessXml;
    //xmlDoc.load(xmlFile);
    //xmlDox.load("http://aquapura.srv2.webdotehosting.com/files/system/aquapura/labels.xml");
  }
}

function verify() { 
  if (xmlDoc.readyState != 4) { 
    return false;
  }
}

function ProcessXml() {
  //xmlObj = xmlDoc.documentElement;
  var labels = xmlDoc.getElementsByTagName('label');
  alert(labels.length);
  for (i=0; i < labels.length; i++) {
    for (j=0; j < labels[i].childNodes.length; j++) {
      if (labels[i].childNodes[j].nodeType != ELEMENT_NODE) continue;
      var cdata = document.createTextNode(labels[i].childNodes[j].firstChild.nodeValue);
      alert(cdata);
    }
  }

  if (CurrentAreaID != TheCurrentAreaID && DestinationLang == 'pt') {
    if (global_DestinationLang == "pt") {
      TheAreaID = 1;
    }
    else if (global_DestinationLang == "es") {
      TheAreaID = 3;
    }
    else {  // en is the standard language
      TheAreaID = 4;
    }
    window.location = "?AreaID=" + TheAreaID;
  }
}

function ChangeLanguage(OriginID, DestinationLang, TheCurrentAreaID) {
  global_DestinationLang = DestinationLang;
  global_TheCurrentAreaId = TheCurrentAreaID;

  var TheAreaID = 1;
  if (DestinationLang == "pt") {
    TheAreaID = 1;
  }
  else if (DestinationLang == "es") {
    TheAreaID = 3;
  }
  else {  // en is the standard language
    TheAreaID = 4;
  }

  if (CurrentAreaID != TheAreaID && DestinationLang != "es") {
    var PagePosition = new Array();
    
    // Get Current Page's Position
    MyNextAreaId = TheAreaID;
    loadXML('/admin/public/getnavigationxml.aspx?ID=' + OriginID + '&Expand=1');
    
    for (levelOne=1 ; levelOne<xmlObj.childNodes.length ; levelOne++) {
      if (xmlObj.childNodes(levelOne).getAttribute("InPath") == "True") {
        
        // Saves reference of the first level page
        PagePosition[0] = xmlObj.childNodes(levelOne).getAttribute("Sort");
        
        // If this is not the Active page, looks in the subpages
        if (xmlObj.childNodes(levelOne).getAttribute("Active") == "False") {
          for (levelTwo=0 ; levelTwo<xmlObj.childNodes(levelOne).childNodes.length ; levelTwo++) {
            if (xmlObj.childNodes(levelOne).childNodes(levelTwo).getAttribute("InPath") == "True") {
              
              // Saves reference of the second level page
              PagePosition[1] = xmlObj.childNodes(levelOne).childNodes(levelTwo).getAttribute("Sort") - 1;
              
              // If this is not the Active page, looks in the subpages
              if (xmlObj.childNodes(levelOne).childNodes(levelTwo).getAttribute("Active") == "False") {
                for (levelThree=0 ; levelThree<xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes.length ; levelThree++) {
                  if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).getAttribute("InPath") == "True") {
                    
                    // Saves reference of the third level page
                    PagePosition[2] = xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).getAttribute("Sort") - 1;
                    
                    // If this is not the Active page, looks in the subpages
                    if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).getAttribute("Active") == "False") {
                      for (levelFour=0 ; levelFour<xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes.length ; levelFour++) {
                        if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).getAttribute("InPath") == "True") {
                          
                          // Saves reference of the forth level page
                          PagePosition[3] = xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).getAttribute("Sort") - 1;
                          
                          // If this is not the Active page, looks in the subpages
                          if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).getAttribute("Active") == "False") {
                            for (levelFive=0 ; levelFive<xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes.length ; levelFive++) {
                              if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).getAttribute("Active") == "True") {
                                
                                // Saves reference of the fifth level page
                                PagePosition[4] = xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).getAttribute("Sort") - 1;
                                
                                // If this is not the Active page, looks in the subpages
                                if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).getAttribute("Active") == "False") {
                                  for (levelSix=0 ; levelSix<xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).childNodes.length ; levelSix++) {
                                    if (xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).childNodes(levelSix).getAttribute("Active") == "True") {
  
                                      // Saves reference of the sixth level page
                                      PagePosition[5] = xmlObj.childNodes(levelOne).childNodes(levelTwo).childNodes(levelThree).childNodes(levelFour).childNodes(levelFive).childNodes(levelSix).getAttribute("Sort") - 1;
                                      
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    // End of current page's position tracking

    // Get destination's href
    loadXML('/admin/public/getnavigationxml.aspx?AreaID=' + TheAreaID);
    
    if (PagePosition.length == 1) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).getAttribute("FriendlyHref");
    }
    else if (PagePosition.length == 2) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).childNodes(PagePosition[1]).getAttribute("FriendlyHref");
    }
    else if (PagePosition.length == 3) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).childNodes(PagePosition[1]).childNodes(PagePosition[2]).getAttribute("FriendlyHref");
    }
    else if (PagePosition.length == 4) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).childNodes(PagePosition[1]).childNodes(PagePosition[2]).childNodes(PagePosition[3]).getAttribute("FriendlyHref");
    }
    else if (PagePosition.length == 5) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).childNodes(PagePosition[1]).childNodes(PagePosition[2]).childNodes(PagePosition[3]).childNodes(PagePosition[4]).getAttribute("FriendlyHref");
    }
    else if (PagePosition.length == 6) {
      TheFriendlyHref = xmlObj.childNodes(PagePosition[0]).childNodes(PagePosition[1]).childNodes(PagePosition[2]).childNodes(PagePosition[3]).childNodes(PagePosition[4]).childNodes(PagePosition[5]).getAttribute("FriendlyHref");
    }
  
    window.location = TheFriendlyHref;

  }
  else if (DestinationLang != 'pt' && DestinationLang != 'en') {
    alert("Brevemente!");
  }
}


// My onLoad function
function onloadEvents() {
  try {
    //Try to load Google Maps. If it cannot plays the audio - REVIEW THIS CODE BY DIOGO
    //load();
  }
  catch(err) {
  }
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

function getCookie_v2(c_name) {
/*
  AquapuraSound=getCookie(c_name);
  if (AquapuraSound == "" || AquapuraSound == "1") {
    AquapuraSound = "1";
  } else {
    AquapuraSound = "0";
  }
  setCookie(c_name,AquapuraSound,365);
  return AquapuraSound;*/
  AquapuraSound = $.cookie(c_name);
  if (AquapuraSound == null) {
    $.cookie(c_name, 1, options);
    AquapuraSound = 1;
  }
  return AquapuraSound;
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie() {
  AquapuraSound=getCookie('AquapuraSoundCookie');
  if (AquapuraSound == ""){
    AquapuraSound = "1";
    setCookie('AquapuraSoundCookie',"1",365);
    setSoundOn();
//    document.sound1.Play();
  }
  else {
    if (AquapuraSound == "1") {
      setSoundOn();
    }
    else {
      setSoundOff();
//      document.getElementById("tooglesoundon").style.display = "inline";
//      document.getElementById("tooglesoundoff").style.display = "none";
    }
  }
}
function setSoundOff() {
//  document.sound1.Stop();
  setCookie('AquapuraSoundCookie',"0",365);
  $("#Sound").html("");
  $("#SoundLink img").attr("src", "Files/Navigation/aquapura/SupportMenus/SoundOff.gif");
  $("#SoundLink img").attr("alt", "Sound off");
//  document.getElementById("tooglesoundon").style.display = "inline";
//  document.getElementById("tooglesoundoff").style.display = "none";
}
function setSoundOn() {
//  document.sound1.Play();
  setCookie('AquapuraSoundCookie',"1",365);
  $("#Sound").flashembed("/Files/System/aquapura/diversos/audio.swf");
  $("#SoundLink img").attr("src", "Files/Navigation/aquapura/SupportMenus/SoundOn.gif");
  $("#SoundLink img").attr("alt", "Sound on");
//  document.getElementById("tooglesoundoff").style.display = "inline";
//  document.getElementById("tooglesoundon").style.display = "none";
}



// PopUp Functions
var originalDocumentWidth;

function MyShowPopup(ThePopupTitle, DescriptTitle, Description) {
  PopUpContent = "<br\/><strong>" + DescriptTitle + "<\/strong><br\/><br\/>" + Description;

  document.getElementById('PopUpTitleArea').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width=450 height=20><param name="movie" value="Files/System/aquapura/flash/titulo_pagina.swf"><param name="quality" value="high"><param name="menu" value="false"><param name="FlashVars" value="pagetitle=' + ThePopupTitle + '"><param name="wmode" value="Transparent" /><embed src="Files/System/aquapura/flash/titulo_pagina.swf" quality="high" pluginspage="https://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" menu="false"  width=450 height=20 wmode="Transparent"  FlashVars="pagetitle=' + ThePopupTitle + '"><\/embed><\/object>'
  document.getElementById('PopUpContentText').innerHTML = PopUpContent;
  showPopup();
}

function showPopup() {
  
  // Get source HTML structure
  var el = document.getElementById('popupMask');
  var elContent = document.getElementById('popupContent');
  
  // Store input dimensions
  originalDocumentWidth = document.documentElement.scrollWidth;
  
  // Set dimensions
  setPopupDimensions();
  
  // Set visibility
  el.style.display = '';
  elContent.style.display = '';
  
  // Detect resizes
  window.onresize = setPopupDimensions;
  window.onscroll = setPopupDimensions;
}

function setPopupDimensions() {

  // Get source HTML structure
  var el = document.getElementById('popupMask');
  var elContent = document.getElementById('popupContent');
  
  // Get scroll position
  var scrollY = document.documentElement.scrollTop;
  var scrollX = document.documentElement.scrollLeft;
  
  // Get document dimensions
  var documentHeight = document.body.clientHeight > document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight;
  var documentWidth = originalDocumentWidth < document.documentElement.scrollWidth ? originalDocumentWidth : document.documentElement.scrollWidth;
  
  // Get browser dimensions
  if(typeof(window.innerWidth) == 'number') {
    //Non-IE
    myInnerWidth = window.innerWidth;
    myInnerHeight = window.innerHeight;
  }
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myInnerWidth = document.documentElement.clientWidth;
    myInnerHeight = document.documentElement.clientHeight;
  }
  else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    myInnerWidth = document.body.clientWidth;
    myInnerHeight = document.body.clientHeight;
  }
  
  if(documentWidth < myInnerWidth)
    documentWidth = myInnerWidth;
  
  // Set dimensions of background mask
  el.style.width = document.body.clientWidth + 'px';  //myInnerWidth + 'px';
  el.style.height = document.body.clientHeight + 'px';  //myInnerHeight + 'px';
  el.style.top = 0;
  el.style.left = 0;
  
  // Set dimensions of content
  height = myInnerHeight - 30;
  width = myInnerWidth - 30;
  elContent.style.top =  (myInnerHeight / 2 - 185) + 'px';  //(scrollY + myInnerHeight / 2 - height / 2 - 20 / 2) + 'px';
  elContent.style.left =  (myInnerWidth / 2 - 432 / 2) + 'px';   //(scrollX + myInnerWidth / 2 - width / 2) + 'px';
}

function cancelPopup() {
  // Hide view
  document.getElementById('popupMask').style.display = 'none';
  document.getElementById('popupContent').style.display = 'none';
  
  // Don't handle resizes
  window.onresize = null;
  window.onscroll = null;
}

function toggleSound(NewValue) {
//  setCookie('AquapuraSoundCookie',NewValue,365);
  $.cookie('AquapuraSoundCookie_v2', NewValue, options);
}

function CheckForSound(PageId, HotelPageId) {
  CookieValue = getCookie_v2('AquapuraSoundCookie_v2');
  if(PageId == 1 || PageId == 218) {
    $("#DwSupportMenu li:first").before('<li id="SoundLink"></li>');
    $("#SoundLink").flashembed({
      src: 'Files/System/aquapura/flash/sound_button.swf',
      width: '46',
      height: '20',
      wmode: 'transparent'
    },
    {
      soundPath: '/Files/System/aquapura/flash/homepage_optimizado.mp3',
      soundStatus: CookieValue
    });
  }
  if(PageId == "6" || PageId == "227") {
    $("#DwSupportMenu li:first").before('<li id="SoundLink"></li>');
    $("#SoundLink").flashembed({
      src: 'Files/System/aquapura/flash/sound_button.swf',
      width: '46',
      height: '20',
      wmode: 'transparent'
    },
    {
      soundPath: '/Files/System/aquapura/flash/hotel.mp3',
      soundStatus: CookieValue
    });
    $("#RightLogos").show();
  }
}

function CheckLogos(PageId) {
  if(PageId == "6") {
    $("#RightLogos").show();
  }
  else {
    $("#RightLogos").remove();
  }
}
