var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "http://esa.ilmari.googlepages.com/markeryellow.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(country, postcode, altLocation, 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, altLocation);

			}else{
				callbackFunction(0, altLocation);
			}
		});	
		
	localSearch.execute(postcode + ", " + country);
}

function placeMarkerAtPoint(point, altLocation)
{

    if(point != 0){
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
	setCenterToPoint(point)
    }
    else
    {
    map.setCenter(new GLatLng(altLocation), 13);
    map.addOverlay(new GMarker(new GLatLng(altLocation),{clickable:false, icon:yellowIcon})); // visited
    }

}

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

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(54.622978,-2.592773), 5, G_HYBRID_MAP);
	}
}

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);

