/* GOOGLE MAPS */
var GM = {
	map: {}, 
	directions: {}, 
	actualSpot: "",
	actualHtml: "",
	actualAddress: "",
	createMarker: function(point,html) {
		var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
			actualSpot = marker;
			actualHtml = html;
		});
		markers.push(marker);
		return marker;
	},
	init: function(){
		markers = [];
		if (GBrowserIsCompatible()) {
			GM.map = new GMap2(document.getElementById("map_canvas"));
			GM.map.setCenter(new GLatLng(mLatitude, mLongitude), zoomLevel);
			GM.map.addControl(new GSmallMapControl());
			GM.directions = new GDirections(GM.map, document.getElementById("directions"));
			GEvent.addListener(GM.directions, "load", GM.onGDirectionsLoad);
			GEvent.addListener(GM.directions, "error", GM.handleErrors);
				var point = new GLatLng(latitude, longitude);
				var html = '<div id="bubble"><h2>' + title + '</h2><p>' + address + '</p>';
				html += '<form action="#" onsubmit="GM.setDirections(this.userAddress.value, this.mapAddress.value, this.dirType.value); return false;"><div style="width: 250px;">Get directions:&nbsp;&nbsp;<span onclick="GM.setFromAndTo(\'to\')" id="toHere">To here</span> - <span onclick="GM.setFromAndTo(\'from\')" id="fromHere">From here</span></div><div id="dirsForm" style="display: none;"><input type="text" name="userAddress" id="userAddress" value="" style="width: 220px;height: 16px;" onclick="GM.test(this);" /><input type="hidden" name="mapAddress" value="' + address + '" /><input type="hidden" name="dirType" id="dirType" value="to" /><input type="submit" value="GO" /><br /><span class="example">Example: 10 Strawberry Road, Falmouth, MA</span></div></form></div>';
				var newMarker = GM.createMarker(point, html, active);
				GM.map.addOverlay(newMarker);
				newMarker.openInfoWindowHtml(html);
				actualSpot = newMarker;
				actualHtml = html;
		}
	},
	test: function(link) {
		link.value = "";
	}, 
	setFromAndTo: function(type){
		$("#dirType").attr("value", type);
		$("#userAddress").attr("value", ((type=="to")?"Type your start address":"Type your end address"));
		if(type=="to") {
		 	$("#toHere").addClass("bold");
		 	$("#fromHere").removeClass("bold");
		} else {
			$("#fromHere").addClass("bold");
			$("#toHere").removeClass("bold");
		}
		$("#dirsForm").show();
		GM.map.updateInfoWindow();
		return false;
	},
	setDirections: function(userAddress, mapAddress, type){
		GM.directions.load("from: " + ((type=="to")?userAddress:mapAddress) + " to: " + ((type=="to")?mapAddress:userAddress), { "locale": "en" });
		return false;
	}, 
	handleErrors: function(){
		if (GM.directions.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.\nError code: " + GM.directions.getStatus().code);
		else if (GM.directions.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.\n Error code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_MISSING_QUERY)
			alert("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: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_BAD_KEY)
			alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + GM.directions.getStatus().code);
		else if (GM.directions.getStatus().code == G_GEO_BAD_REQUEST)
			alert("A directions request could not be successfully parsed.\n Error code: " + GM.directions.getStatus().code);
		else alert("An unknown error occurred.");
	}, 
	onGDirectionsLoad: function(){
		GM.map.clearOverlays();
		document.getElementById("directions").style.width = "375px";
		GM.map.getContainer().style.width="350px";
		GM.map.checkResize();
		$("#directions").prepend('<span id="dirPrint">Print directions</span>');
		$("#directions").prepend('<span id="dirReset">&#171; Back to map</span>');
		$("#gmTitle").html('Directions');
		
		$("#dirReset").click(function() {
			GM.map.clearOverlays();
			$("#directions").html('');
			document.getElementById("directions").style.width = "0px";
			GM.map.getContainer().style.width="725px";
			$("#gmTitle").html('Map');
			$("#dirPrint").remove();
			$("#dirReset").remove();
			GM.map.checkResize();
			jQuery.each(markers, function() {
				GM.map.addOverlay(this);
				actualSpot.openInfoWindowHtml(actualHtml);
			});
		});
		$("#dirPrint").click(function() {
			var dirPrintWindow = window.open('','directions','width=500,height=600,scrollbars=yes,resizable=yes');
			var dirPrintWindowHtml = '<html><head><title>Directions</title><style>#dirPrint, #dirReset {display: none;} \n div, a { cursor: default !important;  }</style></head><body><h1 style="color: #999; font-size: 20px; margin-bottom: 10px;">Directions</h1>' + $("#directions").html() + '</body></html>';
  			dirPrintWindow.document.write(dirPrintWindowHtml);
			dirPrintWindow.document.close();
			dirPrintWindow.print();
			return true;
		});
	}
}