$(function() {

	$('#locations_list').change(locationsListChangeHandler);

	$('.locationList li a').click(function() {
		var locationName = this.innerHTML;

		$('.locationList li a.current').removeClass('current');
		$(this).addClass('current');
		
		locationsList.displayInfo(locationName);
		showAddress(locationsList.locations[locationName]);

		$('.locationDirectionsArea').show();
		$('.locationInfo').show();

		return false;
	});
	
	$('body').unload(function(){ GUnload(); });

});

function locationsListChangeHandler() {
	showLocationList(this.value);
	var firstLocation = $('#'+this.value).children().eq(0).find("a").html();
	locationsList.displayInfo(firstLocation);
	showAddress(locationsList.locations[firstLocation]);
	$('.locationDirectionsArea').show();
	$('.locationInfo').show();
}

function showLocationList(listName) {
	$('.locationList:visible').hide(); // hide the one thats visible
	$('#'+listName).show(); // show the one we want
}

function LocationsList() {
	this.locations = new Array();

	this.addLocation = function(params) {
		this.locations[params['sitename']] = params;
	}
	
	this.displayInfo = function(name) {
		$('#sitename').html(this.locations[name]['sitename']);
		$('#address').html(this.locations[name]['address']);
		$('#phone').html(this.locations[name]['phone']);
		$('#fax').html(this.locations[name]['fax']);
		$('#officers').html(this.locations[name]['officers']);
		$('#lobby_hours').html(this.locations[name]['lobby_hours']);
		$('#driveup_hours').html(this.locations[name]['driveup_hours']);
		$('#coin_toss').html(this.locations[name]['coin_toss']);
		$('#directions').html(this.locations[name]['directions']);
	}
}

google.load("maps", "2");

var map = null;
var geocoder = null;

// Call this function when the page has been loaded
function initialize() {
	map = new google.maps.Map2(document.getElementById("googleMap"));
	geocoder = new google.maps.ClientGeocoder();

	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl(true));
	map.addControl(new GScaleControl());
//	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(32.990236,-82.529297), 4);
}

function showAddress(location) {
	if (map == null) { initialize(); }

	map.clearOverlays();
	
	var cleanaddress = location['cleanaddress'];
	var address = location['address'];
	var sitename = location['sitename'];
	var coords = location['coords'];

	if (coords != null && coords != "") {
		var latLng = coords.split(",");
		var pt = new GLatLng(latLng[0],latLng[1]);
		map.setCenter(pt, 13);
		var marker = new GMarker(pt);
		map.addOverlay(marker);
	} else {
		geocoder.getLatLng(
			cleanaddress,
			function(point) {
				if (!point) {
					alert(cleanaddress + " not found");
				} else {
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
//					marker.openInfoWindowHtml("<strong>" + sitename + "</strong><br/>" + address);
				}
			}
		);
	}
}

//google.setOnLoadCallback(initialize);


//$('body').unload(google.maps.unload());