jQuery(document).ready(init);

function init(){
	jQuery("#map").empty();
	jQuery("#dirForm").append('<form id="getDir" action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false"><input class="text" type="text" id="fromAddress" name="from" value="Your Address"><input class="text" type="hidden" id="toAddress" name="to" value="180 Preston Street, Ottawa, Ontario" disabled="disabled"><select id="locale" name="locale"><option value="en" selected>English</option><option value="fr">French</option><option value="it">Italian</option><input name="submit" type="submit" value="Get Directions!" /></form>');
	createMap()
}

function createMap(){
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		gdir = new GDirections(map, document.getElementById("directions"));
		map.setCenter(new GLatLng(45.40661, -75.71291), 16);
		map.setUIToDefault();
		GEvent.addListener(gdir, "error", handleErrors);
		point = new GLatLng(45.406289, -75.713000)
		map.addOverlay(new GMarker(point));
	}
}

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("Could not find your address, please make sure you have the full adress including city and province. Also make sure to put the street suffix like ave or st. ");
	}
}
