var map;
var localSearch = new GlocalSearch();
 
var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

var icona = new GIcon();
icona.image = "http://maps.google.com/mapfiles/markerA.png";
icona.shadow = "http://www.google.com/mapfiles/shadow50.png";
icona.iconSize = new GSize(20, 34);
icona.shadowSize = new GSize(37, 34);
icona.iconAnchor = new GPoint(10, 34);

var iconb = new GIcon();
iconb.image = "http://maps.google.com/mapfiles/markerB.png";
iconb.shadow = "http://www.google.com/mapfiles/shadow50.png";
iconb.iconSize = new GSize(20, 34);
iconb.shadowSize = new GSize(37, 34);
iconb.iconAnchor = new GPoint(10, 34);

var iconc = new GIcon();
iconc.image = "http://maps.google.com/mapfiles/markerC.png";
iconc.shadow = "http://www.google.com/mapfiles/shadow50.png";
iconc.iconSize = new GSize(20, 34);
iconc.shadowSize = new GSize(37, 34);
iconc.iconAnchor = new GPoint(10, 34);

var icond = new GIcon();
icond.image = "http://maps.google.com/mapfiles/markerD.png";
icond.shadow = "http://www.google.com/mapfiles/shadow50.png";
icond.iconSize = new GSize(20, 34);
icond.shadowSize = new GSize(37, 34);
icond.iconAnchor = new GPoint(10, 34);

function myzoom(a,b,c) {
	map.setCenter(new GLatLng(b,c),a);
	//map.setZoom(map.getZoom() + a);
}	
		
function usePointFromPostcode(postcode, callbackFunction) {
  
  localSearch.setSearchCompleteCallback(null, 
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode);
}


function useGeoDat(postcode, callbackFunction) {
  
  localSearch.setSearchCompleteCallback(null, 
    function() {
      
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK");
}


function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(50.8261,-0.1618), 13);
		//map.addOverlay(new GMarker(new GLatLng(50.827341,-0.168120),icona));
		//map.addOverlay(new GMarker(new GLatLng(50.829645,-0.147156),iconb));
		//map.addOverlay(new GMarker(new GLatLng(50.834768,-0.2065944),iconb));
		map.addOverlay(new GMarker(new GLatLng(50.8274,-0.1681),icona));
	}
}

function usePointFromPostcodeViaCache(postcode, callbackFunction) {

  var ajax_connection = createRequest();
  
  ajax_connection.open('get', "geocode.asp?postcode=" + postcode);
  
  // setup the function to deal with the reply
  ajax_connection.onreadystatechange = function(){
    
    if (ajax_connection.readyState == 4) {
      var xmlDoc = ajax_connection.responseXML;
      var markers = xmlDoc.documentElement.getElementsByTagName("location");
      
      if (markers.length > 0)
      {
        var resultLat = markers[0].getAttribute('latitude');
        var resultLng = markers[0].getAttribute('longitude');
        var resultDate = markers[0].getAttribute('date_added');
        var point = new GLatLng(resultLat,resultLng);
        document.getElementById("result").innerHTML = "Result for " + postcode + " came from cache. It was last cached on " + resultDate;
			callbackFunction(point);
      }else{
        usePointFromPostcode(postcode, callbackFunction);
      }
    }
  }  
  
  ajax_connection.send(null);
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
	map.setCenter(point, 13);
}

function setCenterToPoint(point)
{
	map.setCenter(point, 13);
}

function createRequest() {
  // create an Ajax Request
  
  var ajaxRequest;
  try
  {
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }    
    catch (e1)
    {
      try
      {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
        catch (e2)
        {
          ajaxRequest = new XMLHttpRequest();
        }
    }
  return ajaxRequest;
}




function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);

