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

function createPin () {
	icon.image = "http://www.realbrighton.com/brightonfringe/i_am_here_pin.png";
	icon.shadow = "http://www.realbrighton.com/images/pngs/pin_shadow.png";
	icon.iconSize = new GSize(28, 42);
	icon.shadowSize = new GSize(46, 41);	
	icon.iconAnchor = new GPoint(14, 42);
} // end function


function createNumberPin (number) {
	var myicon = new GIcon();
	myicon.image = "http://www.realbrighton.com/images/pngs/pin_f"+number+".png";
	myicon.shadow = "http://www.realbrighton.com/images/pngs/pin_shadow.png";
	myicon.iconSize = new GSize(28, 42);
	myicon.shadowSize = new GSize(46, 41);
	myicon.iconAnchor = new GPoint(14, 42);
	myicon.infoWindowAnchor = new GPoint(26,8);
	myicon.infoShadowAnchor = new GPoint(18, 25);
	return myicon
} // end function


function createFringePin (number) {
	var myicon = new GIcon();
	myicon.image = "http://www.realbrighton.com/brightonfringe/pins/pin-"+number+".png";
	myicon.iconSize = new GSize(30, 20);
	myicon.iconAnchor = new GPoint(15.5, 20);
	myicon.infoWindowAnchor = new GPoint(15,8);
	return myicon
} // end function

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);
				map.setZoom(15);
			}else{
				alert("Address not found!");
			}
		});	
		
	localSearch.execute(postcode);
}

function placeMarkerAtPoint(point)
{
	if (point != undefined) {
		if (gtemps.length > 0) {
			map.removeOverlay(gtemps[0]);
		} // end if
		var marker = new GMarker(point,icon);
		map.addOverlay(marker);
		setCenterToPoint(point);
		gtemps[0] = marker;
		document.getElementById('i-lati').value = point.lat();
		document.getElementById('i-longi').value = point.lng();
		document.getElementById('i-zoomlevel').value = map.getZoom();
	} // end if
}

function setCenterToPoint(point)
{
	map.setCenter(point);
}
function removeMarker()
{
	if (gtemps.length > 0) {
		map.removeOverlay(gtemps[0]);				
	} // end if
	document.getElementById('i-lati').value = 0;
	document.getElementById('i-longi').value = 0;
	document.getElementById('i-zoomlevel').value = 14;
	map.setCenter(new GLatLng(53.12040528310657,-1.40625), 4);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

if(typeof addLoadEvent != 'function') { 
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  oldonload();
		  func();
		}
	  }
	}
} // end if
	
if(typeof addUnLoadEvent != 'function') {
	function addUnLoadEvent(func) {
		var oldonunload = window.onunload;
		if (typeof window.onunload != 'function') {
		  window.onunload = func;
		} else {
		  window.onunload = function() {
			oldonunload();
			func();
		  }
		}
	}
} // end if

addUnLoadEvent(GUnload);

