﻿
var map;
var geocoder;
var address;
var mgr;


function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

function getUrl(strType, strValue, intLat, intLng, intZoom)
{
//debugger;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url = "/MapNavigations/"+strType+"/"+strValue;
//only pass the lat, lng and zoom for navigations between countries. i.e. clicking across the border.
//so exclude continents and the world map that has a mapNavMethod of GetContinent.
if (strContentType != 'CONTINENT' && strMapNavMethod != 'GET_CONTINENT')
{
    url += "/"+intLat+"/"+intLng+"/"+intZoom;
}


xmlHttp.onreadystatechange=gotUrl;

xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function gotUrl()
{
//debugger;
    if (xmlHttp.readyState==4)
    {
		var strResponse = new String;
		strResponse = xmlHttp.responseText;
		if (strResponse=="NOT_FOUND")
		{
		    //TODO: do something else here... 
		}
		else
		{
		    //alert(strResponse);
		    document.location.href = strResponse;
		}
    }
}

  //State	Description
  //0	The request is not initialized
  //1	The request has been set up
  //2	The request has been sent
  //3	The request is in process
  //4	The request is complete
  
  


function loadMap() {  
    try {
        map = new GMap2(document.getElementById("map_canvas"));  
        map.setCenter(new GLatLng(dblLat, dblLng), intZoom);  
        //map.addControl(new GLargeMapControl); 
        //map.addControl(new GSmallZoomControl3D());
        if (strControlType == "DEFAULT")
        {
            map.setUIToDefault();
        }      
        else 
        {
            map.setUIToDefault();
        } 
        
        GEvent.addListener(map, "click", getAddress);
        
        mgr = new MarkerManager(map);
        createIcons();
        loadMarkers();
          
        geocoder = new GClientGeocoder();
    }
    catch (e)
    {
    
    }
}
function getAddress(overlay, latlng) {
    if (latlng != null) {    
        address = latlng;    
        geocoder.getLocations(latlng, showAddress);  
    }
}
function showAddress(response) { 
//debugger; 
  if (!response || response.Status.code != 200) { 
    //TODO: do something else here...   
   // alert("Status Code:" + response.Status.code);  
  } 
  else {    
    place = response.Placemark[0]; 
    //only navigate to a country if it's different to the current one.
    if (place.AddressDetails.Country.CountryNameCode != strCurrentCountryCode)
    {    
        //the lat and lng values passed in are not the click location but the nearest populated place.
        getUrl(strMapNavMethod, place.AddressDetails.Country.CountryNameCode, place.Point.coordinates[1], place.Point.coordinates[0], map.getZoom());
    }
  }
}

function createMarker(point, html, options) {
  var marker = new GMarker(point, options);

  GEvent.addListener(marker, "click", function() {
    map.openInfoWindowHtml(point, html, {maxWidth:150});
  });
  return marker;
}

function createIcons() {
    
    // Create our "tiny" marker icon
    var tinyRedIcon = new GIcon();
    tinyRedIcon.image = "/Images/SiteImages/mm_20_red.png";
    tinyRedIcon.shadow = "/Images/SiteImages/mm_20_shadow.png";
    tinyRedIcon.iconSize = new GSize(12, 20);
    tinyRedIcon.shadowSize = new GSize(22, 20);
    tinyRedIcon.iconAnchor = new GPoint(6, 20);
    tinyRedIcon.infoWindowAnchor = new GPoint(5, 1);
    // Set up our GMarkerOptions object literal
    townMarkerOptions = { icon:tinyRedIcon };

    // Create our "tiny" marker icon
    var tinyBlueIcon = new GIcon();
    tinyBlueIcon.image = "/Images/SiteImages/mm_20_blue.png";
    tinyBlueIcon.shadow = "/Images/SiteImages/mm_20_shadow.png";
    tinyBlueIcon.iconSize = new GSize(12, 20);
    tinyBlueIcon.shadowSize = new GSize(22, 20);
    tinyBlueIcon.iconAnchor = new GPoint(6, 20);
    tinyBlueIcon.infoWindowAnchor = new GPoint(5, 1);
    // Set up our GMarkerOptions object literal
    aptBlockMarkerOptions = { icon:tinyBlueIcon };

    // Create our "tiny" marker icon
    var tinyGreenIcon = new GIcon();
    tinyGreenIcon.image = "/Images/SiteImages/mm_20_green.png";
    tinyGreenIcon.shadow = "/Images/SiteImages/mm_20_shadow.png";
    tinyGreenIcon.iconSize = new GSize(12, 20);
    tinyGreenIcon.shadowSize = new GSize(22, 20);
    tinyGreenIcon.iconAnchor = new GPoint(6, 20);
    tinyGreenIcon.infoWindowAnchor = new GPoint(5, 1);
    // Set up our GMarkerOptions object literal
    hotelMarkerOptions = { icon:tinyGreenIcon };
}