//<script>
// daniel@klixo.net.nz / dan@klixo.net.nz
function kxGoogleMapDrawPoints(iZoomLevel, oIcon)
{
	try 
	{
		// http://www.google.com/apis/maps/documentation/reference.html
		if (GBrowserIsCompatible()) 
		{	    
			// Initialise the map object
			var ele = document.getElementById("kxMap");
			if (ele == null) return;
			var map = new GMap2(ele);
			map.addControl(new GMapTypeControl());
			map.addControl(new GLargeMapControl());
			map.addControl(new GOverviewMapControl());

			var markerIcon;
			// What icon do we want to use?
			if (typeof oIcon == 'undefined' )
			{
				// Use Google Icon
				markerIcon = new GIcon(G_DEFAULT_ICON);
			}
			else icon = oIcon;
			    
			var minx = 0, maxx, miny, maxy;
			var pCentrePoint;
			    
			// Get an array of hidden inputs that contain the Points data    
			var oGLatLngs = document.getElementsByName("kxGLatLng");
			var sTitles = document.getElementsByName("kxMarkerTitle");
			// Loop through and create a marker for each point
			for (var i = 0; i < oGLatLngs.length; i++)
			{
				var sGLatLng = oGLatLngs[i].value;
				// If a GLatLng value exists for this story, use it to create a point
				if (sGLatLng.length > 0)
				{
					eval("var point = new GLatLng(" + sGLatLng + ");");
					if (point)
					{
						// Get the min and max x/y coordinates
						if (minx == 0)
						{
							pCentrePoint = new GLatLng(point.lat(), point.lng());
							minx = point.x;
							maxx = point.x;
							miny = point.y;
							maxy = point.y;
						}
						else
						{
							if (point.x < minx) minx = point.x;
							if (point.x > maxx) maxx = point.x;
							if (point.y < miny) miny = point.y;
							if (point.y > maxy) maxy = point.y;
							delete pCentrePoint;
							pCentrePoint = new GLatLng((miny + maxy) / 2, (minx + maxx) / 2);
						}
						// Centre the map
						map.setCenter(pCentrePoint, iZoomLevel);
						// Set the map type to Normal (can't do until Zoomlevel set)
						map.setMapType(G_NORMAL_MAP);
						// See if there is a title
						var sTitle;
						if (sTitles != null) {
							if (sTitles.length > i) {
								var sTitle = sTitles[i].value;
							}
						}
						// Create the options for the marker
						var markerOpts = {icon:markerIcon, title:sTitle};
						var marker = createMarker(point, markerOpts, document.getElementById("kxMapInfo" + (i + 1)));
						// Add the marker to the map
						map.addOverlay(marker); 
					}
				}
			}
		}
	}
	// Don't throw exceptions if we encounter any here
	catch(err) {} 
}

function createMarker(point, markerOpts, info) {
	var marker = new GMarker(point, markerOpts);
	if (info) {
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindow(info);
		});
	}
	return marker;
}
