﻿
Aris.GMap = Function.extend({
	constructor: function(el, width, height, zoom, callback) {
		this.el = el;
		this.map = null;
		this.width = width;
		this.height = height;
		this.markers = {};
		this.zoom = zoom || 12;
		this.callback = callback;
		Aris.Event.onPageLoad.add(this.loadMap, this);
	},
	loadMap: function() {
	try{
		this.map = new GMap2(Aris.DOM.get(this.el), { size: new GSize(this.width,this.height) });
		this.map.addControl( new GSmallZoomControl());
		this.map.addControl( new GMenuMapTypeControl());

		if ( this.callback ) {
			this.callback(this);
		}
		}catch(e) {}
	},
	setMarker: function(id, lat, lng, center) {
		this.ensureMap();
		var point = new GLatLng(lat, lng);
		var marker = new GMarker(point);
		if ( center === true ) {
			this.map.setCenter(point, this.zoom);
		}
		this.markers[id] = marker;
		this.map.addOverlay(marker);
		return this;
	},
	ensureMap: function() {
		if (this.map == null) { this.loadMap(); }
		return this;
	}
}, {
	init: function() {
		var map = document.getElementById("gmapKey");
		
		if ( map == null) {
		    map = Aris.DOM.create('script', { src: 'http://www.google.com/jsapi?key=ABQIAAAAcyngNcja2U3s9No_jPkSOxSrCOClsNasLn58fjpEC7SyfclnixRsv8wTTkcS8wVx3Mi4-8GiVgMSBw', type: "text/javascript", id: 'gmapKey' });
			document.getElementsByTagName('head')[0].appendChild(map);
			//alert(document.getElementsByTagName('head')[0].lastChild.outerHTML);
			
			//For now: need to call this at the bottom of the aspx page until I figure out why IE all of the sudden doesn't like it
			Aris.Event.add(map, 'load', function () { google.load('maps', '2.x', { "callback": function () { } }) });
			//Aris.Event.onDOMLoaded.add(function() { google.load('maps','2',{callback: this.loadMaps},this);
		}
	},
	loadMaps: function() {
	}
});

