var siteID = '246';

var hotelListMap;

var hotelLocationMap;

var hotelListMapState = "hotelListMapState_246";
var hotelViewTypeMap = "hotelViewTypeMap_246";

var isIE6 = (window.external &&	typeof window.XMLHttpRequest == "undefined");

function onloadInitHotelLocationMap()
{
  if(!GBrowserIsCompatible())
  {
    return false;
  }
  
  hotelLocationMap = createMap('hotelLocationMap');
  
  hotelLocationMap.initialized = true;
   
  var icon = new GIcon(G_DEFAULT_ICON);
  icon.image = '/images/_default/gmarkers/m01.png';
  icon.shadow = null;
  icon.shadowSize = null;
  icon.iconSize = new GSize(20, 28);
  
  // Dynamically created in hotel_map_location.php
  hotelMarker = createHotelLocationMapMarker(hotelLocationMap, icon);
  
  hotelLocationMap.addOverlay(hotelMarker);
  
  var point = new GLatLng('32.718834000000001083', '-117.16384100000000501');
  
  hotelLocationMap.setCenter(point, 17);
  
  GEvent.trigger(hotelMarker, "click");
  
}

function initHotelListMap()
{
  if(!GBrowserIsCompatible())
  {
    return false;
  }
  
  hotelListMap = createMap('hotelListMap');
  
  hotelListMap.initialized = true;
  
  hotelListMap.hotelMarkers = new Array();
  
  var bounds = new GLatLngBounds();

  var hotelElements = getHotelListElements();

  for(var i=0; i<hotelElements.length; i++)
  {
    var current = hotelElements[i];
    
    var coords = current.getAttribute('geo').split(',');
    
    var point = new GLatLng(coords[0], coords[1]);
    
    var imgI = i+1;
    
    if(imgI<10) {
      imgI = '0' + imgI;
    }
    
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = '/images/_default/gmarkers/m' + imgI + '.png';
    icon.shadow = null;
    icon.shadowSize = null;

    icon.iconSize = new GSize(20, 28);
    
    hotelListMap.hotelMarkers[i] = createHotelMarker(point, getHotelInfoWindowHtml(current), icon, hotelListMap);
    hotelListMap.hotelMarkers[i].markerI = i;
    hotelListMap.hotelMarkers[i].itemID = parseInt( current.getAttribute( 'hotelID' ) );
    
    hotelListMap.addOverlay(hotelListMap.hotelMarkers[i]);

    bounds.extend(new GLatLng(point.lat(), point.lng()));
  }
  
  loadMapState(hotelListMapState, hotelListMap, bounds);
  
  // Located in hotel_maps template
  setTimeout("addAttractionMarkers(hotelListMap)", 20);
  
  // Located in hotel_maps template
  setTimeout('doAddPOIMarker(hotelListMap)', 20);
    
  hotelListMap.savePosition(); 
}

function loadMapState(cookieName, map, bounds)
{
  if (document.cookie.length>0) {
    cookieStart = document.cookie.indexOf(cookieName + "=");
    
    if (cookieStart!=-1)
    {
      cookieStart += cookieName.length+1; 
      cookieEnd=document.cookie.indexOf(";",cookieStart);
      if (cookieEnd==-1)
      {
        cookieEnd=document.cookie.length;
      }
      cookietext = document.cookie.substring(cookieStart,cookieEnd);
      // == split the cookie text and create the variables ==
      bits = cookietext.split("|");
      lat = parseFloat(bits[0]);
      lng = parseFloat(bits[1]);
      zoom = parseInt(bits[2]);
      maptype = parseInt(bits[3]);
      markerI = bits[4];
      itemID = bits[5];
        
      if(!isNaN(bits[4] * 1) && (map.hotelMarkers[markerI] && map.hotelMarkers[markerI].itemID == itemID))
      {
        map.setCenter(new GLatLng(lat, lng), zoom);
        
        hotelListMarkerClick(bits[4]);
      }
      else
      {
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
      }
    }
    else
    {
      map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    }  
  }
  else
  {
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
  }
}

function createMap(mapContainer)
{
  var aMap = new GMap2(document.getElementById(mapContainer));
  
  aMap.initialized = true;

  aMap.addControl(new GMapTypeControl());

  // Need to set the center before adding markers or get an error
  aMap.setCenter(new GLatLng(0,0), 0);

  if(window.ExtLargeMapControl)
  {
    aMap.addControl(new ExtLargeMapControl());
  }
  else
  {
    aMap.addControl(new GLargeMapControl());
  }

  // Create a hotel bubble
  aMap.hotelBubble = new EWindow(aMap, E_STYLE_7);      
  aMap.addOverlay(aMap.hotelBubble);
  
  // Create an attraction bubble
  aMap.attractionBubble = new EWindow(aMap, E_STYLE_7);      
  aMap.addOverlay(aMap.attractionBubble);

  return aMap;
}

function hotelListMarkerClick(markerI)
{
  if(document.getElementById('hotelListMapContainer').style.display == 'none')
  {
    toggleHotelListMap();
    
    document.getElementById('hotelListMapContainer').style.display = '';
  }
  
  window.location.href="#map";
  
  hotelListMarkerClickDelayed(markerI);
}

function hotelListMarkerClickDelayed(markerI)
{
  if(hotelListMap == null || hotelListMap.hotelMarkers == "undefined" || hotelListMap.hotelMarkers[markerI] == "undefined")
  {
    setTimeout("hotelListMarkerClickDelayed(" + markerI + ")", 100);
    
    return;
  }
  
  GEvent.trigger(hotelListMap.hotelMarkers[markerI], "click");
}

function recordMapState(marker, open, map)
{
  maptype = 0;
  for (var i=0;i<map.getMapTypes().length;i++) {
    if (map.getCurrentMapType() == map.getMapTypes()[i]) {
      maptype = i;
    }
  }
  
  var markerI = (marker && open) ? marker.markerI : 'false';
  var itemID  = (marker && open) ? marker.itemID : 'false';
  
  var cookietext = hotelListMapState+"="+map.getCenter().lat()+"|"+map.getCenter().lng()+"|"+map.getZoom()+"|"+maptype+"|"+markerI+"|"+itemID;

  document.cookie=cookietext;
}

function addAttractionMarker(map, point, markerImage, marketingHtml, width, height)
{
  var icon = new GIcon();
  var icon = new GIcon();
  icon.image = markerImage;
  icon.iconSize = new GSize(width, height);
  icon.shadow = null;
  icon.shadowSize = null;
  icon.iconAnchor = new GPoint(10,34);
  icon.infoWindowAnchor = new GPoint(5,2);
  icon.transparent = "";
  icon.printImage = "";
  icon.mozPrintImage = "";
  icon.printShadow = "";
  icon.imageMap = [1,1, width,1, width,height, 1,height];
  
	var html = '<table width="302" border="0" cellpadding="0" cellspacing="0" class="infowWindowTable attractionWindowTable">';
	html    += '  <tr>';
	html    += '    <td valign="top">' + marketingHtml + '</td>';
	html    += '  </tr>';
	html    += '</table>';
	
  var marker = createAttractionMarker(point, html, icon, map);
  
  map.addOverlay(marker);
}

function createAttractionMarker(point,html,icon,map) {
  var marker = new GMarker(point, {noCloseOnClick:true, icon:icon});
  // ========== Open the EWindow instead of a Google Info Window ==========
  GEvent.addListener(marker, "click", function() {
    map.hotelBubble.hide();
    map.attractionBubble.openOnAttraction(marker,html);
    recordMapState(marker, true, map);
  });
  return marker;
}

function createHotelMarker(point,html,icon,map) {
  var marker = new GMarker(point, {noCloseOnClick:true, icon:icon});
  // ========== Open the EWindow instead of a Google Info Window ==========
  GEvent.addListener(marker, "click", function() {
    map.attractionBubble.hide();
    map.hotelBubble.openOnHotel(marker,html);
    recordMapState(marker, true, map);
  });
  return marker;
}

function addPOIMarker(map, lat, lon, markerImage, width, height, title)
{
    var icon = new GIcon();
    icon.image = markerImage;
    icon.iconSize = new GSize(width, height);
    icon.shadow = null;
    icon.shadowSize = null;
    icon.iconAnchor = new GPoint(23, 45);
    icon.transparent = "";
    icon.printImage = "";
    icon.mozPrintImage = "";
    icon.printShadow = "";     
    var point = new GLatLng(lat, lon);
    var marker = new GMarker(point, {'noCloseOnClick':true, 'icon':icon, 'title':title});
    map.addOverlay(marker);
}

function linkTo( token, hotelID )
{
	return '/' + siteID + '_' + token.replace( '_', '-' ) + '_h' + hotelID + '.html?src=mapbubble'
}

function getHotelInfoWindowHtml(element)
{
	hotelID	=	parseInt( element.getAttribute( 'hotelID' ) );
	
	var node;
	
	node = document.getElementById('hotelLink' + hotelID);
	var hotelName = node != null ? node.innerHTML : '';
	
	node = document.getElementById('hotelImage' + hotelID);
	var hotelImgSrc = node != null ? node.getAttribute('src') : '';
		
	node = document.getElementById('hotelLocation' + hotelID);
	var hotelLocation = node != null ? node.innerHTML : '';
	
  node = document.getElementById('minRate' + hotelID);
	var minRate = node != null ? node.innerHTML : '';

	node = document.getElementById('youSaveText' + hotelID);
	var youSaveText = node != null ? node.innerHTML : '';
	
	if(minRate.length == 0) minRate = '<a href="' + linkTo( 'hotel_rooms', hotelID ) + '">Check</a>';
	
	var html = '<table width="302" border="0" cellpadding="0" cellspacing="0" class="infowWindowTable">';
	html    += '  <tr>';
	html    += '    <td height="22" colspan="4">';
	html    += '      <a href="' + linkTo( 'hotel_info', hotelID ) + '" class="infowWindowTitle" style="position:relative;">' + hotelName + '</a>';
	html    += '      <div class="infowWindowClose"><a href="" onclick="hotelListMap.hotelBubble.hide();recordMapState(false, false, hotelListMap);return false;" title="Close" style="position:relative;"><img src="/images/_default/infowindowClose.png" width="16" height="16" alt="Close" border="0"></a></div>';
	html    += '    </td>';
	html    += '  </tr>';
	html    += '  <tr>';
	html    += '    <td width="90" rowspan="3" valign="top"><a href="' + linkTo( 'hotel_info', hotelID ) + '" style="position:relative;" title="Hotel name"><img src="' + hotelImgSrc + '" alt="Hotel name" class="infowWindowImg"></a></td>';
	html    += '    <td width="120" height="22" valign="bottom" class="infowWindowLocation">' + hotelLocation + '</td>';
	html    += '    <td width="85" rowspan="2" align="center" valign="top" class="infowWindowRate">Rates from:<br />';
	html    += '      <span class="infowWindowPrice" style="position:relative;">' + minRate + '</span><br/>';
	html    += (youSaveText.length > 0) ? youSaveText : '<br/>';
	html    += '    </td>';
	html    += '    <td width="14" rowspan="2" class="infowWindowRate">&nbsp;</td>';
	html    += '  </tr>';
	html    += '  <tr>';
	html    += '    <td width="127" height="30" valign="top">' + getPackageImage(element) + getDealsImage(element) + '</td>';
	html    += '  </tr>';
	html    += '  <tr>';
	html    += '    <td width="120" valign="middle"><a href="' + linkTo( 'hotel_info', hotelID ) + '" title="View Hotel Info" class="infowWindowInfoLink" style="position:relative;">View Hotel Info</a></td>';
	html    += '    <td width="85" valign="middle" align="center"><a href="' + linkTo( 'hotel_rooms', hotelID ) + '" title="Book Now!"><img style="position:relative;" src="/images/_default/infoWindowButton.png" width="74" height="19" alt="Book Now!" border="0"></a></td>';
	html    += '  </tr>';
	html    += '  <tr>';
	html    += '    <td width="302" height="24" colspan="3">&nbsp;</td>';
	html    += '  </tr>';
	html    += '</table>';
	
	return html;
}

function getPackageImage(element)
{
  var node;
  
	node = document.getElementById('btnPac' + hotelID);
	var packageUrl = node != null ? node.getAttribute('href') + '?src=mapbubble' : false;
  
  var packageImgSrc = element.getAttribute('pkgs') == '1' ? '/images/_default/package_on.png' : '/images/_default/package_off.png';
  
  var html = '';
  
  if(packageUrl)
  {
    html += '<a href="' + packageUrl + '" style="position:relative;">';
  }
  
  html += '<img src="' + packageImgSrc + '" width="29" height="25" alt="Packages" title="Packages" class="infowWindowIcon" style="position:relative;"/>';
  
  if(packageUrl)
  {
    html += '</a>';
  }
  
  return html;
}

function getDealsImage(element)
{
  var node;
  
  node = document.getElementById('btnDeal' + hotelID);
  var dealsUrl = node != null ? node.getAttribute('href') + '?src=mapbubble' : false;
  
  var dealsImgSrc = element.getAttribute('deals') == '1' ? '/images/_default/deal_on.png' : '/images/_default/deal_off.png';
  
  var html = '';
  
  if(dealsUrl)
  {
    html += '<a href="' + dealsUrl + '" style="position:relative;">';
  }
  
  html += '<img src="' + dealsImgSrc + '" width="29" height="25" alt="Deals" title="Deals" class="infowWindowIcon" style="position:relative;"/>';
  
  if(dealsUrl)
  {
    html += '</a>';
  }
  
  return html;
}
   
function getHotelListElements()
{
  var tables = document.getElementsByTagName('table');
  
  var returnElements = new Array();
  
  for(var i=0; i<tables.length; i++)
  {
    var current = tables[i];
    
    if(typeof current.getAttribute('geo') == "string" && current.getAttribute('geo').length > 0)
    {
        returnElements.push(current);
    } 
  }
  
  return returnElements;
}

function toggleHotelListMap()
{
  if(document.getElementById('hotelListMapContainer').style.display == '')
  {
    document.cookie = hotelViewTypeMap + "=0";

    document.getElementById("hotelListMapContainer").style.display = 'none';
  }
  else
  {
    document.cookie = hotelViewTypeMap + '=1;';
    
    document.getElementById("hotelListMapContainer").style.display = '';

    if(!hotelListMap || hotelListMap.initialized != true)
    {
      document.getElementById("hotelListMap").innerHTML = '<center>Loading... Please wait...</center>';
      
      setTimeout("initHotelListMap()", 20);
    }
  }
}

function setDefaultOpen()
{
  if(isIE6)
  {
    document.cookie = hotelViewTypeMap + '=0;';
    
    return;
  }
  
  if(getAresCookie(hotelViewTypeMap) == "")
  {
    document.cookie = hotelViewTypeMap + '=1;';
  }
}

function onloadInitHotelListMap()
{
  if(getAresCookie(hotelViewTypeMap) ==  1)
  {
    document.getElementById("hotelListMapContainer").style.display = '';
    document.getElementById("hotelListMap").innerHTML = '<center>Loading... Please wait...</center>';
    
    setTimeout("initHotelListMap()", 1);
  }
}

function getAresCookie(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 "";
}

var Global = {
  FixPng: function( img )
  {
    if(!isIE6)
    {
      img.style.display = "";
    }
  }
};

// EWindow.js 
//
//   This Javascript is provided by Mike Williams
//   Blackpool Community Church Javascript Team
//   http://www.commchurch.freeserve.co.uk/   
//   http://econym.googlepages.com/index.htm
//
//   This work is licenced under a Creative Commons Licence
//   http://creativecommons.org/licenses/by/2.0/uk/
//
// Version 0.0  Initial version 
// Version 0.1  10/10/2006 Added E_STYLE_7 
// Version 0.2  17/05/2007 Added .isHidden() and .supportsHide()
// Version 0.3  14/09/2007 added .zindex()
// Version 0.4  09/02/2008 moved the window to the G_MAP_FLOAT_PANE


function EStyle(stemImage, stemSize, boxClass, boxOffset) {
  this.stemImage = stemImage;
  this.stemSize = stemSize;
  this.boxClass = boxClass;
  this.boxOffset = boxOffset;
  //this.border = border;
  
  // Known fudge factors are:
  // Firefox (1.0.6 and 1.5)    5, -1
  // IE 6.0                     0, -1
  // Opera 8.54                 3, -1
  // Opera 9 prev               4, -1
  // Netscape (7.2, 8.0)        5, -1
  // Safari                     5, -1        
  
  var agent = navigator.userAgent.toLowerCase();
  
  var fudge = 5;  // assume Netscape if no match found
 
  if (agent.indexOf("opera") > -1) {
    fudge = 3;
  }   
  if (agent.indexOf("firefox") > -1) {
    fudge = 5;
  }   
  if (agent.indexOf("safari") > -1) {
    fudge = 5;
  }   
  if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){
    fudge = 0;
  }
  this.fudge = fudge;
}

var E_STYLE_1 = new EStyle("/images/_default/__blank__.gif", new GSize(81,87),  "estyle1", new GPoint(-30,87-3));
var E_STYLE_2 = new EStyle("/images/_default/__blank__.gif", new GSize(81,87),  "estyle2", new GPoint(-30,87-1));
var E_STYLE_3 = new EStyle("/images/_default/__blank__.gif", new GSize(81,87),  "estyle3", new GPoint(-30,87-10));
var E_STYLE_4 = new EStyle("/images/_default/__blank__.gif", new GSize(81,87),  "estyle4", new GPoint(-30,87-10));
var E_STYLE_5 = new EStyle("/images/_default/__blank__.gif", new GSize(81,87),  "estyle5", new GPoint(-30,87-3));
var E_STYLE_6 = new EStyle("/images/_default/__blank__.gif", new GSize(100,50), "estyle6", new GPoint(100-2,20));
var E_STYLE_7 = new EStyle("/images/_default/__blank__.gif", new GSize(24,24),  "estyle2", new GPoint(-10,23));


function EWindow(map,estyle) {
  // parameters
  this.map=map;
  this.estyle=estyle;
  // internal variables
  this.visible = false;
  // browser - specific variables
  this.ie = false;
  var agent = navigator.userAgent.toLowerCase();
  if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){ this.ie = true} else {this.ie = false}
} 

EWindow.prototype = new GOverlay();

EWindow.prototype.initialize = function(map) {
  var div1 = document.createElement("div");
  div1.style.position = "absolute";
  map.getPane(G_MAP_FLOAT_PANE).appendChild(div1);
  var div2 = document.createElement("div");
  div2.style.position = "absolute";
  div2.style.width = this.estyle.stemSize.width+"px";
  map.getPane(G_MAP_FLOAT_PANE).appendChild(div2);
  this.div1 = div1;
  this.div2 = div2;
}

EWindow.prototype.openOnMap = function(point, html, offset) {
  this.offset = offset||new GPoint(0,0);
  this.point = point;
  this.div1.innerHTML = '<div class="' + this.estyle.boxClass + '"><nobr>' + html + '</nobr></div>';
  if (this.ie && this.estyle.stemImage.toLowerCase().indexOf(".png")>-1) {
    var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.estyle.stemImage+"', sizingMethod='scale');";
    this.div2.innerHTML = '<div style="height:' +this.estyle.stemSize.height+ 'px; width:'+this.estyle.stemSize.width+'px; ' +loader+ '"  style="border:0px"></div>';
  } else {
    this.div2.innerHTML = '<img src="' + this.estyle.stemImage + '" width="' + this.estyle.stemSize.width +'" height="' + this.estyle.stemSize.height +'" style="border:0px">';
  }
  var z = GOverlay.getZIndex(this.point.lat());
  this.div1.style.zIndex = z;
  this.div2.style.zIndex = z+1;
  this.visible = true;
  this.map.panTo(point);
  this.show();
  this.redraw(true);
}

EWindow.prototype.openOnMarker = function(marker,html) {
  var vx = marker.getIcon().iconAnchor.x - 38;
  var vy = marker.getIcon().iconAnchor.y - 25;
  this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}

EWindow.prototype.openOnHotel = function(marker,html) {
  var vx = marker.getIcon().iconAnchor.x - 38;
  var vy = marker.getIcon().iconAnchor.y - 28;
  this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}

EWindow.prototype.openOnAttraction = function(marker,html) {
  var vx = marker.getIcon().iconAnchor.x - 30;
  var vy = marker.getIcon().iconAnchor.y - 32;
  this.openOnMap(marker.getPoint(), html, new GPoint(vx,vy));
}


EWindow.prototype.redraw = function(force) {
  if (!this.visible) {return;}
  var p = this.map.fromLatLngToDivPixel(this.point);
  this.div2.style.left   = (p.x + this.offset.x) + "px";
  this.div2.style.bottom = (-p.y + this.offset.y -this.estyle.fudge) + "px";
  this.div1.style.left   = (p.x + this.offset.x + this.estyle.boxOffset.x) + "px";
  this.div1.style.bottom = (-p.y + this.offset.y + this.estyle.boxOffset.y) + "px";
}

EWindow.prototype.remove = function() {
  this.div1.parentNode.removeChild(this.div1);
  this.div2.parentNode.removeChild(this.div2);
  this.visible = false;
}

EWindow.prototype.copy = function() {
  return new EWindow(this.map, this.estyle);
}

EWindow.prototype.show = function() {
  this.div1.style.display="";
  this.div2.style.display="";
  this.visible = true;
}

EWindow.prototype.hide = function() {
  this.div1.style.display="none";
  this.div2.style.display="none";
  this.visible = false;
}

EWindow.prototype.isHidden = function() {
  return !this.visible;
}

EWindow.prototype.supportsHide = function() {
  return true;
}

EWindow.prototype.zindex = function(zin) {
  var z = GOverlay.getZIndex(this.point.lat());
  this.div1.style.zIndex = z+zin;
  this.div2.style.zIndex = z+1+zin;
}