if (typeof com == "undefined") { 
	com={}; 
}
if (typeof com.digitaria == "undefined") {
	com.digitaria={}; 
}

com.digitaria.gmaps={
	mapRef: null, /* Reference to the maps object */
	hMarkers: {}, /* Hash of all of the markers */
	hDescs: {}, /* Hash of all of the marker descriptions */

	scrollIntoView: function() {
		// If we're scrolled down beneath it, scroll back up to it.
		var gmpos=$('#google_map').position();
		if ($(document).scrollTop()>gmpos.top) {
			$(document).scrollTop(gmpos.top);
		}
	},
	
	getMarkerKeyAtPosition: function(num) {
		count=0;
		for (key in com.digitaria.gmaps.hMarkers) {
			if (count==num) {
				return key;
			}
			count++;
		}
		return "";
	},
	
	getMarkerPositionFromKey: function(key) {
		count=0;
		for (key in com.digitaria.gmaps.hMarkers) {
			if (count==num) {
				return count;
			}
			count++;
		}
		return -1;
	},
	
	showMarker: function(key) {
		// If there is no key, attempt to find the first item.
		if (typeof key=="undefined") {
			key=com.digitaria.gmaps.getMarkerKeyAtPosition(0);
			
			// Bail if it wasn't found.
			if (key=="") {
				if (typeof console != "undefined") {
					if (typeof console.debug != "undefined") {
						console.debug("com.digitaria.gmaps.showMarker: There were no locations found and therefore we couldn't center on the first one.");
					}
				}
				return;
			}
		}
		
		// Scroll in to view on the page
		com.digitaria.gmaps.scrollIntoView();
				
		var thisMarker=com.digitaria.gmaps.hMarkers[key];
		var thisDesc=com.digitaria.gmaps.hDescs[key];
		
		// Center the map
		com.digitaria.gmaps.mapRef.setCenter(com.digitaria.gmaps.hMarkers[key].getLatLng());
		
		// Set the zoom
		com.digitaria.gmaps.mapRef.setZoom(8);
		
		// Show the description
		thisMarker.openInfoWindowHtml(thisDesc);
	},
	
	initLinks: function() {
		// Find all of class "google_map" entries on the page
		$('.google_map').each(function() {
			var loc=$(this).attr('rel');
			var desc=$(this).attr('rev');
						
			// If there isn't a description, use the link contents.
			if (desc=="") {
				desc=$(this).text();
			}
			
			// Split the location in to the two comma separated parts
			var locAry=loc.split(",");
			
			// If there were more than two parts, something went wrong, bail
			if (locAry.length!=2) {
				alert("The location for "+$(this).text()+" was not in lat,long format.");
				return;
			}
			
			// Create a point from the lat/long
			var point=new GLatLng(locAry[0], locAry[1]);
			
			// Add the point to the hash map.
			com.digitaria.gmaps.hMarkers[loc]=new GMarker(point);
			
			// Add the description to the hash map
			com.digitaria.gmaps.hDescs[loc]=desc;
			
			// Bind the click event listener for the marker to pop up the description
			GEvent.addListener(com.digitaria.gmaps.hMarkers[loc], "click", function() {
				com.digitaria.gmaps.hMarkers[loc].openInfoWindowHtml(com.digitaria.gmaps.hDescs[loc]);
			});
			
			com.digitaria.gmaps.mapRef.addOverlay(com.digitaria.gmaps.hMarkers[loc]);
			
			// Bind the click event listener for the link itself to pop up the description too
			$(this).click(function(e) {
								   
				// Cancel the default event behavior
				e.preventDefault();
				
				// Show the marker
				com.digitaria.gmaps.showMarker(loc);
			});
		});
	},
	
	init: function(address) {
		if (document.getElementById('google_map')) {
			if (typeof google=="undefined") {
				alert("The google maps API has not been loaded.");
				return;
			}
						
			// Initialize the map
			com.digitaria.gmaps.mapRef=new google.maps.Map2(document.getElementById('google_map'));
			
			// Add the default map controls
			com.digitaria.gmaps.mapRef.setCenter(new GLatLng(0, 0), 1);
			com.digitaria.gmaps.mapRef.setUIToDefault();
						
			// Set up all of the links, bind them, etc.
			com.digitaria.gmaps.initLinks(); 
			
			// Center the map on the first marker
			//com.digitaria.gmaps.showMarker();
		}
	}
}