$(document).ready(function() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("mapContainer"));
	var marker = new GMarker(new GLatLng(42.602526, -71.626705));
	var html = '<div class="mapStyle">'+
		'<span style="color:#006633; font-style:bold;">RiverCourt Residence</span><br />'+
		'8 West Main Street, Rt. 225<br />'+
		'West Groton, MA 01450<br />'+
		' Phone: 978-448-4122</div>';

	map.setCenter(new GLatLng(42.602526, -71.626705), 13);
	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 = '50 Kerry Place, Norwood, MA 02062';
			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; 
	});
});