/**
 * @author Nick Verbeck
 * @since 5/6/2009
 */

function EmbededMaps(){
	this.selector = "div.EmbededMap";
	this.staticUrl = 'http://maps.activewebsite.com/googlestatic.php';
	this.maps = new Array();
	this.init();
}
EmbededMaps.prototype.init = function(){
	$$(this.selector).each(function(element){
		var params = {format: 'png',maptype: 'roadmap', zoom: '15'}
		if(element.attributes['format'] != undefined && element.attributes['format'] != null){params.format = element.attributes['format'].nodeValue;}
		if(element.attributes['zoom'] != undefined && element.attributes['zoom'] != null){params.zoom = element.attributes['zoom'].nodeValue;}
		if(element.attributes['location'] != undefined && element.attributes['location'] != null){
			params.center = element.attributes['location'].nodeValue;
		} else {
			params.center = '39.73042572969996,-104.99908447265625';
		}
		if(element.attributes['zoom'] != undefined && element.attributes['zoom'] != null){params.zoom = element.attributes['zoom'].nodeValue;}
		if(element.attributes['markers'] != undefined && element.attributes['markers'] != null){params.markers = element.attributes['markers'].nodeValue;}
		if(element.attributes['maptype'] != undefined && element.attributes['maptype'] != null){params.maptype = element.attributes['maptype'].nodeValue;}
		
		params.size = element.style.width.replace('px', '') +'x'+ element.style.height.replace('px', '');
		
		var image = document.createElement('img');
		var url = this.staticUrl +'?'+ $H(params).toQueryString();
		image.src = url;
		image.style.height = element.style.height;
		image.style.width = element.style.width;
		
		element.appendChild(image);
	}.bind(this));
	google.load("maps", "2");
	google.setOnLoadCallback(function(){this.mapInitialize();}.bind(this));
	Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
}
EmbededMaps.prototype.onLoad = function(){
	Event.observe(window, 'unload', this.unLoadMap.bindAsEventListener(this));
}
EmbededMaps.prototype.mapInitialize = function(){
	if (GBrowserIsCompatible()) {
		$$(this.selector).each(function(element){
			if (element.attributes['dynamic'] != undefined && element.attributes['dynamic'] != null && element.attributes['dynamic'].nodeValue == 'true') {
				if (element.attributes['location'] != undefined && element.attributes['location'] != null) {
					var location = element.attributes['location'].nodeValue.split(',');
				}
				else {
					var location = '39.73042572969996,-104.99908447265625'.split(',');
				}
				location = new google.maps.LatLng(location[0], location[1]);
				
				if (element.attributes['zoom'] != undefined && element.attributes['zoom'] != null) {
					var zoom = element.attributes['zoom'].nodeValue;
				}
				else {
					var zoom = 15;
				}
				var map = new google.maps.Map2(element);
				map.addMapType(G_PHYSICAL_MAP);
				map.enablePinchToZoom();
				map.setCenter(location, zoom);
				map.enableScrollWheelZoom();
				map.addControl(new GLargeMapControl3D());
				map.addControl(new GHierarchicalMapTypeControl());
				map.addControl(new GScaleControl());
				
				if (element.attributes['markers'] != undefined && element.attributes['markers'] != null) {
					$A(element.attributes['markers'].nodeValue.split('|')).each(function(point){
						var location = point.split(',');
						location = new google.maps.LatLng(location[0], location[1]);
						map.addOverlay(new GMarker(location));
					}.bind(this));
				}
				this.maps[this.maps.length] = map;
			}
		}.bind(this));
	}
}
EmbededMaps.prototype.unLoadMap = function(){GUnload();}
var EmbededMapsObj = new EmbededMaps();
