// TODO: Should I geocode the directory entries to save the visitor time?

// Load the version of the API we require - this doesn't like being in an onload event, it moaned.
var gLocalSearch;
var myMarkers = [];
google.load("maps", "2.x");
google.load("search", "1");
google.setOnLoadCallback(initializeGoogleMap);


// -----------------------------------------------------------------------------

function handleMarkerClick(marker) {
	// Reset all the images to be unselected
	for (var i = 0; i < myMarkers.length; i++ ) {
		//myMarkers[i].setImage("http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png");
		myMarkers[i].setImage("/images/presentation/map-marker-green.png");
	}
	
	// Change the icon of the given (selected) marker
	marker.setImage("/images/presentation/map-marker-red.png")

	// Now go and fetch the XML file containing the markup for this single event
	GDownloadUrl("/feeds/get_events.asp?id=" + marker.value, function(data) {
		document.getElementById("mapOneEventContent").innerHTML = data;
	});
}

// -----------------------------------------------------------------------------

function createMarker(APoint, AID) {
	// Create our custom icon (just use a Google one for now)
	var iconOurMarker = new GIcon(G_DEFAULT_ICON);
	iconOurMarker.image = "/images/presentation/map-marker-green.png";
	iconOurMarker.iconSize = new GSize(32, 32);	                

	// Set up our GMarkerOptions object
	markerOptions = {icon:iconOurMarker};

	var marker = new GMarker(APoint, markerOptions);
	marker.value = AID;

	// The ability to click on the marker and bring up the events details is dependent on
	//      there being a "mapOneEventContent" element on the page
	if (document.getElementById("mapOneEventContent")) {
		GEvent.addListener(marker, "click", function() {
			handleMarkerClick(marker);
		});
	}

	return marker;
}

// -----------------------------------------------------------------------------

	// Called when search results are returned
	function OnLocalSearch() {
		if (gLocalSearch.results[0]) {
			// For some reason, it doesn't like it when I set this up outside this function
			var map = new google.maps.Map2(document.getElementById("mapDirMember"));
			map.addControl(new GLargeMapControl());	// Add zoom and positioning buttons
			map.addControl(new GMapTypeControl());	// Add "Map/Satellite/Hybrid" buttons

			// Add a marker for the first result
			var point = new GLatLng(gLocalSearch.results[0].lat, gLocalSearch.results[0].lng);
			map.setCenter(point, 15, G_NORMAL_MAP);
			map.addOverlay(createMarker(point, "Map"));

			// Change the class on the map so that the styling will all work
			document.getElementById("mapDirMember").className = "mapYes";
		}
    }
	
// -----------------------------------------------------------------------------

	function initializeGoogleMap() {
	// TODO: One initialisation for both maps

	// The events map
	if (document.getElementById("mapEventsMap")) {
        // Setup the map
		var map = new google.maps.Map2(document.getElementById("mapEventsMap"));
		map.addControl(new GLargeMapControl());	// Add zoom and positioning buttons
		map.addControl(new GMapTypeControl());	// Add "Map/Satellite/Hybrid" buttons

		map.setCenter(new google.maps.LatLng(40, -100), 1);		// Just a test long and lat, can change if needed

		// Now go and fetch the XML file containing all the events
		// TODO: Change this to fetch the markers in order of date
		GDownloadUrl("/feeds/get_events.asp", function(data) {
			var xml = GXml.parse(data);
			var arEvents = xml.documentElement.getElementsByTagName("event");
			for (var i = 0; i < arEvents.length; i++) {
				var point = new GLatLng(arEvents[i].getElementsByTagName("event_latitude")[0].firstChild.nodeValue, arEvents[i].getElementsByTagName("event_longitude")[0].firstChild.nodeValue);
				var marker=createMarker(point, arEvents[i].getElementsByTagName("event_id")[0].firstChild.nodeValue)
				map.addOverlay(marker);
				myMarkers.push(marker);
			}

			// Select the first marker
			if (myMarkers.length > 0) {
				handleMarkerClick(myMarkers[0]);
			}

		});
	}
	
	// A directory members map, make sure we have both elements we need for this
	if (document.getElementById("mapDirMember")) {
		if ((document.getElementById("mapLongitude")) && (document.getElementById("mapLatitude"))) {
			// Longitude and latitude field found, so use that to display the map
	        // Setup the map
			var map = new google.maps.Map2(document.getElementById("mapDirMember"));
			map.addControl(new GLargeMapControl());	// Add zoom and positioning buttons
			map.addControl(new GMapTypeControl());	// Add "Map/Satellite/Hybrid" buttons
			var point = new GLatLng(document.getElementById("mapLatitude").value, document.getElementById("mapLongitude").value);
			map.setCenter(point, 15, G_NORMAL_MAP);
			map.addOverlay(createMarker(point, "Map"));

			// Change the class on the map so that the styling will all work
			document.getElementById("mapDirMember").className = "mapYes";
		} else {
			// Search for the mapAddress for the map
			if (document.getElementById("mapAddress")) {
		      	// Initialize the local search
		      	gLocalSearch = new GlocalSearch();
		      	gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
				gLocalSearch.execute(document.getElementById("mapAddress").value);
			}
		}
	}	

}

// -----------------------------------------------------------------------------

