
jQuery(function() {
	
	/*
	// content header slideshow
	jQuery(".content-header").gstSlideshow({
		zIndexForTopSlide: 700
	});
	*/
	
	jQuery("#strip-main>ul").gstSlideshow({
		zIndexForTopSlide: 700
	});
	
	jQuery("#sidebar .tabs").tabs();
	
	jQuery("#content .locations-map").each(function() {
		var theMap = this;
		
		if (GBrowserIsCompatible()) {
			
			// Gather the locations we'll be displaying
			var locations = jQuery("#content .location");
			if (locations.length <= 0) {
				//alert("No locations found");
			} else {
				jQuery(theMap).css("display", "block");
				
				// Create the map (don't forget to setCenter and add controls)
				var map = new GMap2(theMap);
				
				var zoom_level = 7;
				
				// Initialize the map, centered on Knoxville, TN
				map.setCenter(new GLatLng(35.868673,-84.207716), zoom_level);
				
				// Add a zoom/pan control
				map.addControl(new GLargeMapControl());
				
				//alert("Found " + locations.length + " locations.");
				
				var bounds = new GLatLngBounds();
				
				// Add each location
				locations.each(function() {
					var theLocation = this;
					
					// The location's title
					var locationTitle = jQuery(theLocation).find(".title").text();
					
					// The location's address
					var address = jQuery(theLocation).find(".address").text();
					
					//alert("Location: " + locationTitle + " @ " + address);
					
					if (address) {
						// Start a geocode request for the address
						var geocoder = new GClientGeocoder();
						geocoder.getLocations(address, function(response) {
							// Retrieve the object
							var place = response.Placemark[0];
							
							// Retrieve the latitude and longitude
							var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
							
							// Center the map on this point
							//this.setCenter(point, zoom_level);
							
							// Create a marker
							var marker = new GMarker(point);
							
							var marker_html = "<h2 style='background-image: none'>" + locationTitle + "</h2><p>" + address + "</p>";
							
							// Give it click behavior
							GEvent.addListener(marker, "click", function() {
								this.openInfoWindowHtml(marker_html);
							});
							
							// Add the marker to map
							map.addOverlay(marker);
							
							// Start with the info window open
							//marker.openInfoWindowHtml(marker_html);
							
							// Smart zoom & center
							bounds.extend(point);
							map.setCenter(bounds.getCenter(), Math.min(12, map.getBoundsZoomLevel(bounds)));
							
						});
					}
				});
				
				if (locations.length > 0) {
					// Make the directions form work
					//alert("# of directions forms: " + jQuery(theWrapper).find(".gst-map-directions-form").length);
					jQuery(".gst-map-directions-form").submit(function() {
						var $directionsForm = jQuery(this);
						
						var previousDirections = $directionsForm.data("gst-map-previous-directions");
						
						if (previousDirections) {
							previousDirections.clear();
						}
						
						var elemDirectionsContainer = jQuery(".gst-map-directions-container").get(0);
						if (elemDirectionsContainer) {
							// Use the GST_gmap_directions div as the target
							var dir = new GDirections(map, elemDirectionsContainer);
							
							// Combine the form fields into one address string
							var from_street = jQuery("#fromaddress_street").attr("value");
							var from_city = jQuery("#fromaddress_city").attr("value");
							var from_state = jQuery("#fromaddress_state").attr("value");
							var from_postal_code = jQuery("#fromaddress_postal_code").attr("value");
							var dirfrom = from_street + ", " + from_city + ", " + from_state + ", " + from_postal_code;
							
							// Convert the from address and to address into a directions string google maps understands
							dirstring = "from: " + dirfrom + " to: " + jQuery(".gst-map-address").text();
							
							//alert("dirstring = " + dirstring);
							
							// Get the directions
							var loadResult = dir.load(dirstring, {getSteps:true});
							
							// Store that there has been a directions request (for clearing the map before later requests)
							//previousDirections = dir;
							$directionsForm.data("gst-map-previous-directions", dir);
							
						}
						
						return false;
					});
				}
			}
		} else {
			//alert("GBrowserIsCompatible failed");
		}
	});
});

