var map;
var localSearch = new GlocalSearch();
var iconCount = 0;
var pointKey = 0;
var zoomLevel=17;

//Postcode handling
function usePointFromPostcode(postcode, selzoom) {
	localSearch.setSearchCompleteCallback(null,function() {
			if (localSearch.results[0])
				{
					resultLat = localSearch.results[0].lat;
					resultLng = localSearch.results[0].lng;
					point = new GLatLng(resultLat,resultLng);
					if (selzoom) { 
						iconCount++;
						var icon = new GIcon();
						icon.image = "/images/gmap/"+iconCount+".png";
						icon.iconSize = new GSize(16, 28);
						icon.iconAnchor = new GPoint(10, 28);
						map.setCenter(point, 15);	 
						map.addOverlay(new GMarker(point,icon));
	bounds.extend(point);
					} else { 
					setCenterToPoint(point);
					}
				}
		});	
	localSearch.execute(postcode);

}
function centerMapOnPostcode(postcode,zoomLevel) {
	document.getElementById("loader").style.display = 'inline-block';
	localSearch.setSearchCompleteCallback(null,function() {
			if (localSearch.results[0])
			{	
				resultLat = localSearch.results[0].lat;
				resultLng = localSearch.results[0].lng;
				point = new GLatLng(resultLat,resultLng);
				map.setCenter(point, zoomLevel);
				document.getElementById("loader").style.display = 'none';
			}
		});	
	localSearch.execute(postcode);
}
function setCenterToPoint(point)
{
	iconCount++;
	var icon = new GIcon();
	icon.image = "/images/gmap/"+iconCount+".png";
	icon.iconSize = new GSize(16, 28);
	icon.iconAnchor = new GPoint(10, 28);
	map.setCenter(point, 8);	 
	map.addOverlay(new GMarker(point,icon));
	bounds.extend(point);
	zoomfit();
}
function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmaps"));
		var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,30));
		map.addControl(new GSmallZoomControl3D(),topLeft);
		//var customUI = map.getDefaultUI();
        //customUI.controls.scalecontrol = false;
        //map.setUI(customUI);
		map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_NORMAL_MAP);
		document.getElementById("loader").style.display = 'none';
	}
}

var bounds = new GLatLngBounds();
function zoomfit()
{
var newzoom = map.getBoundsZoomLevel(bounds);
var newcenter = bounds.getCenter();
map.setCenter (newcenter,newzoom);
}
function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}
addUnLoadEvent(GUnload);
