// A function to create the marker and set up the event window

function createMarker(latlng,html,mIcon) {
	var markerOptions = { icon:mIcon };
	var marker = new GMarker(latlng,markerOptions);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
  	});
  return marker;
}

function gup( num )
{
	var href = window.location.href;
	var segments = href.split('/');
	return (segments[num]);
}

$(function() { 
	if (GBrowserIsCompatible()) {

		// Read the data from xml
		var request = GXmlHttp.create();
		request.open("GET", "/maps/explore_location/"+gup(4), true);
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				var xmlDoc = request.responseXML;
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	            // Extract lat/longs
	            var lat = parseFloat(markers[0].getAttribute("lat"));
	            var lng = parseFloat(markers[0].getAttribute("lng"));
          		// Set up icon style
				var tinyIcon = new GIcon(G_DEFAULT_ICON);

				// create the map
				var map = new GMap2(document.getElementById("google_map"));
				var mapControl = new GMapTypeControl();
				map.addControl(mapControl);
				// Add controls and initialise the map
				map.addControl(new GSmallMapControl());
				map.setCenter(new GLatLng(lat, lng), 10);
				map.enableDoubleClickZoom();
				new GKeyboardHandler(map);
		            // Create the marker
		 			map.addOverlay(
						createMarker(
							new GLatLng(lat,lng),
							markers[0].getAttribute("html"),
							tinyIcon
						)
					);          
/* DEBUG
				GEvent.addListener(map, "click", function() {
					console.log(map.getCenter());
					console.log(map.getZoom());
				});
*/
	        }
      }
      if (window.recenter_map) recenter_map(map);
      request.send(null);
    }
});