
var markers = new Array();
var htmlTexts = new Array();

var selectedMarker =0;
var debugTimeOut;


function getHttpRequest() {
    var xmlHttp;
    

    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return null;
        }
      }
    }

    return xmlHttp;
}

function createXml() {

    var xmlHttp = getHttpRequest();
    xmlHttp.open("GET","/createXml.aspx",true);
    xmlHttp.send(null);
}

/* Put the counties on the map */

function getCounties(zoom)
  {
  

  var xmlHttp = getHttpRequest();

    

    xmlHttp.onreadystatechange=function()
      {
      
      if(xmlHttp.readyState==4) {
        
        if(xmlHttp.status==200) {

            
            putCountiesOnMap(xmlHttp.responseXML,zoom);
            
            
        }
      }
    }
    xmlHttp.open("GET","/xml/countyList.xml",true);
    
    xmlHttp.send(null);
    
}

/* get the hotels in a specified area */

function getHotels(xLoc, yLoc)
  {
  

  var xmlHttp = getHttpRequest();

    

    xmlHttp.onreadystatechange=function()
      {
      
      if(xmlHttp.readyState==4) {
        
        if(xmlHttp.status==200) {

            
            putHotelsOnMap(xmlHttp.responseXML);
            
            
        }
      }
    }
    xmlHttp.open("GET","/xml/smallList.aspx?xLoc=" + xLoc + "&yLoc=" + yLoc,true);
    
    xmlHttp.send(null);
    
}


function putCountiesOnMap(response,zoom) {
    
    var mgr = new GMarkerManager(map);
    
    
    var countyMarkers = [];
    
    var counties = response.getElementsByTagName("county");
    
    for(i=0;i<counties.length;i++) {
        var name = counties[i].getElementsByTagName("name")[0].firstChild.data;
        var xLoc = counties[i].getElementsByTagName("xLoc")[0].firstChild.data;
        var yLoc = counties[i].getElementsByTagName("yLoc")[0].firstChild.data;
        
        var marker = new GMarker(new GPoint(yLoc,xLoc));
        marker.name = name;
        
        countyMarkers.push(marker);
        GEvent.addListener(marker, 'click', function() { window.location="county.aspx?county=" + this.name; });
        GEvent.addListener(marker, 'mouseover', function() { this.openInfoWindowHtml("Zoom in County " + this.name); });
        
        
        
    }
    
    mgr.addMarkers(countyMarkers,0,zoom);

    mgr.refresh();
    

}

function putHotelsOnMap(response) {
    
    
    
    var hotels = response.getElementsByTagName("hotel");
    
    
    
    var mgr = new GMarkerManager(map);
    
    var hotelMarkers = [];
    
    var isHotelOnMap = false;

    for(i=0;i<hotels.length;i++) {
        
        var id = hotels[i].getElementsByTagName("id")[0].firstChild.data;

        if(id==hotel) isHotelOnMap = true;


        var xLoc = hotels[i].getElementsByTagName("xLoc")[0].firstChild.data;
        
        var yLoc = hotels[i].getElementsByTagName("yLoc")[0].firstChild.data;
        var htmlText = hotels[i].getElementsByTagName("htmlContents")[0].firstChild.data;
        
        var marker = new GMarker(new GPoint(xLoc,yLoc));
        
        markers[id] = marker;
        htmlTexts[id] = htmlText;
        marker.id = id;

        GEvent.addListener(marker,'click',function() { markers[this.id].openInfoWindowHtml(htmlTexts[this.id]); });
        
        var hotelMapLink = document.getElementById("hotelMapLink"+id);
        if(hotelMapLink != null) {
            hotelMapLink.style.visibility ="visible";
        }
        
        hotelMarkers.push(marker);
    
    }
    
    
    
    mgr.addMarkers(hotelMarkers,9);
    

    mgr.refresh();

    
    var dateXml = response.getElementsByTagName("date")[0].firstChild.data;
    if(dateXml != today) createXml();

    if(isHotelOnMap) {
        openMarker(hotel);
    }



}



function openMarker(id) {
    if(map.getZoom()<14) {
        selectedMarker = id;
        map.setCenter(markers[id].getLatLng(),14);
        selectedMarker = id;
        debugTimeOut = setTimeout(function() { openMarker(selectedMarker); },500);
    } else {

        markers[id].openInfoWindowHtml(htmlTexts[id]);
        clearTimeout(debugTimeOut);
    }

}