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 handleClicks(marker, point){
	lat = point.lat();
	lng = point.lng();
	reverse(point);
	//add_steps(point.lat() + ", " + point.lng());
}


function ispostcode(postcode){
var re = new RegExp('^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$');
if (postcode.match(re)) {
return true;
} else {
return false;
}
}

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);
				document.getElementById('submit').disabled=false;
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function checkpostcode(postcode) {
	if (ispostcode(postcode) == true){
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{
				document.getElementById('lat').value = localSearch.results[0].lat;
				document.getElementById('lng').value = localSearch.results[0].lng;
				document.getElementById('submit').disabled=false;
			}else{
				alert('Postcode not found!');
			}
		});	
	
	localSearch.execute(postcode + ", UK");
	} else {
	alert('Postcode incorrect!');
	}
}

function search_place(name){
	if (name != ""){
		localSearch.setSearchCompleteCallback(null,
			function() {
				if (localSearch.results[0])
					{
						lat = localSearch.results[0].lat.substr(0,7);
						lng = localSearch.results[0].lng.substr(0,7);
						var point = new GLatLng(lat,lng);
						//placeMarkerAtPoint(point);
						setCenterToPoint(point);
						reverse(point);
					} else {
						info.innerHTML = "<p class=error>Unable to find place, please click a road!</p>";
					}
				}
			);
			
		localSearch.execute(name + ", Cheshire,  UK");
		}
}

function search_event(name){
if (ispostcode(name) == true){
		localSearch.setSearchCompleteCallback(null,
			function() {
				if (localSearch.results[0])
					{
						lat = localSearch.results[0].lat;
						lng = localSearch.results[0].lng;
						var point = new GLatLng(lat,lng);
						//placeMarkerAtPoint(point);
						setCenterToPoint(point);
						reverse(point);
					} else {
						info.innerHTML = "<p class=error>Unable to find location, please click nearest road!</p>";
					}
				}
			);
			
		localSearch.execute(name + ",  UK");
		} else {
		info.innerHTML = "<p class=error>Postcode Incorrect, Please use full postcode (WA11 1EX) to search!</p>";
		}
}

function search_postcode_latlng(name){
if (ispostcode(name) == true){
		localSearch.setSearchCompleteCallback(null,
			function() {
				if (localSearch.results[0])
					{
						lat = localSearch.results[0].lat;
						lng = localSearch.results[0].lng;
						var point = new GLatLng(lat,lng);
						//placeMarkerAtPoint(point);
						setCenterToPoint(point);
						placeMarkerAtPoint(point);
						document.getElementById("lat").value = point.lat();
						document.getElementById("lng").value = point.lng();
					} else {
						info.innerHTML = "<p class=error>Unable to find location, please click nearest road!</p>";
					}
				}
			);
			
		localSearch.execute(name + ",  UK");
		} else {
		info.innerHTML = "<p class=error>Postcode Incorrect, Please use full postcode (WA11 1EX) to search!</p>";
		}
}

var baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);

function createMarker(point, letter, title, desc) {  
	// Create a lettered icon for this point using our icon class  	
	var letteredIcon = new GIcon(baseIcon);
	letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png"; 
	// Set up our GMarkerOptions object  
	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<b>" + title + "</b>" + desc);
		});
	return marker;
}

function createRailMarker(point, title, address, tel) {  
	// Create a lettered icon for this point using our icon class  	
	var letteredIcon = new GIcon(baseIcon);
	letteredIcon.image = "http://maps.google.com/mapfiles/ms/micons/rail.png"; 
	letteredIcon.shadow  = "http://maps.google.com/mapfiles/ms/micons/rail.shadow.png"; 
	letteredIcon.iconSize = new GSize(32, 32);
	letteredIcon.shadowSize = new GSize(59, 32);

	// Set up our GMarkerOptions object  
	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("<div style='width: 250px; margin-bottom:0px;'><b>" + title + " Railway Station</b><br/>" + address + tel+"</div>");
		});
	return marker;
}

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


