﻿//alert("Please be aware that we are currently experiencing technical difficulties with the Google Maps API, you may find certain British postcodes are being reported as not being found. Google is aware of this problem and we hope to have the situation rectified shortly.");

var map;
var gdir;
var geocoder = null;
var gMapMarkers = new Array(2);
var gMapRecordCount;
var intervalCount = 0;
var gMapInterval;
var xml;
var markers;
var directions;
var hotelIndex;


function ltrim(str, chars) 
  {   
    chars = chars || "\\s";   
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");  	
  }   

var qString = window.location.search;
hotelIndex = ltrim(qString, "?")
     
function plotHotelSelectedLatLng(lat,lng,detail,image) 
    {
    
       var point = new GLatLng(lat,lng);
       
       map.panTo(point);
       
       map.setCenter(point,7);
       
       gMapMarkers[hotelIndex].openInfoWindowTabsHtml([new GInfoWindowTab('Location',detail), new GInfoWindowTab('Hotel',image), new GInfoWindowTab('Facilities','')]);
    }


function intervalPlotHotel()
{
    //Build directions string for additional tab
    
    var innerDetail;
    var detail;
    
    if(intervalCount != gMapRecordCount)
    {      
      innerDetail = markers[intervalCount].getAttribute("detail") + '<br><br>Start address/postcode:<br><form action="http://maps.google.com/maps" method="get" target="_blank">' +
                  '<input type="text" size="30" maxlength="40" name="saddr" id="saddr" value="" /><br>' +
                  '<input value="Get Directions" type="submit">' +
                  '<input type="hidden" name="daddr" value="' +
                  markers[intervalCount].getAttribute("lat") + ',' + markers[intervalCount].getAttribute("lng") + "(" + "Brend Hotels Ltd" + ' - ' + markers[intervalCount].getAttribute("hotel")  + ")" + '"/>';
                 
      detail = "<p class=" + '"' + "gMapWindow" + '"' +">" + innerDetail  + "</p>";
      
      image = "<img src=" +'"'+ markers[intervalCount].getAttribute("img") +'"'+ "width=205" + "height=180 />"  
                    
      plotHotelLatLng(markers[intervalCount].getAttribute("lat"), markers[intervalCount].getAttribute("lng"), detail, intervalCount, image); 
      
      // Increase interval count
      
      intervalCount++;
    
    }
    
    else
    
    {
    
      innerDetail = markers[hotelIndex].getAttribute("detail") + '<br><br>Start address/postcode:<br><form action="http://maps.google.com/maps" method="get" target="_blank">' +
                  '<input type="text" size="30" maxlength="40" name="saddr" id="saddr" value="" /><br>' +
                  '<input value="Get Directions" type="submit">' +
                  '<input type="hidden" name="daddr" value="' +
                  markers[hotelIndex].getAttribute("lat") + ',' + markers[hotelIndex].getAttribute("lng") + "(" + "Brend Hotels Ltd" + ' - ' + markers[hotelIndex].getAttribute("hotel")  + ")" + '"/>';
                 
      detail = "<p class=" + '"' + "gMapWindow" + '"' +">" + innerDetail  + "</p>";
      
      image = "<img src=" +'"'+ markers[hotelIndex].getAttribute("img") +'"'+ "width=205" + "height=180 />"
              
      clearInterval(gMapInterval); 
      var tmpPostcode = markers[hotelIndex].getAttribute("postcode");   
      plotHotelSelectedLatLng(markers[hotelIndex].getAttribute("lat"), markers[hotelIndex].getAttribute("lng"), "<p class=" + '"' + "gMapWindow" + '"' +">" + detail +"</p>",image);     
    }
               
}
    
    function plotHotelLatLng(lat,lng,detail,number,image) 
    {
    
       var point = new GLatLng(lat,lng);     
       var marker = new GMarker(point);
       
       gMapMarkers[number] = marker;
       
       GEvent.addListener(marker, "click",
       function() 
         {
            marker.openInfoWindowTabsHtml([new GInfoWindowTab('Location',detail), new GInfoWindowTab('Hotel',image), new GInfoWindowTab('Facilities','')]);
			   }
			 );                     
       
       map.addOverlay(marker);
    }

function initialize()
{

  if (GBrowserIsCompatible())
  {
    geocoder = new GClientGeocoder();
    directions = new Array();
    icon = new GIcon();
    map = new GMap2(document.getElementById("map"));
    
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(50.4217, -4.1888), 4);
    
    //gdir = new GDirections(map, document.getElementById("directions"));
    
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    
    //setDirections("EX312LL", "EX329JL", "en_US");
    
    GDownloadUrl("hotels.xml", function(data) 
		{
          xml = GXml.parse(data);
          markers = xml.documentElement.getElementsByTagName("marker");
          
          gMapRecordCount = markers.length;
                    
          gMapMarkers.length = markers.length; 
          
          if (gMapRecordCount != 0)
          {
            gMapInterval = setInterval('intervalPlotHotel()', 200);         
          }  
  
     });
       
  } 

  else
  {
   alert("Sorry, the Google Maps API is not compatible with this browser");
  }
  
}

//Direction Error Handler 
function handleErrors()
{
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
}



  
 //Set Directions Function   
function setDirections(fromAddress, toAddress, locale) 
 {
    document.getElementById("directions").style.display="";
    
    gdir.load("from: " + fromAddress + " to: " + toAddress,
    { "locale": locale });    
 }
    
function onGDirectionsLoad()
{ 
  // On Load Status
  map.addOverlay(gdir.getPolyline());
}
		 
if (window.attachEvent) //Unload catch 
{ 
  window.attachEvent("onunload", function() 
  { 
  GUnload();// Internet Explorer 
  }); 
  } 
  else 
  { 
  window.addEventListener("unload", function() 
  { 
  GUnload(); // Firefox and standard browsers 
  }, false); 
}


this.onload = initialize; //Start
       