MappingSystem = Class.create();
MappingSystem.prototype = {
	initialize: function(options) {
		this.options = {
			mapDiv: 'ClientMap', //Div that contains the map
			centerPoint: MappingSystem.CreatePoint(39.787071136332834, -104.93157863616943), //Point as witch to center on
			centerZoom: 10, //Map Zoom Depth 1 = Far 20 = Close
			objectName: 'MappingSystemObj' //Class Instance Variable Name
		}
		Object.extend(this.options, options || {});
		
		this.mapObj = null;
		this.PushPins = new Array();
		this.loaded = false;
		
		Event.observe(window, 'load', this.onLoad.bind(this));
		Event.observe(window, 'unload', this.unLoad.bind(this));
	},
	//Window OnLoad Events
	onLoad: function(){
		if (GBrowserIsCompatible()) {
			this.mapObj = new GMap2($(this.options.mapDiv));
			this.mapObj.addControl(new GMapTypeControl());
			this.mapObj.addControl(new GLargeMapControl());
			this.mapObj.setCenter(this.options.centerPoint, this.options.centerZoom);
			for(var i = 0; i<this.PushPins.length; i++){
				var TemplateVars = {
					'zoomCenter': this.options.objectName +'.zoomToCenter('+i+'); return false;'
				}
				this.mapObj.addOverlay(this.PushPins[i].getPushPin(TemplateVars));
			}
			this.loaded = true;
		}
	},
	//Window UnLoad Events
	unLoad: function(){
		GUnload();
	},
	//Adds a PushPin to the Map and Modifies HTML with Events
	addPushPin: function(PushPin){
		this.PushPins.push(PushPin);
		if(this.loaded){
			var TemplateVars = {
				'zoomCenter': this.options.objectName +'.zoomToCenter('+(this.PushPins.length - 1)+'); return false;'
			}
			this.mapObj.addOverlay(PushPin.getPushPin(TemplateVars));
		}
	},
	//Zooms map onto pushpin
	zoomToCenter: function(PushPinID){
		var point = this.PushPins[PushPinID].point;
		this.mapObj.setCenter(point, 15);
	}
}

//Generic Function to create a Google Point
MappingSystem.CreatePoint = function(Latitude, Longitude){
	return new GLatLng(Latitude, Longitude);
}


//Creates a new PushPin
MappingSystemPushPin = Class.create();
MappingSystemPushPin.prototype = {
	initialize: function(Latitude, Longitude, HTML) {
		this.point = MappingSystem.CreatePoint(Latitude, Longitude);
		this.marker = new GMarker(this.point);
		this.template = new Template(HTML);
		this.html = HTML;
	},
	
	getPushPin: function(templateValues){
		var evalHTML = this.template.evaluate(templateValues);
		GEvent.addListener(this.marker, "click", function(){this.marker.openInfoWindowHtml(evalHTML);}.bind(this));
		return this.marker;
	}
}