$(document).ready(function() {
 
	function showAddress(address) {
	  geocoder.getLatLng(
	    address,
	    function(point) {
	      if (!point) {
	        alert(address + " not found");
	      } else {
			var html = $("#mapHtml").html();        
			map.setCenter(point, 13);
	        var marker = new GMarker(point);
	        map.addOverlay(marker);
	        marker.openInfoWindowHtml(html);
	      }
	    }
	  );
	}
 
 if (GBrowserIsCompatible()) {
	var geocoder = new GClientGeocoder();
	
	var address = $('#address').html();
	var address = address.replace('<br>', ', ');
	var address = address.replace('<BR>', ', ');
	var address = address.replace('<br />', ', ');
	var address = address.replace('<p>', '');
	var address = address.replace('</p>', '');
	var address = address.replace('<P>', '');
	var address = address.replace('</P>', '');
	// alert( address );
	
	var map = new GMap2(document.getElementById("mapContainer"));
	// var marker = new GMarker(new GLatLng(42.179631, -71.19322));
	// var marker = new GMarker(escape(address));
	// var html = $("#mapHtml").html();
	
	showAddress(address);
	
	// map.setCenter(new GLatLng(42.179631, -71.19322), 13);
	// map.setCenter(escape(address));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	// map.addOverlay(marker);
	// marker.openInfoWindowHtml(html);

  }
  	// default text for fields
	$(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    $(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    $(".defaultText").blur();  
  	
	$("#getDirections").bind("submit", function() { 
	// $("#getDirectionsLink").bind("click", function() { 
		// make sure addr and csz are filled in
			var errors = false;
			var directionsResponse = $("#directionsResponse");
			var fromAddress;
			var toAddress = address;
			var errorText = 'Please provide both your address and your city, state and zip information.';
			$(".defaultText").each(function() {
				if($(this).val() == $(this)[0].title) {
					errors = true;
					$(this).val($(this)[0].title);
				}
			});
			$('#mapContainer').addClass('mapContainerWithDirections');
			var map;
			var directionsPanel;
			var directions;
			fromAddress = $('#addr').val();;
			map = new GMap2( document.getElementById("mapContainer") );
			directionsPanel = document.getElementById("directionsPanel");
			directions = new GDirections(map, directionsPanel);
			fromToStr = "from: "+fromAddress+" to: "+toAddress;
			GEvent.addListener(directions, "load", onGDirectionsLoad);
            GEvent.addListener(directions, "error", handleErrors);
			directions.load(fromToStr);
			function handleErrors() {
				errors = true;
				if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
					errorText = 'That location could not be found.';
				else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
					errorText = 'There was a problem with your request. Please try again.';
				else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
					errorText = 'Please specify a location';
				/*
				else if (gdir.getStatus().code == G_GEO_BAD_KEY)
					alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
				else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
					alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
				*/
				else alert("An unknown error occurred. Please try again.");
				
				// return errorText;
				directionsResponse.addClass('submissionFail');
				directionsResponse.html(errorText);			
				directionsResponse.show();
				$('#directionsPanel').hide();
				
			}
			
			function onGDirectionsLoad() {
				if(errors == true) {
					directionsResponse.addClass('submissionFail');
					directionsResponse.html(errorText);			
					directionsResponse.show();
					$('#directionsPanel').hide();
				} else {
					directionsResponse.addClass('submissionSuccess');
					directionsResponse.html('You can find your directions below.');
					directionsResponse.show();
					// alert(fromAddress);
					$('#directionsPanel').show();
				}	
			}
		return false; 
	});
});
