
	$(function() {
		var locations = $('#locations');
		if (locations.size()) {
			locations.before('<div id="googlemap"></div>');
			var mapDiv = $('#googlemap');
			mapDiv.css({marginBottom: '2em'});
			
			var markers = [];
			var zoomBounds = new GLatLngBounds();
			
			locations.find('li').each(function() {
				var latitude = parseFloat($(this).meta('latitude'));
				var longitude = parseFloat($(this).meta('longitude'));
				if (latitude && longitude) {
					$(this).find('.latitude, .longitude').remove();
					
					var l = new GLatLng(latitude, longitude);
				
					zoomBounds.extend(l);
					var marker = new GMarker(l);
					marker.html = $(this).html();
					
					marker.window = function() {
						this.openInfoWindowHtml(this.html, {maxWidth: 400});
					};
					
					
					$(this).find('h2').click(function() {
						marker.window();
					}).css({cursor: 'pointer'}).hover(function() {
						$(this).css({textDecoration: 'underline'});
					}, function() {
						$(this).css({textDecoration: 'none'});
					});
					
					GEvent.addListener(marker, 'click', function() {
						this.window();
					});
					
					markers.push(marker);
				}
			});
			
			var width = mapDiv.width();
			mapDiv.width(width + 'px');
			mapDiv.height(((width / 4) * 3) + 'px');
		
			var map = new GMap2(mapDiv.get(0));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.enableContinuousZoom();
			map.enableDoubleClickZoom();

			map.setCenter(zoomBounds.getCenter(), Math.min(12, map.getBoundsZoomLevel(zoomBounds)));
			
			for (var n = 0; n < markers.length; n++) {
				map.addOverlay(markers[n]);
			}
			
			var winOnUnload = window.onunload;
			window.onunload = function() {
				if (typeof winOnUnload == "function") winOnUnload();
				GUnload();
			}
		}
	
	});
	
