var map;
var directionsPanel;
var directions;
var from;
		
$(function() {
	
	// hero fade
	$("#hero-feature").cycle({
		activePagerClass: 'active-slide',
		timeout: 7000,
		pager: '.hero-paging',
		manualTrump: true
	});
	
	$("#email").bind("focus", function(e) {
		$("#label").addClass("focus");
	});
	
	$("#email").bind("blur", function(e) {
		if ($(this).val() == '') {
			$("#label").removeClass("focus");
			$("#label").removeClass("keypress");
		}
	});
	
	$("#email").bind("keypress", function(e) {
		$("#label").addClass("keypress");
	});
	
	$("#directions").bind("submit", function(e) {
				
		$("#google-maps").addClass("loading");
		
		if (GBrowserIsCompatible()) {
		    directionsPanel = document.getElementById("google-maps");
		    directionsPanel.innerHTML = '';
		    $("#error").hide();
			from = document.getElementById("user-location").value;
		
			var geo = new GClientGeocoder(new GGeocodeCache()); 

		    var reasons=[];
		    reasons[G_GEO_SUCCESS]            = "Success";
		    reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		    reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		    reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		    reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		    reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		    reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
		    reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
		    reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
		    reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";

			directions = new GDirections(null, directionsPanel);

			geo.getLatLng(from, function(from_point){
				if (from_point) {
					var to_point = new GLatLng(35.8735169, -86.3854229);
					var waypoint_query = ([from+"@"+from_point,"3320 Memorial Blvd, Murfreesboro, TN@"+to_point]);
					GEvent.addListener(directions, "error", handleErrors);
					directions.loadFromWaypoints(waypoint_query);
				}else {
					var result = geo.getCache().get(from_point);
					if (result) {
						var reason = "Code" + result.Status.code;
						if (reasons[result.Status.code]) {
							reason = reasons[result.Status.code];
						}
					}else {
						var reason = "";
					}
					alert("Could not find " + from + "\n" + reason);					
				}

			});
		}
		
		$("#google-maps").removeClass("loading");
			
		return false;
	});
	
	$("#user-location").bind("focus", function(e) {
		$("#eg").addClass("focus");
	});
	
	$("#user-location").bind("blur", function(e) {
		if ($(this).val() == '') {
			$("#eg").removeClass("focus");
			$("#eg").removeClass("keypress");
		}
	});
	
	$("#user-location").bind("keypress", function(e) {
		$("#eg").addClass("keypress");
	});
	
})

function handleErrors() {
	   if (directions.getStatus().code == 602)
	     $("#error").html("<p>No corresponding geographic location could be found for the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.</p>");
	
	   else if (directions.getStatus().code == 604)
	     $("#error").html("<p>No corresponding geographic location could be found for the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.</p>");

	   else if (directions.getStatus().code == 500)
	     $("#error").html("<p>A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.</p>");
	   
	   else if (directions.getStatus().code == 601)
	     $("#error").html("<p>The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.</p>");
	     
	   else if (directions.getStatus().code == 610)
	     $("#error").html("<p>The given key is either invalid or does not match the domain for which it was given.</p>");

	   else if (directions.getStatus().code == 400)
	     $("#error").html("<p>A directions request could not be successfully parsed.</p>");
	    
	   else $("#error").html("<p>An unknown error occurred.");	
	
	   $("#error").show();
	
}
