var localSearch = new GlocalSearch();
var locationPosn = new Array();
var searchPostcode;

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 load() {
  if (GBrowserIsCompatible()) {
	localSearch = new GlocalSearch();
	map = new GMap2(document.getElementById("googlemap"));
	// map.setCenter(new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude), 13);
	//map.addControl(new GSmallMapControl());
	//map.addControl(new GMapTypeControl());
	var customUI = map.getDefaultUI();
		customUI.maptypes.hybrid = false;
		map.setUI(customUI);
  }
}

function lookupPostcodeCB(postcode) {

	localSearch.setSearchCompleteCallback(null,
		function() {
			if (localSearch.results[0]) {

				var Lat = localSearch.results[0].lat;
				var Lng = localSearch.results[0].lng;
				var point = new GLatLng(Lat,Lng);
				setMap(document.getElementById("postcode").value,Lat,Lng);
			} else {
				alert("Postcode not found!");
			}
		});

	localSearch.execute(postcode + ", UK");
}

function doLookup(){
	searchPostcode = document.getElementById("postcode").value;
	lookupPostcodeCB(document.getElementById("postcode").value);
	return false;
}

function lookupAtoB(a,b){
	directions = new GDirections(map);
	directions.loadFromWaypoints(a,b);
	alert(directions.getDistance());
}

function createMarker(point,html) {
	var mylabel = {"url":"", "anchor":new GLatLng(4,4), "size":new GSize(12,12)};
	var Icon = new GIcon(G_DEFAULT_ICON, "", mylabel)

	var marker = new GMarker(point,Icon);
	GEvent.addListener(marker, "mouseover", function() {
		// marker.openInfoWindowHtml(html);
		var logo = new Image(157,39);
		var div = document.createElement('div');
		div.id = 'googleMapDescHtml';
		div.style.display = 'none';
		
		try {
			logo.src = 'images/logo-small.gif';	
		} catch(e) {}
		
		document.getElementsByTagName('body')[0].appendChild(div);
		
		var markerDesc = document.getElementById('address').innerHTML.replace(/<p>/,'<p><strong>').replace(/<\/p>/, '</strong></p>');
		
		div.innerHTML = '<div id="markerDesc"><img style="margin-bottom: 0.25em; height: 40px;" src="images/logo-small.gif" />' + markerDesc + '</div>';

		marker.openInfoWindowHtml(div.innerHTML);
		
		document.getElementsByTagName('body')[0].removeChild(div);
		
	});
	
	return marker;
}


function sortArray(a,b){
	a = a[1];
	b = b[1];
	return a == b ? 0 : (a < b ? -1 : 1)
}


function setMap(location,pointx,pointy) {
	var mappoint = new GLatLng(pointx,pointy,true);
	map.setCenter(mappoint, 15);
	var marker = createMarker(mappoint,"<h3>"+location+"</h3>");
	map.addOverlay(marker);
	if (showDirections){
		var directions = new GDirections(map, document.getElementById("direction"));
		document.getElementById("direction").innerHTML = '';
	}
}
load();
doLookup();