/*

Global Variables:
	- gmap_element_id: The ID attribute value of the HTML element you want the map to occupy. 
	- gmap_center_lat, gmap_center_lng: Latitude and Longitude values.  Ignore if you plan to use gmap_loc_description to geocode.
	- gmap_location_name: The name of the location.  This is put into the info window.
	- gmap_loc_description: Address of location.  If you geocode and the address can't be found, Birmingham is set as the center and the window.status is updated as a warning.
	- gmap_center_zoom: A zoom number.  Defaults to gmap.defaultZoom.
	- gmap_offer_directions: True or false to allow users to get directions.

*/

gmap = {
	map: false,
	defaultPoint: new GLatLng(33.499463,-86.793537),
	defaultZoom: 13,
	init: function() {
		if (GBrowserIsCompatible() && window.gmap_element_id) {
			gmap.map = new GMap2(document.getElementById(gmap_element_id));
			gmap.map.addControl(new GLargeMapControl);
			if(window.gmap_center_lat && window.gmap_center_lng) {
				var point = new GLatLng(gmap_center_lat, gmap_center_lng);
				gmap.setMarker(point, 
					window.gmap_loc_description ? gmap_loc_description: false, 
					window.gmap_location_name ? gmap_location_name : false);
				if(!window.gmap_loc_description) {
					var geocoder = new GClientGeocoder();
					geocoder.getLocations(new GLatLng(gmap_center_lat, 
						gmap_center_lng),
						function(response) {
							if (!response || response.Status.code != 200) {
								alert('An error occurred while gathering the address of this location.');
								location.reload();
							} else {
								var place = response.Placemark[0];
								window.gmap_loc_description = place.address;
							}
						});
				}
			} else if(window.gmap_loc_description) {
				gmap.geocode(gmap_loc_description);
			} else {
				gmap.map.setCenter(gmap.defaultPoint, 
					window.gmap_center_zoom ? gmap_center_zoom : gmap.defaultZoom);
			}
		}
	},
	setMarker: function(point, p_text, p_title) {
		gmap.map.setCenter(point, 
			window.gmap_center_zoom ? gmap_center_zoom : gmap.defaultZoom);
		var marker = new GMarker(point);
		gmap.map.addOverlay(marker);
		if(p_text || p_title) {
			marker.openInfoWindowHtml(
				(p_title ? '<strong>' + p_title + '</strong><br>' : '') + 
				(p_text ? p_text : '') + 
				(window.gmap_offer_directions && gmap_offer_directions ? gmap.addDir() : ''));
		}
	},
	addDir: function() {
		ret = '';
		ret += '<hr><form method="post" action="" onsubmit="gmap.getDir(this); return false;">';
		ret += '<div><label><strong>Get Directions</strong><br>';
		ret += '<input type="text" name="loc">';
		ret += '</label><input type="submit" value="Go"></div>';
		ret += '</form>';
		return ret;
	},
	getDir: function(dir_form) {
		gmap.map.clearOverlays();
		var addr = dir_form.loc.value;
		var directionsPanel = document.createElement('div');
		var map_cont = document.getElementById(gmap_element_id);
		map_cont.parentNode.insertBefore(directionsPanel, map_cont.nextSibling);
		var directions = new GDirections(gmap.map, directionsPanel);
		directions.load('from: ' + addr + ' to: ' + gmap_loc_description);
		gmap.map.closeInfoWindow();
	},
	geocode: function(address) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					window.status = address + " Not Found!";
					gmap.map.setCenter(gmap.defaultPoint, 
						window.gmap_center_zoom ? gmap_center_zoom : gmap.defaultZoom);
				} else {
					alert('The GPS location is: ' + point + '.  Please enter this into your JavaScript code.');
					gmap.map.setCenter(point, gmap_center_zoom);
					gmap.setMarker(point, address, 
						window.gmap_location_name ? gmap_location_name : false);
				}
			}
		);
	}
}

if(window.addEventListener) {
	window.addEventListener('load', gmap.init, false);
} else if(window.attachEvent) {
	window.attachEvent('onload', gmap.init);
}