/* This library provides functionality for the google_map template tag.
 *
 * It has the following dependancies:
 * 1. mootools 1.2
 *  utils/mootools/1.2/core.js
 * 2. google maps v2 api
 *  http://maps.google.com/maps?file=api&amp;v=2&amp;key=
 *
 * To use, add the following to your document <head>:
 * 
 * var mapManager = new MapManager(map_div_id, center_lat, center_long, zoom, media_url);
 *
 * Where map_div_id is the id of the div you want to contain the map, center_lat
 * and center_long are the coordinates that the map will use to center itself,
 * and zoom is an integer value representing the Google Maps zoom level.The 
 * media_url parameter is MEDIA_URL value that you should have in context.
 * 
 * To add points to the map, you'll want to add locations in your template by 
 * calling:
 * 
 * mapManager.addLocation(lat, lng, html, region, id);
 * 
 * Where lat,lng on the coordinates of the point, and html is the snippet
 * of HTML to be used in the pin popup.
 *
 * These calls should be made after this file is imported into the page, and
 * before the body onLoad event is raised, which can usually be accomplished
 * by just putting <script></script> tags with the addLocation call anywhere
 * in the page (afer the var mapManager = new MapManager() call, of course!)
 */
 
/* Regions Map */
var regions = { 'East':'blue', 'Southeast':'brown', 'Middle':'darkgreen', 'West':'orange', 'Northeast':'yellow', 'At Large':'pink' };

var MapManager = Class({
	getIconImage : function(index, color){
		alpha_index = index % 26;
		var letter = String.fromCharCode("A".charCodeAt(0) + alpha_index);
		marker = 'marker';
		if (color) {
			marker = "markers/"+color+"_Marker";
		}
		return this.media_url + "images/mapping/" + marker + letter + ".png";
	},
	
	createMarker : function(lat, lon, html, index, region) {
		var point = new GLatLng( lat, lon )
		var icon = new GIcon();
		icon.shadow = this.media_url + "images/mapping/shadow50.png";
		icon.iconSize = new GSize(20, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(9, 34);
		icon.infoWindowAnchor = new GPoint(9, 2);
		icon.infoShadowAnchor = new GPoint(18, 25);
		
		var letteredIcon = new GIcon(icon);
		letteredIcon.image = this.getIconImage(index, regions[region]);

		var marker = new GMarker(point, {icon:letteredIcon});
		GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
		
		return marker;
	},
	
	mapLoad : function() {
		if (GBrowserIsCompatible()) {
	 	 	this.map = new GMap2($(this.map_div));
			this.map.setMapType(this.map_type);
	 	 	this.map.setCenter(new GLatLng(this.center_lat, this.center_long), this.zoom_level);
	 	 	this.map.addControl(new GSmallMapControl());

			for (i=0; i<this.locations.length; i++)
			{
				var l = this.locations[i]
				var marker = this.createMarker( l['lat'], l['lng'], l['html'], i, l['region']);
				this.map.addOverlay( marker );
				this.locations[i]['image'] = marker.getIcon().image;
			}
			
			if (this.mapLoadCallback) {
				this.mapLoadCallback();
			}
		}
	},
	
	addLocation : function(lat, lng, html, region, id) {
		//this.locations.include({'lat':lat, 'lng':lng, 'html':html, 'region':region, 'id':id});
        this.locations.include({'lat':lat, 'lng':lng, 'html':html, 'region':region, 'id':id});
	},
	
	addMaploadCallback : function(func) {
		this.mapLoadCallback = func;
	},
	
	initialize : function(map_div, center_lat, center_long, zoom_level, media_url, map_type)
	{
		this.media_url = media_url;
		this.map_div = map_div;
		this.center_lat = center_lat;
		this.center_long = center_long;
		this.zoom_level = zoom_level;
		this.locations = Array();
		this.map_type = map_type;
		
		var instance = this;
		window.addEvent('load', function() { instance.mapLoad(); });
		window.addEvent('unload', GUnload);
	}
});
