var map;


//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);


function usePointFromPostcode(postcode, callbackFunction, colour, html, index) {
    var localSearch = new GlocalSearch();
    var number = index;

   
    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, colour, html, number);
		        
		    } else {
		        alert("Postcode not found!");
		    }
		});	
		
	localSearch.execute(postcode + ", UK");
}



function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}

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.setCenter(new GLatLng(51.459141, -0.287018), 11);
	}
}

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();
	  }
	}
}
function placeMarkerAtPoint(point, colour, html, index) {
    
    if (!map) {
     
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
        var icon = new GIcon();
        icon.image = "http://www.google.com/mapfiles/marker" + colour + letter + ".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);
        icon.infoWindowAnchor = new GPoint(9, 2);
     map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.setCenter(point, 16);
        var marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
        map.addOverlay(marker);
        
    }
    else {

        var letter1 = String.fromCharCode("A".charCodeAt(0) + index);
        var icon1 = new GIcon();
        icon1.image = "http://www.google.com/mapfiles/marker" + colour + letter1 + ".png";
        icon1.shadow = "http://www.google.com/mapfiles/shadow50.png";
        icon1.iconSize = new GSize(20, 34);
        icon1.shadowSize = new GSize(37, 34);
        icon1.iconAnchor = new GPoint(10, 34);
        icon1.infoWindowAnchor = new GPoint(9, 2);
        map.setCenter(point, 11);
        var marker = new GMarker(point, icon1);
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
        map.addOverlay(marker);
       
    }
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
