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 usePointFromPostcode(fulladdress, 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);
				
				
				window.setTimeout(function() {
				 map.setCenter(point, 17);
				 placeMarkerAtPoint(point);
				 map.openInfoWindow(map.getCenter(),
                   document.createTextNode(fulladdress));
				}, 500);
				
				//callbackFunction(point);
			}else{
				document.getElementById("addGeoButton").disabled=true;
				document.getElementById("lon").value = "0.000";
				document.getElementById("lontxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='red'>0.000</font>";
				document.getElementById("lattxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='red'>0.000</font>";
				document.getElementById("lat").value = "0.000";
			
			}
		});	
		
	localSearch.execute(fulladdress + ", UK");
}

	function returnLLFromNONUK(pcode,country_code)
	{
		getpcodeajax=GetXmlHttpObject();
		if (getpcodeajax==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		var url="relay.php?postalcode="+pcode+"&country="+country_code+"&maxRows=1&ysr=yes";	
		
		if (window.ActiveXObject)
		{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(url);
			getlonlat();
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc=document.implementation.createDocument("","",null);
			xmlDoc.load(url);
			xmlDoc.onload=getlonlat;
		}
		else
		{
			alert('Your browser cannot handle the pressure, get a new one.');
		}
		
	}
	function getlonlat()
	{
		
		numreturned = xmlDoc.getElementsByTagName("totalResultsCount")[0].childNodes.length;
		
		if(numreturned>0)
		{
			//var root = xmlDoc.getElementsByTagName('geoname')[0];
			
			var items = xmlDoc.getElementsByTagName("geoname");
			var count = 0;
			
			for (var i = 0 ; i < items.length ; i++) {
				
				// get one item after another
				var item = items[i];
				
				// now we have the item object, time to get the contents
				// get the name of the item
				var lat = item.getElementsByTagName("lat")[0].firstChild.nodeValue;
		
				// get the quantity
				var lon = item.getElementsByTagName("lng")[0].firstChild.nodeValue;
				

			
				document.getElementById("lon").value = lon;
				document.getElementById("lontxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='#aaaaaa'>"+lon+"</font>";
				document.getElementById("lattxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='#aaaaaa'>"+lat+"</font>";
				document.getElementById("lat").value = lat;
				document.getElementById("addGeoButton").disabled=false;
				
			}
			//document.getElementById("output").innerHTML=numreturned + " post codes : " + pcodes;
		}else{
			//document.getElementById("output").innerHTML=" 0 post codes : none";
		}
	}


function returnLL(fulladdress, postcode, callbackFunction) {
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				document.getElementById("lon").value = resultLng;
				document.getElementById("lontxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='blue'>"+resultLng+"</font>";
				document.getElementById("lattxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='blue'>"+resultLat+"</font>";
				document.getElementById("lat").value = resultLat;
				document.getElementById("addGeoButton").disabled=false;

			}else{
				document.getElementById("lon").value = "0.000";
				document.getElementById("lontxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='red'>0.000</font>";
				document.getElementById("lattxt").innerHTML = "<font 'Trebuchet MS, Helvetica, Arial, san-serif' size='-2' color='red'>0.000</font>";
				document.getElementById("lat").value = "0.000";
				document.getElementById("addGeoButton").disabled=true;
			}
		});	
		
	localSearch.execute(fulladdress + ", UK");
	
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
}

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