// JavaScript Document

var map;

var gdir;

var geocoder = null;

var addressMarker;



$(document).ready(function() {

	if (GBrowserIsCompatible()) {      

		map = new GMap2(document.getElementById("map_canvas"));

		gdir = new GDirections(map, document.getElementById("directions"));
		
		GEvent.addListener(gdir, "error", handleErrors);        

		geocoder = new GClientGeocoder();

		map.addControl(new GLargeMapControl())


	}

});

function showAddress(address) {

	if (geocoder) {

		geocoder.getLatLng(

			address,

			function(point) {

				if (!point) {

					alert(address + " not found");

				} else {

					map.setCenter(point,15);

					var marker = new GMarker(point);
					
					var icon = new GIcon(G_DEFAULT_ICON);
					icon.image = "/images/icon.png";
		
					// Set up our GMarkerOptions object
					markerOptions = { icon:icon };


					map.addOverlay(marker);

					marker.openInfoWindowHtml(address);
					
				}

			}

		);

	}

}

function setDirections(fromAddress, toAddress, locale) {

	gdir.load("from: " + fromAddress + " to: " + toAddress);

}



function handleErrors(){

	

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)

		$('map_div').text("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.\nError code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)

		$('map_div').text("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

	 

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)

		$('map_div').text("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)

		$('map_div').text("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)

		$('map_div').text("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

		

	else $('map_div').text("An unknown error occurred.");

	 

}
