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);

var iconhome = new GIcon();
iconhome.image = "http://www.facefeeder.com/images/home.png";
//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
iconhome.iconSize = new GSize(24, 24);
//icon.shadowSize = new GSize(22, 20);
iconhome.iconAnchor = new GPoint(12, 24);
iconhome.infoWindowAnchor = new GPoint(12, 12);

var iconstar = new GIcon();
iconstar.image = "http://www.facefeeder.com/images/starpin.png";
//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
iconstar.iconSize = new GSize(24, 24);
//icon.shadowSize = new GSize(22, 20);
iconstar.iconAnchor = new GPoint(12, 24);
iconstar.infoWindowAnchor = new GPoint(12, 12);

var blankpin = new GIcon();
blankpin.image = "http://www.facefeeder.com/images/blankpin.png";
//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
blankpin.iconSize = new GSize(24, 24);
//icon.shadowSize = new GSize(22, 20);
blankpin.iconAnchor = new GPoint(12, 24);
blankpin.infoWindowAnchor = new GPoint(12, 12);


// Creates a marker at the given point with the given number label
function createMarker(point, label) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(label);
  });
  return marker;
}

// Creates a home marker at the given point with the given number label
function createCustomMarker(point, label, icon) {
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(label);
  });
  return marker;
}
		
function usePointFromPostcode(postcode, callbackFunction) {
	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{		
    			//alert("Latitude = " + localSearch.results[0].lat.toString() + ", Longitude = " + localSearch.results[0].lng.toString());
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
    			alert("No location matched");
				/*var resultLat = -1;
				var resultLng = -1;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point); */
				history.back();
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function setInputFields(point)
{
	document.getElementById("latitude").value = point.lat();
	document.getElementById("longitude").value = point.lng();
	if(checkAndSubmitForm)
	    checkAndSubmitForm();
}

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());
}


