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

Math.deg2rad=function(x){return x*(Math.PI/180.0);};// Distance in metres

var EARTH_RADIUS=6367000;// Calculates the distance (in metres) between 2 points.
function distance_between_points(p1,p2)
{
	var a		= Math.deg2rad(90-p1.y);
	var b		= Math.deg2rad(90-p2.y);
	var theta	= Math.deg2rad(p2.x-p1.x);
	var c		= Math.acos(Math.cos(a)*Math.cos(b)+Math.sin(a)*Math.sin(b)*Math.cos(theta));
	return c*EARTH_RADIUS;
}

function saveGeoLocationFromPostcode( postcode )
{
	if( "" != postcode )
	{
		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 );

					geo_element = document.getElementById( "s_geo_location" );
					geo_element.value = point;
				}
				else
				{
					alert( "Postcode '" + postcode + "' not found!" );
				}
			}
		);

		localSearch.execute( postcode + ", UK" );
	}
}


// Called on form submit with callback of AJAX function
function usePointFromPostcode( postcode, callbackFunction )
{
	localSearch.setSearchCompleteCallback(null,
		function()
		{
				gotOne = false;
				document.getElementById( "your_choices" ).innerHTML = "";
				for( resLoop=0; !gotOne && resLoop<localSearch.results.length; resLoop++ )
				{
					//alert( "Checking: " + localSearch.results[resLoop].country );
					if( "GB" == localSearch.results[resLoop].country || "GB" == localSearch.results[resLoop].city )
					{
						//alert( "Got: " + localSearch.results[resLoop].country );
						var resultLat = localSearch.results[resLoop].lat;
						var resultLng = localSearch.results[resLoop].lng;
						var point = new GLatLng(resultLat,resultLng);
						document.getElementById( "your_location" ).value = point;
						gotOne = true
						callbackFunction(point);
					}

					//var node = localSearch.results[resLoop].html.cloneNode(true);	// clone the .html node from the result
					//document.getElementById( "your_choices" ).appendChild(node);	// attach the node into my dom
				}

				if( !gotOne )
					alert("Sorry, that place cannot be found. Try using your full postcode.");
				else
				{
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
					document.getElementById( "your_location" ).value = point; //.toUrlValue();
					callbackFunction(point);

				}

			/*
			if( 0 == localSearch.results.length )
			{
				alert("Sorry, that place cannot be found. Please try using your full postcode.");
			}
			else
			{
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				document.getElementById( "your_location" ).value = point; //.toUrlValue();
				callbackFunction(point);
			}*/
			/*
			if ( 1 == localSearch.results.length )
			{
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				document.getElementById( "your_location" ).value = point; //.toUrlValue();
				callbackFunction(point);
			}
			else
			{
				gotOne = false;
				document.getElementById( "your_choices" ).innerHTML = "";
				for( resLoop=0; !gotOne && resLoop<localSearch.results.length; resLoop++ )
				{
					//alert( "Checking: " + localSearch.results[resLoop].country );
					alert( ""+localSearch.results[resLoop].country );
					if( "UK" == localSearch.results[resLoop].country )
					{
						alert( "Got: " + localSearch.results[resLoop].country );
						var resultLat = localSearch.results[resLoop].lat;
						var resultLng = localSearch.results[resLoop].lng;
						var point = new GLatLng(resultLat,resultLng);
						document.getElementById( "your_location" ).value = point;
						gotOne = true
						callbackFunction(point);
					}

					var node = localSearch.results[resLoop].html.cloneNode(true);				// clone the .html node from the result
					document.getElementById( "your_choices" ).appendChild(node);	// attach the node into my dom

					//document.getElementById( "your_choices" ).innerHTML = "";
					//document.getElementById( "your_choices" ).innerHTML = document.getElementById( "your_choices" ).innerHTML + "<br>" + localSearch.results[ resLoop ].title + ", " + localSearch.results[ resLoop ].country + "->" + point; //.toUrlValue();
				}

				if( !gotOne )
					alert("Sorry, that place cannot be found. Try using your full postcode.");
			}*/
		});

	localSearch.execute( postcode + ", UK" );
}


function old_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);
				document.getElementById( "your_location" ).value = point; //.toUrlValue();
				//alert( "your loc: " + document.getElementById( "your_location" ).value );
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});

	localSearch.execute(postcode + ", UK");
}



function update_map( point )
{

	var obj = document.getElementById( "your_location" );
	obj.innerHTML = point;

	placeMarkerAtPoint(point);
	setCenterToPoint( point );
}

function append_point( name, point )
{

}

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

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

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

function mapLoad() {
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));

		map.addControl(new GSmallMapControl());
		//	map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 5);

		// allow the user to select to/from for driving directions
		/*var options = {
			  zoomControl : GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL,
			  title : "Marks House",
			  url : "http://www.google.com/corporate/index.html",
			  idleMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM,
			  activeMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM,
			  drivingDirections : GSmapSearchControl.DRIVING_DIRECTIONS_FROM_USER,
			  showResultList : GSmapSearchControl.DEFAULT_RESULT_LIST
			  }

		new GSmapSearchControl(
			  document.getElementById("map"),
			  "1231 Lisa Lane, Los Altos CA",
			  options
			  );
		*/
	}
}

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