
/*
*
* Copyright (c) 2007 Seth Van Booven
*
* ProsperMag.com GoogleMaps
* Version 1.0 (2007-08-15)
* 
*/

var googleMap = {
	map : null,
	baseIcon : null,
	ico : null,
	bounds : new GLatLngBounds(),
	c : 0,
	load : function(id, mapctrl, overview, ico) {
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById(id));
			if (mapctrl.toLowerCase() == 'large') {
				this.map.addControl(new GLargeMapControl());
			} else {
				this.map.addControl(new GSmallMapControl());
			}
			this.map.addControl(new GMapTypeControl());
			if (overview == 1) {
				this.map.addControl(new GOverviewMapControl());
			}
			this.map.setCenter(new GLatLng(38.577410, -121.491720), 10);
			
			this.ico = ico;
			this.baseIcon = new GIcon();
			this.baseIcon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
			this.baseIcon.iconSize = new GSize(20, 34);
			this.baseIcon.shadowSize = new GSize(37, 34);
			this.baseIcon.iconAnchor = new GPoint(9, 34);
			this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
			this.baseIcon.infoShadowAnchor = new GPoint(18, 25);
		}
	},
	draw : function(dataObj) {
		this.map.clearOverlays();
		var geocoder = new GClientGeocoder();
		for (var i = 0; i < dataObj.addresses.length - 1; i++) {
			var _this = this;
			geocoder.getLocations(dataObj.addresses[i].address, function(response) {
				if (!response || response.Status.code != 200) {
					//alert("Sorry, we were unable to geocode that address");
				} else {
					var place = response.Placemark[0];
					var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					if (this.ico == 1) {
						var letter = String.fromCharCode('A'.charCodeAt(0) + _this.c);
						var icon = new GIcon(_this.baseIcon);
						icon.image = 'http://www.google.com/mapfiles/marker' + letter + '.png';
						var marker = new GMarker(point, icon);
					} else {
						var marker = new GMarker(point);
					}
					marker.html = dataObj.addresses[_this.c].html;
					_this.map.addOverlay(marker);
					_this.bounds.extend(marker.getPoint());
					GEvent.addListener(marker, 'click', function() {
						marker.openInfoWindowHtml(this.html);
					});
					_this.map.setZoom(_this.map.getBoundsZoomLevel(_this.bounds));
      				_this.map.setCenter(_this.bounds.getCenter());
					_this.c++;
				}
			}
			);
		}
	},
	create : function(id, data, mapctrl, overview, ico) {
		googleMap.load(id, mapctrl, overview, ico);
		googleMap.draw(data);
	}
}
