var markers = [];
var map;
var boolShowSearch = true;

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
//    map.setMapType(G_HYBRID_MAP);
    map.setCenter(new GLatLng(20, 0), 1);
    map.setUIToDefault();
  }
}

function getHotelData(){
  jQuery.ajax({
    type: "POST",
    data: {"brandId": -1},
    url: "/searchData/mapData.asmx/GetHotels",
    datatype: "xml",
    success:loadData
  });
}

function loadData(xml){
  jQuery(xml).find('Hotel').each(function(){
    var latlng = new GLatLng(jQuery(this).find('Latitude').text(), jQuery(this).find('Longitude').text());
    var hotelName = jQuery(this).find('HotelName').text();
    var id = jQuery(this).find("HotelID").text();
    var brand = jQuery(this).find("Brand").text();
    var city = jQuery(this).find("City").text();
    var state = jQuery(this).find("State").text();
    var country = jQuery(this).find("Country").text();
    markers.push(createMarker(latlng, hotelName, id, brand, city, state, country));
  });
  var markerCluster = new MarkerClusterer(map, markers);
}

function createMarker(point, hotelName, id, brand, city, state, country){
  var marker = new GMarker(point);
  var link_location = getFolderName(brand) + '/hotels/index.aspx?id=' + String(id);
  GEvent.addListener(marker, "mouseover", function() {
    var hotelLink = "";
    if (brand != 40){
        hotelLink = '<a href="' + link_location + '"><img alt="" src="/uploadedImages/Hotels/' + getImageFolder(brand) + '/' + String(id) + '/1s.jpg" class="floatleft" height="50" /></a>';
    }
    hotelLink += '<p><a href="' + link_location + '" class="hotel_name">' + hotelName + '</a><br/><span class="hotel_location">' + city + '<br />';
    if(state != ''){
      hotelLink += state + ', ';
    }
    hotelLink += country + '</span></p>';
    map.openInfoWindowHtml(point, hotelLink);
  });
  return marker;
}

function getFolderName(brand){
  switch(String(brand)){
    case "2":
         return "http://www.preferredhotels.com/preferred_hotel";
         break;
    case "3":
         return "http://www.summithotels.com/summit";
         break;
    case "4":
         return "http://www.sterlinghotels.com/sterling";
         break;
    case "20":
         return "http://www.preferredboutique.com/preferred_boutique";
         break;
    case "40":
         return "/phg";
  }
  return "";
}

function getImageFolder(brand){
  switch(String(brand)){
    case "2":
         return "preferred";
         break;
    case "3":
         return "summit";
         break;
    case "4":
         return "sterling";
         break;
    case "20":
         return "boutique";
         break;
  }
  return "";
}

function showSearch(){
  if(boolShowSearch){
    jQuery('#widget').css('display','block');
    jQuery('#showSearch').html('&gt; Hide Search');
    jQuery('#map_canvas').addClass('fleft');
    jQuery('#map_canvas').css('width','430px');
    boolShowSearch = !boolShowSearch;
  }else{
    jQuery('#widget').css('display','none');
    jQuery('#showSearch').html('&gt; Advanced Search');
    jQuery('#map_canvas').removeClass('fleft');
    jQuery('#map_canvas').css('width','620px');
    boolShowSearch = !boolShowSearch;
  }
  return false;
}
addLoadEvent(initialize);
addLoadEvent(getHotelData);


