var highlight = {
  loadingPanel: null,
  initialize: function () {
    var propertyHighlightTriggerElements = yuiDom.getElementsByClassName('propertyHighlight','a');
    propertyHighlightTriggerElements.forEach(function(triggerElement) {
      var propertyId = triggerElement.getAttribute("propertyid");
      triggerElement.config = {
        propertyId: propertyId
      }
      yuiEvent.addListener(triggerElement,"click",highlight.getHighlight);
    });
  },
  createPanel: function() {
    var propertyHighlightPanel = document.createElement("div");
    yuiDom.addClass(propertyHighlightPanel, "propertyHighlightPanel");

    var headerContainer = document.createElement("div");
    yuiDom.addClass(headerContainer,"hd");

    var header = document.createElement("h4");
    headerContainer.appendChild(header);

    propertyHighlightPanel.appendChild(headerContainer);

    var bodyContainer = document.createElement("div");
    yuiDom.addClass(bodyContainer,"bd");

    var tabList = document.createElement("ul");
    yuiDom.addClass(tabList, "propertyHighlightSectionTabs");

    var hotelTabListItem = document.createElement("li");
    hotelTabListItem.innerHTML = highlight.labels.hotelTabText;
    tabList.appendChild(hotelTabListItem);

    var featuresTabListItem = document.createElement("li");
    featuresTabListItem.innerHTML = highlight.labels.featuresTabText;
    tabList.appendChild(featuresTabListItem);

    var offersTabListItem = document.createElement("li");
    offersTabListItem.innerHTML = highlight.labels.offersTabText;
    tabList.appendChild(offersTabListItem);

    bodyContainer.appendChild(tabList);

    var localInformationContainer = document.createElement("div");
    yuiDom.addClass(localInformationContainer,"localInformation")

    var longitudeLatitude = document.createElement("div");
    yuiDom.addClass(longitudeLatitude,"longitudeLatitude");
    localInformationContainer.appendChild(longitudeLatitude);

    var localTime = document.createElement("div");
    yuiDom.addClass(localTime,"localTime");
    localInformationContainer.appendChild(localTime);

    bodyContainer.appendChild(localInformationContainer);

    var hotelFrameContainer = document.createElement("div");
    yuiDom.addClass(hotelFrameContainer, "hotelFrameContainer");
    yuiDom.addClass(hotelFrameContainer, "propertyHighlightFrameContainer");
    bodyContainer.appendChild(hotelFrameContainer);

    var featuresFrameContainer = document.createElement("div");
    yuiDom.addClass(featuresFrameContainer, "featuresFrameContainer");
    yuiDom.addClass(featuresFrameContainer, "propertyHighlightFrameContainer");
    bodyContainer.appendChild(featuresFrameContainer);

    var offersFrameContainer = document.createElement("div");
    yuiDom.addClass(offersFrameContainer, "offersFrameContainer");
    yuiDom.addClass(offersFrameContainer, "propertyHighlightFrameContainer");
    bodyContainer.appendChild(offersFrameContainer);

    propertyHighlightPanel.appendChild(bodyContainer);

    return {
      main: propertyHighlightPanel,
      header: header,
      localTime: localTime,
      longitudeLatitude: longitudeLatitude,
      hotel:{tab: hotelTabListItem, frameContainer: hotelFrameContainer, url: "/lemeridien/property/propertyHighlights/hotel.html?propertyID="+this.config.propertyId},
      features: {tab: featuresTabListItem, frameContainer: featuresFrameContainer, url: "/lemeridien/property/propertyHighlights/features.html?propertyID="+this.config.propertyId},
      offers: {tab: offersTabListItem, frameContainer: offersFrameContainer, url: "/lemeridien/property/propertyHighlights/offers.html?propertyID="+this.config.propertyId}
    };

  },
  flashGetHighlightBridge: function(propertyId) {
    if(highlight.properties&&highlight.properties[propertyId]) {
      highlight.getHighlight.apply({config:highlight.properties[propertyId]});
    }
    else {
      if(!highlight.properties) {
        highlight.properties = {};
      }
      var property = {
                         propertyId: propertyId
                       };
      highlight.properties[propertyId] = property;
      highlight.getHighlight.apply({config:property});
    }
  },
  getHighlight: function (e) {
    if(!this.config.panel) {
      this.config.panelConfig = highlight.createPanel.apply(this);
      var panel = new YAHOO.widget.Panel(this.config.panelConfig.main,
        {
          close:true,
          visible:false,
          modal:true,
          constraintoviewport:true,
          fixedcenter: true,
          effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
        }
      );

      panel.render(document.body);
      this.config.panel = panel;
    }
    if(highlight.loadingPanel == null) {
      highlight.loadingPanel =  new YAHOO.widget.Panel("propertyHighlightLoadingPanel",
        {
          close:false,
          visible:false,
          modal:true,
          constraintoviewport:true,
          fixedcenter: true,
          effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
        }
      );
      highlight.loadingPanel.setBody(highlight.labels.loadingText);
      highlight.loadingPanel.render(document.body);
    }
    highlight.loadingPanel.show();
    if(!this.config.loaded) {
      yuiConnect.asyncRequest("get",
        "/ajax/property?propertyID="+this.config.propertyId+"&localeCode=en_US&content=localInformation|plot",
        {success: highlight.setHighlight, argument: {triggerElement: this}});
    }
    else {
      highlight.showInit.apply(this);
    }

    if (e) {
      if (typeof e.preventDefault != 'undefined')  {
        e.preventDefault();
      }                        // W3C
      else {
        e.returnValue = false;
      }                     // IE
    }
    return false;
  },
  setHighlight: function (xhr) {
    var triggerElement = xhr.argument.triggerElement;

    triggerElement.config.panelConfig.header.innerHTML = "<a href=\"/lemeridien/property/overview/index.html?propertyID="+triggerElement.config.propertyId+"\">"+xhr.responseXML.getElementsByTagName("propertyName")[0].firstChild.nodeValue+"</a>";

    var latitude = xhr.responseXML.getElementsByTagName("latitude")[0].firstChild.nodeValue;
    var longitude = xhr.responseXML.getElementsByTagName("longitude")[0].firstChild.nodeValue;
    var latLongConvertion = highlight.convertLatLong(latitude,longitude);
    triggerElement.config.panelConfig.longitudeLatitude.innerHTML = "&nbsp;| " + latLongConvertion.latitude + " - " + latLongConvertion.longitude;

    triggerElement.config.panelConfig.localTime.innerHTML = xhr.responseXML.getElementsByTagName("localTime")[0].firstChild.nodeValue;

    yuiEvent.addListener(triggerElement.config.panelConfig.hotel.tab,"click",highlight.tabs.show, triggerElement.config.panelConfig.hotel,triggerElement);
    yuiEvent.addListener(triggerElement.config.panelConfig.features.tab,"click",highlight.tabs.show, triggerElement.config.panelConfig.features,triggerElement);
    yuiEvent.addListener(triggerElement.config.panelConfig.offers.tab,"click",highlight.tabs.show, triggerElement.config.panelConfig.offers,triggerElement);

    triggerElement.config.loaded = true;
    highlight.showInit.apply(triggerElement);
  },
  showInit: function() {
    highlight.loadingPanel.hide();
    if(!this.config.activeTabConfig) {
      this.config.activeTabConfig = this.config.panelConfig.hotel;
    }
    highlight.tabs.show.apply(this, [null,this.config.activeTabConfig]);
    this.config.panel.show();
    setTimeout(function(){yuiDom.addClass(document.body, "masked")}, 2000);
  },
  tabs: {
    show: function(e,tabConfig) {
      highlight.tabs.closeActive.apply(this);
      if(!tabConfig.loaded) {
        highlight.iFrame.load.apply(this,[tabConfig]);
        tabConfig.loaded = true;
      }
      this.config.activeTabConfig = tabConfig;
      tabConfig.frameContainer.style.display = "block";
    },
    closeActive: function () {
      yuiDom.get(this.config.activeTabConfig.frameContainer).style.display = "none";
    }
  },
  iFrame: {
    load: function(tabConfig) {
      var iFrame = document.createElement("iframe");
      var dateIdentifier = new Date();
      var frameIdName = "frame_"+this.config.propertyId+"_"+(dateIdentifier.getHours()+""+dateIdentifier.getMinutes()+""+dateIdentifier.getSeconds());
      iFrame.setAttribute("name",frameIdName);
      iFrame.setAttribute("id",frameIdName);
      iFrame.frameBorder = "0";
      iFrame.style.border = "none";
      iFrame.scrolling = "no";
      iFrame.height = "400";
      yuiDom.addClass(iFrame,"propertyHighlightIFrame")
      highlight.currentFrame = iFrame;
      iFrame.src  = tabConfig.url;
      tabConfig.frameContainer.appendChild(iFrame);
      if (window.frames[iFrame.name]) {
        window.frames[iFrame.name].location = tabConfig.url;
        return iFrame;
      }
      return null;
    },
    setHeight: function() {
      if (highlight.currentFrame) {
        highlight.currentFrame.style.height = "auto";
        var height = highlight.iFrame.getDocumentHeight(window.frames[highlight.currentFrame.name].document);
        var realHeight = Math.max(height, highlight.currentFrame.height)
        if (height) {
          highlight.currentFrame.style.height = height + 20 + "px";
        }
      }
    },
    getDocumentHeight: function(frameDocument) {
      var documentHeight = 0, scrollHeight, offsetHeight;
      if (frameDocument.height) {
        documentHeight = frameDocument.height;
      }
      else if (frameDocument.body) {
        if (frameDocument.body.scrollHeight) {
          documentHeight = scrollHeight = frameDocument.body.scrollHeight;
        }
        if (frameDocument.body.offsetHeight) {
          documentHeight = offsetHeight = frameDocument.body.offsetHeight;
        }
        if (scrollHeight && offsetHeight) {
          documentHeight = Math.max(scrollHeight, offsetHeight);
        }
      }
      return documentHeight;
    }
  },
  convertLatLong: function(latitude,longitude){

    var lat=parseFloat(latitude);
    var lon=parseFloat(longitude);

    //Check if any error occurred

    if (isNaN(latitude) || isNaN(longitude)) {
      return {latitude:'', longtitude:''};
    }
    else if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
      return {latitude:'',longtitude:''};
    }
    else {
      if (latitude.indexOf('-')!=0){
        latDirect = "N";
      }
      else{
        latDirect = "S";
      }

      if (longitude.indexOf('-')!=0){
        longDirect = "E";
      }else{
        longDirect = "W";
      }

      lat=Math.abs(lat);
      lon=Math.abs(lon);

      //Convert to Degree Minutes Seconds Representation

      var latDeg = Math.floor(lat);
      var latMin = Math.floor((lat-latDeg)*60);
      var latSec =  (Math.round((((lat - latDeg) - (latMin/60)) * 60 * 60) * 100) / 100 ) ;

      var latitudeString = latDeg+'&#176; '+latMin+'\' '+ latDirect;

      var lonDeg = Math.floor(lon);
      var lonMin = Math.floor((lon-lonDeg)*60);
      var lonSec = (Math.round((((lon - lonDeg) - (lonMin / 60 )) * 60 * 60) * 100 ) / 100);

      var longitudeString =lonDeg+'&#176; '+lonMin+'\' '+longDirect;

      return {latitude:latitudeString,longitude:longitudeString}
    }
  }

}

//yuiEvent.onDOMReady(highlight.initialize);