// * by Craig Cordell
// *
// * USAGE
// *
/*
		// create markers array using markerInfo constructor
		var map_markers = new Array();
		map_markers[0] = new markerInfo(37.11, -122.111, "hello world");
		
		if (GBrowserIsCompatible()) { 
			$("#googleMapDiv").googleMap(37.11, -122.111, 5, {
				controls: ["GSmallMapControl", "GMapTypeControl"],
				markers: map_markers
			});		
		};
		
		// example to access the map object
		// $.googleMap.maps["googleMapDiv"].setMapType(G_SATELLITE_TYPE);

*/		


$(document).ready(function() {

    $("#submit_directions, #submit_directions_reverse").click(function () { 
		
		var from;
		var to;
		
		from = $(this).siblings("input[id^='directions_from']").val();
		to = $(this).siblings("input[id^='directions_to']").val();
						
		directions(from, to);
    });

    $("#reverse_directions").click(function () { 
		
		var from;
		var to;
		var temp;
		
		from = $("input[id='directions_from']").val();
		to = $("input[id='directions_to']").val();
		
		temp = to;
		to = from;
		from = temp;		
		
		$("input[id='directions_from']").val(from);
		$("input[id='directions_to']").val(to);		
						
		directions(from, to);
    });

});





function handleDirectionsErrors(){
	
   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.\nError code: " + gdir.getStatus().code);
   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.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.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: " + gdir.getStatus().code);
	 
   else if (gdir.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: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
   
}



function directions(from, to) {

	var map;
	var directionsPanel;
	var directions;

	map = new GMap2(document.getElementById("tab1"));
  	
	directionsPanel = document.getElementById("directions");
	directionsPanel.innerHTML  = "";
	
  	map.setCenter(new GLatLng(49.496675,-102.65625), 3);
  	gdir = new GDirections(map, directionsPanel);
	GEvent.addListener(gdir, "error", handleDirectionsErrors);
  	gdir.load( "from: " + from + " to: " + to );
}




function markerInfo( lat, lng, txt ) { 
	this.lat = lat; 
	this.lng = lng; 
	this.txt = txt; 
}  



$.googleMap = {
	maps: {},
	marker: function(m) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return null;
		} else {	
			var marker = new GMarker(new GLatLng( parseFloat(m.lat), parseFloat(m.lng) ));
			if (m.txt) {
				GEvent.addListener(marker, "click", function() {
    				marker.openInfoWindowHtml(m.txt);
  				});
			}
			return marker;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) {
		alert("Sorry, this browser is not compatible with Google maps");
		return this;
	}

	// Default values make for easy debugging
	if (lat == null) lat = 37.4419;
	if (lng == null) lng = -122.1419;
	if (!zoom) zoom = 13;
	
	// Sanitize options
	if (!options || typeof options != 'object')	options = {};
	options.mapOptions = options.mapOptions || {};
	options.markers = options.markers || [];
	options.controls = options.controls || {};

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
		// Create a map and a shortcut to it at the same time
		var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
		// Center and zoom the map
       	map.setCenter(new GLatLng(lat, lng), zoom);
       	// Add controls to our map
       	for (var i = 0; i < options.controls.length; i++) {
	       	var c = options.controls[i];
	       	eval("map.addControl(new " + c + "());");
       	}
       	// If we have markers, put them on the map
       	var marker = null;
       	for (var i = 0; i < options.markers.length; i++) {
	       	if (marker = $.googleMap.marker(options.markers[i])) map.addOverlay(marker);
       	}
    });
	
	$(window).unload( function () { GUnload(); } );

};
