$(function () {
	url = window.location.href;

	if (GBrowserIsCompatible()) {
		$(document.body).unload("GUnload");

		$('.google_map').each(function () {
			var currentMap = $(this);
			var address = $('.address', currentMap).html();
			var coords = $('.address_alt_coords', currentMap).html();
			var map = $('.map', currentMap).css('width',520).css('height',300).css('border', '1px solid #999896');

			if ( (address != "" || coords != "") && map ) {
				DI_Google_Maps.createMap(address, coords, map[0]);
			}

		});
	}

	if (DI_Google_Maps.errors != "") alert(DI_Google_Maps.errors + "\n\nnot found!");
});

var DI_Google_Maps = {

	errors: "",
	geocoder: new GClientGeocoder(),
	maps: new Array(),
	
	createMap: function(address, altCoords, mapElement) {
	
		var gMapElement = new GMap2(mapElement);
		gMapElement.addControl(new GSmallMapControl());
		gMapElement.addControl(new GScaleControl());
		gMapElement.addControl(new GMapTypeControl());

		this.maps[address] = {
			'address': address,
			'altCoords': ((altCoords && altCoords != "") ? altCoords.split(',') : null),
			'pointBounds': new GLatLngBounds(),
			'map': mapElement,
			'gMap': gMapElement
		};
		
		this.showAddress(this.maps[address]);
	},

	showAddress: function(map) {
		var point = null;
		var cleanAddress = this.tagRemove(map.address);
	
		if (map.altCoords) {
			this.showAddressHelper(map, new GLatLng(map.altCoords[0], map.altCoords[1]));
		} else {
			this.geocoder.getLatLng(cleanAddress, function(gPoint) {
				if (!gPoint) {
					DI_Google_Maps.notFound(cleanAddress);
				} else {
					DI_Google_Maps.showAddressHelper(map,gPoint);
				}
			});
		}
	},
	
	showAddressHelper: function(map, point)
	{
		var marker = new GMarker(point);
		map.pointBounds.extend(point);
		map.gMap.setCenter(map.pointBounds.getCenter(), 13);
		map.gMap.addOverlay(marker);
		GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml("<strong>" + map.address + "</strong>"); } ); 
	},
	
	notFound: function(str) {
		DI_Google_Maps.errors += str + "\n";
	},
	
	tagRemove: function(str)
	{
		return str.replace(/\<(.*)\>/gi, " ")
			.replace(/\((.*)\)/gi, " ")
			.replace(/\&\#(\d*)\;/, " ")
			.replace(/\n/gi, "");
	},

	tagReplace: function(str)
	{
		var result = (str == "") ? "&#160;" : str;
		return result.replace(/\(br\)/gi, "<br />")
						.replace(/\(sup\)/gi, "<sup>")
						.replace(/\(\/sup\)/gi, "</sup>")
						.replace(/\(bold\)/gi, "<strong>")
						.replace(/\(\/bold\)/gi, "</strong>")
						.replace(/\(ital\)/gi, "<em>")
						.replace(/\(\/ital\)/gi, "</em>");
	},
	
	http_geocoder: {
		apiKey: null,
		latlng: null,
		setKey: function(key) {
			this.apiKey = key;
		},
		getLatLng: function(address, callback) {
			this.latlng = null;
			if (!this.apiKey) { alert("The Google Maps API key must be set prior to creation of a map!"); return null; }
			this.syncGetJSON("http://maps.google.com/maps/geo", {q:address, sensor:false, output:'json', key:this.apiKey}, function(json) {
				DI_Google_Maps.geocoder.latlng = json.Placemark.Point.coordinates;
			});
			callback(this.latlng);
		},
		
		

		// Performs a synchronous jquery getJSON to preserve the order of the
		// calls being made. This acts to replace the normal jquery $.getJSON() 
		// with one that is capable of being called synchronously
		syncGetJSON: function (url, params, callback) {
			$.ajax({	'url':		url,
						'type':		"get",
						'data':		params,
						'dataType':	"json",
						'success':	callback,
						'async':	false
			});
		}
	}

};