(function () {
	var Locations = window.Locations = new Object();
	
	// Global variables
		Locations.$map = null;
		Locations.data = new Array();

	// Private variables
		var Google = new Object();
		Google.geocoder = null;
		Google.map = null;
		
	// Functions
		function newGIcon () {
			var icon = new GIcon();
			icon.image = 'http://nxdev01.nauticom.net/tudi/images/mapIcon.png';
			//icon.shadow = '/tudi/images/mapIconShadow.png';
			icon.iconSize = new GSize(16.0, 30.0);
			icon.shadowSize = new GSize(30.0, 15.0);
			icon.iconAnchor = new GPoint(10.0, 15.0);
			icon.infoWindowAnchor = new GPoint(10.0, 15.0);
			return icon;
		};
		
		function addMarker (locationNumber, place) {
			var locationIndex = locationNumber-1;
			var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0])
			Locations.data[locationIndex].marker = new GMarker(point, newGIcon());
			delete point
			
			Locations.data[locationIndex].html = new Object();
			Locations.data[locationIndex].html.location = '<div class="googleMarker"><b>' + Locations.data[locationIndex].name + '</b><br /><span class="text_sm">' + Locations.data[locationIndex].content + '</span>';
			if (Locations.data[locationIndex].directions) {
				Locations.data[locationIndex].html.location += '<br /><br /><span class="text_xsm"><b>Get Directions:</b><br /><a href="#" style="color: #BF311A;" onClick="javascript:return Locations.directions.toHere(' + locationIndex + ');">To Here</a>, <a href="#" style="color: #BF311A;" onClick="javascript:return Locations.directions.fromHere(' + locationIndex + ');">From Here</a></span>';
				Locations.data[locationIndex].html.to = '<div class="googleMarker"><b>' + Locations.data[locationIndex].name + '</b><br /><div class="text_sm">' + Locations.data[locationIndex].content + '</div><span class="text_xsm"><br /><b>Get Directions:</b><br />To Here, <a href="#" style="color: #BF311A;" onClick="javascript:return Locations.directions.fromHere(' + locationIndex + ');">From Here</a><br /><form action="http://maps.google.com/maps" method="get" target="_blank"><input type="text" id="saddr" name="saddr" value="Starting Address" class="text_xsm" style="color: #666666; margin-top:4px;" onFocus="javascript:if(this.value==\'Starting Address\')this.value=\'\';" onBlur="javascript:if(this.value==\'\')this.value=\'Starting Address\';" size="40" maxlength="60" /><input type="submit" value="Go" style="margin-left:4px; margin-top:4px;" class="text_xsm"><input type="hidden" id="daddr" name="daddr" value="' + place.address + '","(Name)" /></form></span></div>';
				Locations.data[locationIndex].html.from = '<div class="googleMarker"><b>' + Locations.data[locationIndex].name + '</b><br /><div class="text_sm">' + Locations.data[locationIndex].content + '</div><span class="text_xsm"><br /><b>Get Directions:</b><br /><a href="#" style="color: #BF311A;" onClick="javascript:return Locations.directions.toHere(' + locationIndex + ');">To Here</a>, From Here<br /><form action="http://maps.google.com/maps" method="get" target="_blank"><input type="text" id="daddr" name="daddr" value="Ending Address" class="text_xsm" style="color: #666666; margin-top:4px;" onFocus="javascript:if(this.value==\'Ending Address\')this.value=\'\';" onBlur="javascript:if(this.value==\'\')this.value=\'Ending Address\';" size="40" maxlength="60" /><input type="submit" value="Go" style="margin-left:4px; margin-top:4px;" class="text_xsm"><input type="hidden" name="saddr" value="' + place.address + '","(Name)"/></form></span></div>';
			}
			Locations.data[locationIndex].html.location += '</div>';
			
			function markedClicked () {
				Locations.data[locationIndex].marker.openInfoWindowHtml(Locations.data[locationIndex].html.location);
				if (Locations.data[locationIndex].overlay) {
					for (var iLocations = 0; iLocations < Locations.data.length; ++iLocations)
						if (Locations.data[iLocations].overlay && !Locations.data[iLocations].overlay.isHidden())
							Locations.data[iLocations].overlay.hide();
					Locations.data[locationIndex].overlay.show();
				}
					
				return false;
			};
			
			Google.map.addOverlay(Locations.data[locationIndex].marker);
			GEvent.addListener(Locations.data[locationIndex].marker, 'click', markedClicked);
			
			if (Locations.data[locationIndex].boundary !== false) {
				var swLatLng = Locations.data[locationIndex].boundary.sw.split(',');
				var neLatLng = Locations.data[locationIndex].boundary.ne.split(',');
				var boundaries = new GLatLngBounds(new GLatLng(swLatLng[0], swLatLng[1]), new GLatLng(neLatLng[0], neLatLng[1]));
				
				Locations.data[locationIndex].overlay = new GGroundOverlay('http://nxdev01.nauticom.net/tudi/images/locations/' + Locations.data[locationIndex].boundary.image, boundaries);
				Locations.data[locationIndex].overlay.hide();
				Google.map.addOverlay(Locations.data[locationIndex].overlay);
				
				delete swLatLng, neLatLng, boundaries;
			} else
				Locations.data[locationIndex].overlay = false;
			
			jQuery('a[rel="'+Locations.data[locationIndex].name+'"]').bind('click', markedClicked);//.triggerHandler('click');
		};
		
		Locations.add = function (json) {
			var locationNumber = Locations.data.push(json);
			Google.geocoder.getLocations(json.address,
				function (response) {
					if (!response || response.Status.code != 200) {
						// hopefully don't land here...
					} else {
						var place = response.Placemark[0];
						addMarker(locationNumber, place);
						delete place;
					}
				}
			);
		};
		
		Locations.directions = new Object();
			Locations.directions.fromHere = function (locationIndex) {
				Locations.data[locationIndex].marker.openInfoWindowHtml(Locations.data[locationIndex].html.from);
				return false;
			};
		
			Locations.directions.toHere = function (locationIndex) {
				Locations.data[locationIndex].marker.openInfoWindowHtml(Locations.data[locationIndex].html.to);
				return false;
			};
			
	
	Locations.initialize = function () {
		if (!GBrowserIsCompatible())
			return;
		
		Locations.$map = jQuery('div#map').get(0);
		Google.geocoder = new GClientGeocoder();
		Google.map = new GMap2(Locations.$map);

		// Add controls to map
			 Google.map.setUIToDefault();

			
		// Set default location
			Google.map.setCenter(new GLatLng(40.438423, -80.001933), 8);
			
		window.Locations.initialize = function () {return true;};
		jQuery(document).bind('unload', GUnload);
	};
	
	jQuery(document).ready(window.Locations.initialize);
}());