function createVenueMarker (point, html, number) {
	var venueIcon = new GIcon();
		venueIcon.shadow = "/etc/images/venue_icon_shadow.png";
		venueIcon.iconSize = new GSize(12, 12);
		venueIcon.shadowSize = new GSize(20, 20);
		venueIcon.iconAnchor = new GPoint(0, 0);
		venueIcon.infoWindowAnchor = new GPoint(7, 7);
		venueIcon.image = "/etc/images/venue_icon.png";

	var venueMarker = new GMarker(point, venueIcon);

	GEvent.addListener(venueMarker, "mouseover", function() {
		venueMarker.openInfoWindowHtml(html);
	});

	return venueMarker;
}

function HtmlEscape(str)
{
	if (str == null) {
		return "NULL STRING in HtmlEscape";
	}
	str = str.replace(new RegExp('&','gm'), '&amp;');
	str = str.replace(new RegExp('<','gm'), '&lt;');
	str = str.replace(new RegExp('>','gm'), '&gt;');
	return str;
}

// Return some HTML with names and links for all the venues
// at this location.
function GetInfoWindowHtml(venues)
{
	var html = "<table class='gmap_popup'>";
	for (var i in venues) {
		var venue = venues[i];
		main_ahref = "<a href='" + venue.link_main + "'>";
		contact_ahref = "<a href='/contact_venue.php?to_contact[]="+venue.venue_id+"'>";
		html += "<tr>";
		if (venue.venue_img_id) {
			html += "<td class='img'><div>";
			html += main_ahref;
			html += "<img src='/show_venue_image.php?image_id="+venue.venue_img_id+"&size=resized' width='100' alt='"+venue.venue_name+"' />";
			html += "</a></div></td><td class='txt'>";
		} else {
			html += "<td colspan='2' class='txt'>";
		}
		html += "<strong>" + main_ahref;
		html += HtmlEscape(venue.venue_name);
		html += "</a></strong><br />";
		html += venue.venue_address + "<br />";
		html += venue.venue_postcode + "<br />";
		if (venue.venue_tel) {
			html += "Tel: " + HtmlEscape(venue.venue_tel) + "<br />";
		}
		if (venue.venue_contactable) {
			html += "<br />" + contact_ahref + "Contact venue</a><br />";
		}
		html += "</td></tr>";
	}
	html += "</table>";
	return html;
}

function SetMarkerTitle(marker, title)
{
	// TODO. Make this work with Google maps 2.0
}

// Return a GLatLngBounds object which encloses all our points.
function FindBounds(extend)
{
	if (! extend) {
		extend = 0;
	}
	var minx = 180;
	var maxx = -180;
	var miny = 90;
	var maxy = -90;
	for (var i in map_marker_info) {
		var marker_info = map_marker_info[i];
		minx = Math.min(minx, marker_info.longitude);
		maxx = Math.max(maxx, marker_info.longitude);
		miny = Math.min(miny, marker_info.latitude);
		maxy = Math.max(maxy, marker_info.latitude);
	}
	minx -= extend;
	miny -= extend;
	maxx += extend;
	maxy += extend;
	
	return new GLatLngBounds(
		new GLatLng(miny, minx),
		new GLatLng(maxy, maxx));
}

// This controls the zoom level of single marker maps
var SMALL_DISTANCE = (1.0 / 180); // A one hundred and eightieth of a degree, or a slightly more 
// than a mile (in the UK)

// return a GLatLngBounds object which encloses our points and is
//	centred about cx, cy
function FindBoundsCentred(cx, cy, extend)
{
	var bounds = FindBounds(extend);
	var xdist = Math.max(Math.abs(bounds.getSouthWest().lng() - cx), 
		Math.abs(bounds.getNorthEast().lng() - cx));
	var ydist = Math.max(Math.abs(bounds.getSouthWest().lat() - cy), 
		Math.abs(bounds.getNorthEast().lat() - cy));
	bounds.extend(new GLatLng(cy - ydist, cx - xdist));
	bounds.extend(new GLatLng(cy + ydist, cx + xdist));
	return bounds;
}

function GetDefaultZoomLevel(map)
{
	var here = new GLatLng(51 - SMALL_DISTANCE, -1 - SMALL_DISTANCE);
	var there = new GLatLng(51 + SMALL_DISTANCE, -1 + SMALL_DISTANCE);
	return map.getBoundsZoomLevel(new GLatLngBounds(here, there));
}

var google_map_obj;
function SetupGoogleMap() 
{
	var container = document.getElementById("map");
	if (! container) {
		return; // Nothing to put the map in.
	}

	var venues = false;
	if (map_marker_info.length > 0) {
		venues = true;
	}
	
	if ( !window.map_centre_longitude && ! venues) {
		// No centre, no venues. Give up.
		return;
	}

	google_map_obj = new GMap2(container);
	var map = google_map_obj;
	map.addControl(new GSmallMapControl());
	var centreMarker = false;
	if (window.map_centre_longitude) {
		var centrePoint = new GLatLng(window.map_centre_latitude, window.map_centre_longitude);
		var centreMarker = new GMarker(centrePoint);
		if (venues) var bounds = FindBoundsCentred(centrePoint.lng(), centrePoint.lat(),	
			SMALL_DISTANCE);
	} else if (venues) {
		// No centre determined.
		var bounds = FindBounds(SMALL_DISTANCE);
		// Invent a centre point in the middle of the bounds.
		var centrePoint = new GLatLng(
				(bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2,
				(bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2);
	}
	if (venues) {
		var zoomLevel = map.getBoundsZoomLevel(bounds);
		map.setCenter(centrePoint, zoomLevel);
	} else if (centrePoint) {
		map.setCenter(centrePoint, GetDefaultZoomLevel(map));
	}
	if (centreMarker) {
		map.addOverlay(centreMarker);
	}

	// Add markers...
	for (var i in map_marker_info) {
		var marker_info = map_marker_info[i];
		var index = marker_info.pin_number;
		var point = new GPoint (marker_info.longitude, marker_info.latitude);
		var title = marker_info.title + " - " +
			marker_info.venue_name;
		var html = GetInfoWindowHtml(marker_info.venues);
		var marker = createVenueMarker(point, html, index);
		map.addOverlay(marker);
		SetMarkerTitle(marker, title);
	}
	
}

function InitGoogleMap()
{
	SetupGoogleMap();
}

window.onload = InitGoogleMap;
