// ***************** BEGIN DECLARE GLOBAL VARIABLES *************************************************************************************
var verbose = 0;									// COMMENT: Set debugging code output to false.
var dataSource = "../v03_includes/mapsquery.php";	// COMMENT: Specify the data source location. 

var map;											// COMMENT: Create map array.
var startPoint = new GLatLng(38.9111, -77.00622);	// COMMENT: This variable holds the default map starting point.
var startZoom = 10;									// COMMENT: This variable holds the default map zoom level.
var bounds;											// COMMENT: This variable holds the map boundaries for use in our auto map movemenent.

var icons = [];										// COMMENT: Create icons array.  Since we're only using one type of marker, we'll only be using one element in the array for now.

var markers = [];									// COMMENT: This variable holds the dealer markers and data pulled from the dealers variable.
var numMarkers = 0;

var amenities = [];

amenities["Bar-Club"] = new Object();
amenities["Bar-Club"].name = "Bars & Clubs";
amenities["Bar-Club"].iconColor = "purple";

amenities["Restaurant"] = new Object();
amenities["Restaurant"].name = "Restaurants";
amenities["Restaurant"].iconColor = "green";

amenities["Movie Theater"] = new Object();
amenities["Movie Theater"].name = "Movie Theaters";
amenities["Movie Theater"].iconColor = "gray";

amenities["Museum and Visual Arts"] = new Object();
amenities["Museum and Visual Arts"].name = "Museums";
amenities["Museum and Visual Arts"].iconColor = "neon";

amenities["Performing Arts Location"] = new Object();
amenities["Performing Arts Location"].name = "Theaters";
amenities["Performing Arts Location"].iconColor = "yellow";

amenities["Stores"] = new Object();
amenities["Stores"].name = "Stores";
amenities["Stores"].iconColor = "orange";

amenities["Sports and Recreation"] = new Object();
amenities["Sports and Recreation"].name = "Sports & Recreation";
amenities["Sports and Recreation"].iconColor = "aqua";

amenities["Hotel"] = new Object();
amenities["Hotel"].name = "Hotels";
amenities["Hotel"].iconColor = "red";

amenities["metrostops"] = new Object();
amenities["metrostops"].name = "Metro Stops";

var numLoads = 0;

var geocoder = new GClientGeocoder();

var initialID;

// ********** BEGIN get and split URL parameters **********
var args = new Array();

allargs = document.URL.split("?");
if(allargs[1])
{
	arg_pairs = allargs[1].split("&");

	for(var lupe=0; lupe<arg_pairs.length; lupe++)
	{
		tmp=arg_pairs[lupe].split("=");
	eval("args[\""+tmp[0]+"\"]=\""+tmp[1]+"\";");
	}
}
// ********** END get and split URL parameters **********

// ********** BEGIN modify initial variables based on URL parameters **********

if (args["verbose"])
{
	verbose = args["verbose"];
}

// ***************** END DECLARE GLOBAL VARIABLES *************************************************************************************

function showAddress(whichAddress, whichCity, whichState, whichZipcode)
{
	var tempAddress = "";
	if (whichAddress)
	{
		tempAddress += whichAddress;
	}
	if (whichCity)
	{
		tempAddress += ", " + whichCity;
	}
	if (whichState)
	{
		tempAddress += ", " + whichState;
	}
	if (whichZipcode)
	{
		tempAddress += " " + whichZipcode;
	}
	return tempAddress;
}	// end function showAddress(whichAddress, whichCity, whichState, whichZipcode)

// ***************** BEGIN FUNCTION TO START THE APPLICATION *************************************************************************************
function startApp(whichType, whichID)
{

	if (GBrowserIsCompatible())
	{

		loadMap();	// COMMENT: Load the map and settings.
		resetOptions();	// COMMENT: Make sure all options are set to off.


		if (whichID)	// COMMENT: Check to see if there is an initial venue/metro stop to load.
		{
			if ((whichType == "event") || (whichType == "venue") || (whichType == "restaurant"))
			{
				addVenue(whichType, whichID);	// COMMENT: Load the initial venue and open its infowindow.
			}
			else if ((whichType == "events") || (whichType == "venues") || (whichType == "restaurants"))
			{
				switch (whichType)
				{
					case "events" :
						whichType = "event";
						break;
					case "venues" :
						whichType = "venue";
						break;
					case "restaurants" :
						whichType = "restaurant";
						break;
				}
				addVenue(whichType, whichID);	// COMMENT: Load the initial venue and open its infowindow.
			}
			else if (whichType == "metrostop")
			{
				addMetroStop("metrostops", whichID);	// COMMENT: Load the initial metro stop and open its infowindow.
			}
		} // end if (whichID)

	} // end if (GBrowserIsCompatible())
	else
	{
		alert("Your browser is incompatible with this application.");
	}
} // end function startApp()
// ***************** END FUNCTION TO START THE APPLICATION *************************************************************************************

// ***************** BEGIN FUNCTION TO LOAD MAP AND DATA *************************************************************************************
function loadMap()
{

	map = new GMap2(document.getElementById("metrolinks_map"));			// COMMENT: Create a new map instance.
	map.enableContinuousZoom();											// COMMENT: Allow continous zooming in the map.
	map.enableDoubleClickZoom();										// COMMENT: Allow double click zooming.
	map.addControl(new GLargeMapControl());							// COMMENT: Add the map controls.
	map.setCenter(startPoint, startZoom);								// COMMENT: Initialize the map and set the center point to Washington, D.C.

	bounds = new GLatLngBounds();	// COMMENT: Reset the bounds.

	GEvent.addListener(map, "moveend", function()
	{

		if (numLoads)
		{
			for (key in amenities)
			{
				if(document.getElementById(key).checked)
				{
					getData(dataSource + "?searchType=" + key);
				}
			}
		}
	});

} // end function loadMap()
// ***************** END FUNCTION TO LOAD MAP AND DATA *************************************************************************************

function addVenue(whichType, whichID)
{

	if (markers[whichID])
	{
		alert("this marker is already displayed so go ahead and open the infowindow for it.");
	}
	else
	{
		getData(dataSource + "?searchType=" + whichType + "&searchID=" + whichID);
	}

} // end function addVenue(whichType, whichID)

function addMetroStop(whichType, whichID)
{
	if (markers[whichID])
	{
		alert("this marker is already displayed so go ahead and open the infowindow for it.");
	}
	else
	{
		getData(dataSource + "?searchType=" + whichType + "&searchID=" + whichID);
		getData(dataSource + "?searchType=metrostops");
	}
} // end function addMetroStop(whichID)

function getData(whichURL)
{

	document.getElementById("metrolinks_loading").style.display = "block";

	if (whichURL.indexOf("searchID") == -1)
	{
		var mapBounds = map.getBounds();
		southWest = mapBounds.getSouthWest();
		northEast = mapBounds.getNorthEast();
		whichURL = whichURL + "&latitude1=" + southWest.lat() + "&longitude1=" + southWest.lng() + "&latitude2=" + northEast.lat() + "&longitude2=" + northEast.lng();
	}

	if (verbose)
	{
		GLog.write(whichURL);
	}

	GDownloadUrl(whichURL, function(data, responseCode)
	{

		var xml = GXml.parse(data);	// COMMENT: Parse the XML data.
		var venues = xml.documentElement.getElementsByTagName("venue");	// COMMENT: Get the data source.

		for (var i = 0; i < venues.length; i++)	// COMMENT: Loop through the dealers from the XML file.
		{

			// ***************** BEGIN CREATE A NEW MARKER WITH THE KEY BEING THE VENUE'S ID *****************
			if ((whichURL.indexOf("event") != -1) || (whichURL.indexOf("venue") != -1))
			{
				var tempID = GXml.value(venues[i].getElementsByTagName("VenueID")[0]);
				if (!markers[tempID])
				{
					markers[tempID] =  new Object();
					markers[tempID].id = GXml.value(venues[i].getElementsByTagName("VenueID")[0]);
					markers[tempID].type = GXml.value(venues[i].getElementsByTagName("type")[0]);
					markers[tempID].venuetype = GXml.value(venues[i].getElementsByTagName("VenueTypeName")[0]);
					markers[tempID].name = GXml.value(venues[i].getElementsByTagName("VenueName")[0]);
					markers[tempID].address = showAddress(GXml.value(venues[i].getElementsByTagName("VenueAddress")[0]), GXml.value(venues[i].getElementsByTagName("VenueCity")[0]), GXml.value(venues[i].getElementsByTagName("VenueState")[0]), GXml.value(venues[i].getElementsByTagName("VenueZip")[0]));
					markers[tempID].phone = GXml.value(venues[i].getElementsByTagName("VenuePhoneNumber")[0]);
					markers[tempID].latitude = GXml.value(venues[i].getElementsByTagName("GeocodeLatitude")[0]);
					markers[tempID].longitude = GXml.value(venues[i].getElementsByTagName("GeocodeLongitude")[0]);
					markers[tempID].point = new GLatLng(parseFloat(markers[tempID].latitude), parseFloat(markers[tempID].longitude));
				}
			}
			else if (whichURL.indexOf("metrostops") != -1)
			{
				var tempID = "metrostop_" + GXml.value(venues[i].getElementsByTagName("id")[0]);
				if (!markers[tempID])
				{
					markers[tempID] =  new Object();
					markers[tempID].id = GXml.value(venues[i].getElementsByTagName("id")[0]);
					markers[tempID].type = "metrostops";
					markers[tempID].name = GXml.value(venues[i].getElementsByTagName("name_full")[0]);
					markers[tempID].address = GXml.value(venues[i].getElementsByTagName("address")[0]);
					markers[tempID].address2 = GXml.value(venues[i].getElementsByTagName("address2")[0]);
					markers[tempID].address3 = GXml.value(venues[i].getElementsByTagName("address3")[0]);
					markers[tempID].city = GXml.value(venues[i].getElementsByTagName("city")[0]);
					markers[tempID].state = GXml.value(venues[i].getElementsByTagName("state")[0]);
					markers[tempID].zipcode = GXml.value(venues[i].getElementsByTagName("zipcode")[0]);
					markers[tempID].red = GXml.value(venues[i].getElementsByTagName("red")[0]);
					markers[tempID].orange = GXml.value(venues[i].getElementsByTagName("orange")[0]);
					markers[tempID].blue = GXml.value(venues[i].getElementsByTagName("blue")[0]);
					markers[tempID].green = GXml.value(venues[i].getElementsByTagName("green")[0]);
					markers[tempID].yellow = GXml.value(venues[i].getElementsByTagName("yellow")[0]);
					markers[tempID].latitude = GXml.value(venues[i].getElementsByTagName("latitude")[0]);
					markers[tempID].longitude = GXml.value(venues[i].getElementsByTagName("longitude")[0]);
					markers[tempID].zoomlevel = GXml.value(venues[i].getElementsByTagName("zoomlevel")[0]);
					markers[tempID].point = new GLatLng(parseFloat(markers[tempID].latitude), parseFloat(markers[tempID].longitude));
				}
			}
			else if (whichURL.indexOf("restaurant") != -1)
			{

				var tempID = GXml.value(venues[i].getElementsByTagName("VenueID")[0]);
				if (!markers[tempID])
				{
					markers[tempID] =  new Object();
					markers[tempID].id = GXml.value(venues[i].getElementsByTagName("venueID")[0]);
					markers[tempID].type = GXml.value(venues[i].getElementsByTagName("type")[0]);
					markers[tempID].venuetype = GXml.value(venues[i].getElementsByTagName("category")[0]);
					markers[tempID].name = GXml.value(venues[i].getElementsByTagName("name")[0]);
					markers[tempID].address = showAddress(GXml.value(venues[i].getElementsByTagName("address")[0]), GXml.value(venues[i].getElementsByTagName("city")[0]), GXml.value(venues[i].getElementsByTagName("state")[0]), GXml.value(venues[i].getElementsByTagName("zip")[0]));
					markers[tempID].phone = GXml.value(venues[i].getElementsByTagName("phone")[0]);
					markers[tempID].latitude = GXml.value(venues[i].getElementsByTagName("geocodeLatitude")[0]);
					markers[tempID].longitude = GXml.value(venues[i].getElementsByTagName("geocodeLongitude")[0]);
					markers[tempID].point = new GLatLng(parseFloat(markers[tempID].latitude), parseFloat(markers[tempID].longitude));

if (verbose)
{
	alert(markers[tempID].id);
}
				}
			}
			else
			{
				var tempID = GXml.value(venues[i].getElementsByTagName("VenueID")[0]);
				if (!markers[tempID])
				{
					markers[tempID] =  new Object();
					markers[tempID].id = GXml.value(venues[i].getElementsByTagName("VenueID")[0]);
					markers[tempID].type = GXml.value(venues[i].getElementsByTagName("type")[0]);
					markers[tempID].venuetype = GXml.value(venues[i].getElementsByTagName("VenueTypeName")[0]);
					markers[tempID].name = GXml.value(venues[i].getElementsByTagName("VenueName")[0]);
					markers[tempID].address = showAddress(GXml.value(venues[i].getElementsByTagName("VenueAddress")[0]), GXml.value(venues[i].getElementsByTagName("VenueCity")[0]), GXml.value(venues[i].getElementsByTagName("VenueState")[0]), GXml.value(venues[i].getElementsByTagName("VenueZip")[0]));
					markers[tempID].phone = GXml.value(venues[i].getElementsByTagName("VenuePhoneNumber")[0]);
					markers[tempID].latitude = GXml.value(venues[i].getElementsByTagName("GeocodeLatitude")[0]);
					markers[tempID].longitude = GXml.value(venues[i].getElementsByTagName("GeocodeLongitude")[0]);
					markers[tempID].point = new GLatLng(parseFloat(markers[tempID].latitude), parseFloat(markers[tempID].longitude));
				}
			}

			// ***************** BEGIN CREATE THE MARKER'S DEFAULT INFOWINDOW *****************
			var tempHTML = "";
			tempHTML += "<div id=\"metrolinks_infowindow\">\n";
			if (markers[tempID].type == "metrostops")
			{
				tempHTML += "	<span class=\"name\">" + markers[tempID].name + "</span>\n";
			}
			else
			{
				tempHTML += "	<a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" class=\"name\">" + markers[tempID].name + "</a>\n";
			}
			tempHTML += "	<span class=\"description\">" + markers[tempID].address + "<br>";
			if (markers[tempID].phone)
			{
				tempHTML += "Tel: " + markers[tempID].phone + "</span>\n";
			}
		//	if (markers[whichID].url)
		//	{
				if (markers[tempID].type.indexOf("metrostops") == -1)
				{
//					tempHTML += "<div id=\"metrolinks_infowindow_additionallinks\"><a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" target=\"_blank\" class=\"link\">Events</a> | <a href=\"" + markers[tempID].url + "\" target=\"_blank\" class=\"link\">Reviews from The Washington Post</a></div>\n";
				}
		//	}
			tempHTML += "<span class=\"directions\">Directions: <a href=\"javascript:void(0);\" id=\"metrolinks_directions_tohere\" class=\"unselected\" onclick=\"showDirections('metrolinks_directions_tohere','to', '" + tempID + "');\">To Here</a> | <a href=\"javascript:void(0);\" id=\"metrolinks_directions_fromhere\" class=\"unselected\" onclick=\"showDirections('metrolinks_directions_fromhere','from', '" + tempID + "');\">From Here</a></span><br>\n";
			tempHTML += "<div id=\"metrolinks_directions\">\n";
			tempHTML += "	Address: <input type=\"text\" class=\"input\" id=\"metrolinks_address\"><input valign=middle type=\"image\" src=\"/v03_images/metrolinks_go.jpg\" value=\"submit\" class=\"go\"><input type=\"hidden\" value=\"\" id=\"metrolinks_toorfrom\" name=\"toorfrom\">\n";
			tempHTML += "</div>\n";
			markers[tempID].infowindowDefault = tempHTML;
			// ***************** END CREATE THE MARKER'S DEFAULT INFOWINDOW *****************

			// ***************** BEGIN CREATE THE MARKER'S DRIVE TO INFOWINDOW *****************
			var tempHTML = "";
			tempHTML += "<div id=\"metrolinks_infowindow\">\n";
			if (markers[tempID].type == "metrostops")
			{
				tempHTML += "	<span class=\"name\">" + markers[tempID].name + "</span>\n";
			}
			else
			{
				tempHTML += "	<a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" class=\"name\">" + markers[tempID].name + "</a>\n";
			}
			tempHTML += "	<span class=\"description\">" + markers[tempID].address + "<br>";
			if (markers[tempID].phone)
			{
				tempHTML += "Tel: " + markers[tempID].phone + "</span>\n";
			}
		//	if (markers[whichID].url)
		//	{
				if (markers[tempID].type.indexOf("metrostops") == -1)
				{
//					tempHTML += "<div id=\"metrolinks_infowindow_additionallinks\"><a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" target=\"_blank\" class=\"link\">Events</a> | <a href=\"" + markers[tempID].url + "\" target=\"_blank\" class=\"link\">Reviews from The Washington Post</a></div>\n";
				}
		//	}
			tempHTML += "<span class=\"directions\">Directions: <a href=\"javascript:void(0);\" id=\"metrolinks_directions_tohere\" class=\"selected\" onclick=\"showDirections('metrolinks_directions_tohere','to', '" + tempID + "');\">To Here</a> | <a href=\"javascript:void(0);\" id=\"metrolinks_directions_fromhere\" class=\"unselected\" onclick=\"showDirections('metrolinks_directions_fromhere','from', '" + tempID + "');\">From Here</a></span><br>\n";
			tempHTML += "<div id=\"metrolinks_directions\" style=\"display: block\">\n";
			tempHTML += "<form method=\"post\" action=\"http://www.expressnightout.com/directions.php\"><input type=\"hidden\" value=\"to\" id=\"metrolinks_toorfrom\" name=\"toorfrom\"><input type=\"hidden\" id=\"metrolinks_stopAddress\" name=\"stopAddress\" value=\"" + showAddress(markers[tempID].address, markers[tempID].city, markers[tempID].state, markers[tempID].zipcode) + "\">Address: <input type=\"text\" class=\"input\" id=\"metrolinks_address\" name=\"startAddress\"><input valign=middle type=\"image\" src=\"/v03_images/metrolinks_go.jpg\" name=\"submit\" value=\"submit\" class=\"go\"></form>\n";
			tempHTML += "</div>\n";
			markers[tempID].infowindowDriveTo = tempHTML;
			// ***************** END CREATE THE MARKER'S DRIVE TO INFOWINDOW *****************

			// ***************** BEGIN CREATE THE MARKER'S DRIVE FROM INFOWINDOW *****************
			var tempHTML = "";
			tempHTML += "<div id=\"metrolinks_infowindow\">\n";
			if (markers[tempID].type == "metrostops")
			{
				tempHTML += "	<span class=\"name\">" + markers[tempID].name + "</span>\n";
			}
			else
			{
				tempHTML += "	<a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" class=\"name\">" + markers[tempID].name + "</a>\n";
			}
			tempHTML += "	<span class=\"description\">" + markers[tempID].address + "<br>";
			if (markers[tempID].phone)
			{
				tempHTML += "Tel: " + markers[tempID].phone + "</span>\n";
			}
		//	if (markers[whichID].url)
		//	{
				if (markers[tempID].type.indexOf("metrostops") == -1)
				{
//					tempHTML += "<div id=\"metrolinks_infowindow_additionallinks\"><a href=\"/thescene/details.php?venueid=" + markers[tempID].id + "\" target=\"_blank\" class=\"link\">Events</a> | <a href=\"" + markers[tempID].url + "\" target=\"_blank\" class=\"link\">Reviews from The Washington Post</a></div>\n";
				}
		//	}
			tempHTML += "<span class=\"directions\">Directions: <a href=\"javascript:void(0);\" id=\"metrolinks_directions_tohere\" class=\"unselected\" onclick=\"showDirections('metrolinks_directions_tohere','to', '" + tempID + "');\">To Here</a> | <a href=\"javascript:void(0);\" id=\"metrolinks_directions_fromhere\" class=\"selected\" onclick=\"showDirections('metrolinks_directions_fromhere','from', '" + tempID + "');\">From Here</a></span><br>\n";
			tempHTML += "<div id=\"metrolinks_directions\" style=\"display: block\">\n";
			tempHTML += "<form method=\"post\" action=\"http://www.expressnightout.com/directions.php\"><input type=\"hidden\" value=\"to\" id=\"metrolinks_toorfrom\" name=\"toorfrom\"><input type=\"hidden\" id=\"metrolinks_startAddress\" name=\"startAddress\" value=\"" + showAddress(markers[tempID].address, markers[tempID].city, markers[tempID].state, markers[tempID].zipcode) + "\">Address: <input type=\"text\" class=\"input\" id=\"metrolinks_address\" name=\"stopAddress\"><input valign=middle type=\"image\" src=\"/v03_images/metrolinks_go.jpg\" name=\"submit\" value=\"submit\" class=\"go\"></form>\n";
			tempHTML += "</div>\n";
			markers[tempID].infowindowDriveFrom = tempHTML;
			// ***************** END CREATE THE MARKER'S DRIVE FROM INFOWINDOW *****************

			// ***************** END CREATE A NEW MARKER WITH THE KEY BEING THE VENUE'S ID *****************
		} // end for (var i = 0; i < venues.length; i++)

		addMarkers(whichURL);

		document.getElementById("metrolinks_loading").style.display = "none";

	});	// end GDownloadUrl(dataFileName, function(data, responseCode)

} // end function getData()

function addMarkers(whichURL)
{

	for (key in markers)
	{
		if (whichURL.indexOf(markers[key].type) != -1)
		{
			if (!markers[key].marker)	// COMMENT: If the marker hasn't yet been added to the map, go ahead and add it now.
			{
				markers[key].marker = createMarker(key);
				map.addOverlay(markers[key].marker);
				if (verbose)
				{
					GLog.write("ADD markers[\"" + key + "\"]. Name = \"" + markers[key].name + "\". Type = \"" + markers[key].type + "\"");
				}
			}
			else // from if (!markers[key].marker)	// COMMENT: If the marker is already mapped but hidden, go ahead and show it now.
			{
				markers[key].marker.show();
				if (verbose)
				{
					GLog.write("SHOW markers[\"" + key + "\"]. Name = \"" + markers[key].name + "\". Type = \"" + markers[key].type + "\"");
				}
			} // end else from if (!markers[key].marker)

			if (whichURL.indexOf("searchID") != -1)	// COMMENT: Only extend the bounds if this is an individual point.
			{
				bounds.extend(markers[key].point);	// COMMENT: Extend the current bounds to enclose this point.
			}

			numMarkers++;
		}

	}

	if (whichURL.indexOf("searchID") != -1)
	{
		if (map.getBoundsZoomLevel(bounds) > 14)
		{
			map.setZoom(14);	// COMMENT: A BoundsZoomLevel is way too close, so back it out to 12.
		}
		else if (map.getBoundsZoomLevel(bounds) > 10)
		{
			map.setZoom(10);	// COMMENT: A BoundsZoomLevel is slightly too close, so back it out to 10.
		}
		else
		{
			map.setZoom(map.getBoundsZoomLevel(bounds));	// COMMENT: Zoom to the appropriate level encompassing all of the bounded points.
		}
		map.panTo(bounds.getCenter());	// COMMENT: Center on all of the bounded points.
	}

	numLoads++;

	
	if ((numMarkers == 1) && (whichURL.indexOf("searchID") != -1))	// COMMENT: Only automatically open an infowindow if there is only one marker and the searchID was provided.
	{
		for (key in markers)	// COMMENT: Get the initial ID (can't use searchID when it's an event)
		{
			autoOpenInfoWindow(key);	// COMMENT: Automatically open this infowindow.
		} // end for (key in markers)
	} // end if ((numMarkers == 1) && (whichURL.indexOf("searchID") != -1))

	displayResultsList();

} // end function addMarkers(whichURL)

// ***************** BEGIN FUNCTION TO CREATE AND RETURN A MARKER *************************************************************************************
function createMarker(whichID)
{

/*
	if (!icons["allemenities"])	// COMMENT: Temporarily hardcoded since we're only using one type of marker.  Eventually change this to accept the 'makes' variable (or whatever).
	{
		icons["allemenities"] = new GIcon();
		icons["allemenities"].image = "/v03_images/maps/map-marker-red-single.png";
		icons["allemenities"].shadow = "/v03_images/maps/map-marker-shadow.png";
		icons["allemenities"].iconSize = new GSize(19, 33);
		icons["allemenities"].shadowSize = new GSize(36, 33);
		icons["allemenities"].iconAnchor = new GPoint(9, 33);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which this icon is anchored to the map.
		icons["allemenities"].infoWindowAnchor = new GPoint(9, 15);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which the info window is anchored to this icon.
	} // end if (!icons["allemenities"])
*/

	if (!icons[markers[whichID].type])
	{

		if (verbose)
		{
			GLog.write("need new icon: " + markers[whichID].type);
		}

		if (markers[whichID].type == "metrostops")
		{
			icons["metrostops" + whichID] = new GIcon();
			icons["metrostops" + whichID].image = "/v03_images/maps/icon_metro_metro_" + markers[whichID].red + markers[whichID].orange + markers[whichID].blue + markers[whichID].green + markers[whichID].yellow + ".png";
			icons["metrostops" + whichID].iconSize = new GSize(17, 30);
			icons["metrostops" + whichID].shadowSize = new GSize(23, 14);
			icons["metrostops" + whichID].iconAnchor = new GPoint(8, 15);
			icons["metrostops" + whichID].infoWindowAnchor = new GPoint(1, 1);
		}
		else if (amenities[markers[whichID].type])
		{
			icons[markers[whichID].type] = new GIcon();
			icons[markers[whichID].type].image = "/v03_images/maps/icon_" + amenities[markers[whichID].type].iconColor + ".png";
			icons[markers[whichID].type].shadow = "/v03_images/maps/icon_shadow.png";
			icons[markers[whichID].type].iconSize = new GSize(14, 22);
			icons[markers[whichID].type].shadowSize = new GSize(24, 22);
			icons[markers[whichID].type].iconAnchor = new GPoint(7, 22);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which this icon is anchored to the map.
			icons[markers[whichID].type].infoWindowAnchor = new GPoint(6, 11);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which the info window is anchored to this icon.
		}
		else
		{
			icons[markers[whichID].type] = new GIcon();
			icons[markers[whichID].type].image = "/v03_images/maps/icon_hover.png";
			icons[markers[whichID].type].shadow = "/v03_images/maps/icon_shadow.png";
			icons[markers[whichID].type].iconSize = new GSize(14, 22);
			icons[markers[whichID].type].shadowSize = new GSize(24, 22);
			icons[markers[whichID].type].iconAnchor = new GPoint(7, 22);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which this icon is anchored to the map.
			icons[markers[whichID].type].infoWindowAnchor = new GPoint(6, 11);	// COMMENT: The pixel coordinate relative to the top left corner of the icon image at which the info window is anchored to this icon.
		}
	}
	else
	{
		if (verbose)
		{
			GLog.write("already have icon: " + markers[whichID].type);
		}
	}

	var point = new GLatLng(parseFloat(markers[whichID].latitude), parseFloat(markers[whichID].longitude));
	if (markers[whichID].type != "metrostops")
	{
		var tempMarker = new GMarker(point, icons[markers[whichID].type]);	// COMMENT: Create a new marker object.
	}
	else
	{
		var tempMarker = new GMarker(point, icons[markers[whichID].type + whichID]);	// COMMENT: Create a new marker object.
	}

	// ***************** BEGIN ADD MARKER CLICK EVENT LISTENER TO OPEN INFOWINDOW *****************
	GEvent.addListener(tempMarker, "click", function()
	{
		tempMarker.openInfoWindowHtml(markers[whichID].infowindowDefault);	// COMMENT: Add the infowindow to the marker on the "click" event.
	});	// end GEvent.addListener(marker, "click", function()
	// ***************** END ADD MARKER CLICK EVENT LISTENER TO OPEN INFOWINDOW *****************

	GEvent.addListener(tempMarker, "mouseover", function()
	{

		var text = "<div id=\"metrolinks_tooltip\"><nobr>" + markers[whichID].name + "</nobr></div>";
		label = new ELabel(point, text);

		switch (markers[whichID].type)
		{
			case "metrostops" :
				label.pixelOffset=new GSize(14,-7);
				break;
			default:
				label.pixelOffset=new GSize(14,-28);
				tempMarker.setImage("/v03_images/maps/icon_hover.png");	// COMMENT: Set the marker icon to its mouseover state.
				break;
		}

		map.addOverlay(label);	// add the label to the map on the "mouseover" event.

	});	// end GEvent.addListener(marker, "mouseover", function()

	GEvent.addListener(tempMarker, "mouseout", function()
	{
		label.hide();	// COMMENT: First hide the label since this is faster, then remove it in the background while the label is hidden.
		map.removeOverlay(label);	// COMMENT: Now remove the label from the map on the "mouseout" event.

		if (markers[whichID].type != "metrostops")
		{
			tempMarker.setImage(icons[markers[whichID].type].image);	// COMMENT: Reset the marker icon back to its default
		}

	});	// end GEvent.addListener(marker, "mouseout", function()

	return tempMarker;	// COMMENT: Return the marker object.

} // end function createMarker(point)
// ***************** END FUNCTION TO CREATE AND RETURN A MARKER *************************************************************************************

function removeMarkers(whichType)
{
	for (key in markers)
	{
		if (markers[key].type != "metrostops")
		{
			if (verbose)
			{
				GLog.write("HIDE markers[key].type: '" + markers[key].type + "', whichType: '" + whichType + "'. markers[\"" + key + "\"]. Name = \"" + markers[key].name + "\". Type = \"" + markers[key].type + "\"");
			}
		}
		if (markers[key].type == whichType)
		{
			markers[key].marker.hide();
			numMarkers--;
		}
	}

	displayResultsList();

} // end function removeMarkers(whichType)

function toggleMapOptions(whichMapOption)
{
	if(document.getElementById(whichMapOption).checked)
	{
		getData(dataSource + "?searchType=" + whichMapOption);
	}
	else
	{
		removeMarkers(whichMapOption);
	}

} // end function toggleMapOptions(whichMapOption)

function displayResultsList()
{
	var tempHTML = "";

/*
	var newAmenities = [];
	newAmenities = ConvertAssocToIndexedArray(newAmenities, amenities);
	newAmenities.sort();
*/

	if (location.href.indexOf("/metrolinks") != -1)
	{

		if (numMarkers)
		{

			for (key in amenities)
			{
				if(document.getElementById(key).checked)
				{
					tempHTML += "				<span class=\"header\"><span class=\"header_text\">" + amenities[key].name.toUpperCase() + "</span></span>\n";
					tempHTML += "				<ul>\n";
					for (key2 in markers)
					{
						if (key == markers[key2].type)
						{
							tempHTML += "					<li><a href=\"javascript:void(0);\" onclick=\"autoOpenInfoWindow('" + markers[key2].id + "');\" class=\"name\">" + markers[key2].name + "</a><span class=\"info\">" + markers[key2].address + "</span></li>\n";
						}
					}
					tempHTML += "				</ul>\n";
				}
			} // end for (key in amenities)

		} // end if (numMarkers)

		document.getElementById("metrolinks_results").innerHTML = tempHTML;

	}

}

function autoOpenInfoWindow(whichID)
{
	if (verbose)
	{
		GLog.write("auto open infowindow: '" + whichID);
	}
	GEvent.trigger(markers[whichID].marker,'click');
}

function getAddress() {

	address = document.getElementById("metrolinks_address").value;
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " cannot be resolved at this time.  Please try again.");
      } else {
        map.setCenter(point, 13);
   //     var marker = new GMarker(point);
     //   map.addOverlay(marker);
       // marker.openInfoWindowHtml(address);
      }
    }
  );

  return false;
}

function resetOptions()
{
	for (key in amenities)
	{
		if(document.getElementById(key).checked)
		{
			document.getElementById(key).checked = false;
		}
	}
}