$(document).ready(function(){
	
	var lat = 54.82244789281753;
	var lon = -1.6663169860839844;
	var map;
	var gdir;
	
	if (GBrowserIsCompatible()) {      
	    map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(lat,lon),11);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		// Add the marker
		var point = new GLatLng(lat,lon);
		var marker = new GMarker(point,{draggable:false});		
		map.addOverlay(marker);
		
		// Create the directions object
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "error", handleErrors);

	}
	
	$('#directions').submit(function(){
		var fromAddress = $('#frmAddr').val();
		var toAddress = lat + ',' + lon;
		setDirections(fromAddress,toAddress,"en_GB");
		return false;
	});
	
	function setDirections(fromAddress,toAddress, locale) {
	    gdir.load("from: " + fromAddress + " to: " + toAddress,
	              { "locale": locale });
		return false;
	}
	
	function handleErrors (){
		if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
			alert('No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.');
		else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
			alert('A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.');
		else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
			alert('Empty query, please enter your address')
		else if (gdir.getStatus().code == G_GEO_BAD_KEY)
			alert('Bad Key')
		else
			alert('error');
	}
});
