google.load("maps","2");
var gMap;
var Locations = [];
var markers = [];
var nativeZoom;

$(document).ready(function(){

	initializeMap();
	placeLocationMarkers(drag);
	
	$("input.directions").click(
		function()
		{
			$("#directionsDisplay").html("");
			var locID = extractID($(this).attr("id"));
			var from = $("#directions_"+locID).attr("value");
			var to = Locations[locID]["Latitude"] + "," + Locations[locID]["Longitude"];
			var directions = new GDirections(gMap,document.getElementById("directionsDisplay"));
			GEvent.addListener(directions, "error", function() {
				if ($("#directionsDisplay").html() == "")
				{
					$("#directionsDisplay").html("<div class=\"noResults\">Sorry, your address was not found</div>");
					show($("#directionsContainer"));
				}
			});
			GEvent.addListener(directions, "addoverlay", function() {
				show($("#directionsContainer"));
			});			
			directions.load("from: " + from + " to: " + to);
			gMap.closeInfoWindow();
		}
	);
	$("a.closeDirections").click(
		function()
		{
			hide($("#directionsContainer"));
			return false;
		}
	);
	
	$("#locationList li.mapLocation").click(
		function()
		{
			var locID = extractID($(this).attr("id"));
			GEvent.trigger(markers[locID],'click');
			gMap.setZoom(nativeZoom);
			return false;
		}
	).addClass("clickable");	
	
	//	activate current location
	if(locationID != 0)
	{
		if (markers[locationID])
		{
			GEvent.trigger(markers[locationID],'click');
		}
		else if ($("a.button.addLocationButton").size () > 0)
		{
			alert ('The currently-selected location is not yet positioned on the map. To fix this, click the location name to edit it, and make sure to provide a full valid address.');
			$("#location_" + locationID).animate ({backgroundColor:"#d00"},700).animate ({backgroundColor:"#fff"},700)
			.animate ({backgroundColor:"#d00"},700).animate ({backgroundColor:"#fff"},700);
		}
	}
	
	if (filters)
	{
		$("#sportFilterBox img.sport_filter_button").click(
			function(){
				sport = extractID($(this).attr("id"));
				if(sport == undefined || sport == 'allsports'){
					sport = '';
				}else{
					sport = sport.replace('-',' ');
				}
				updateDisplay();
				return false;
			}
		).addClass("clickable");	
		updateDisplay();
	}
});

function updateDisplay()
{
	//	show the correct location list and pushpins
	if(sport == "" || sport == undefined || sport == "allsports")
	{
		//	showing everything
		$("#filterDescriptor").html("All");
		for(locID in Locations)
		{
			markers[locID].show();
			$("#location_"+locID).removeClass("hidden");
		}
	}
	else
	{
		//	so we don't orphan the info window
		gMap.closeInfoWindow();
		$("#filterDescriptor").html(sport);
		
		for(locID in Locations)
		{
			if(indexOf(sport,Locations[locID]["Sports"]) == -1)
			{
				$("#location_"+locID).addClass("hidden");
				markers[locID].hide();
			}
			else
			{
				$("#location_"+locID).removeClass("hidden");
				markers[locID].show();
			}
		}
	}
	
	setSportButtonStatuses(sport);
}

function initializeMap(){
	var southwest = new GLatLng(map_bottom,map_left);
	var northeast = new GLatLng(map_top,map_right);
	var window = new GLatLngBounds(southwest,northeast);
	gMap = new GMap2(document.getElementById("map"));
	nativeZoom = gMap.getBoundsZoomLevel(window);
	if(nativeZoom > 15)
		nativeZoom = 15;
	gMap.setCenter(window.getCenter(),nativeZoom);
	gMap.addControl(new GLargeMapControl());
}

function placeLocationMarkers(drag){
	for(locID in Locations){
		var marker = new GMarker(
			new GLatLng(Locations[locID]["Latitude"],Locations[locID]["Longitude"]),
			{draggable:drag}
		);
		
		marker.LocationID = locID;
		marker.bindInfoWindow(document.getElementById("details_" + locID));	
		markers[locID] = marker;
		gMap.addOverlay(marker);
	}
}