var gdir = false;
var startPointData = false;
var startPoint = false;
if (!geocoder) {
	var geocoder = new GClientGeocoder();
}

Event.observe(window,'load',function () {
	gdir = new GDirections(map,$('directions-container'));	
    GEvent.addListener(gdir, "error", HandleDirectionErrors);		
		
	// This defines the area around Nashvills... which gives the results priority
	geocoder.setViewport(
		new GLatLngBounds(
			new GLatLng(35.993840980952726,-86.98528289794922), // soutwest corner
			new GLatLng(36.27085020723902,-86.5945816040039) // northeast corner
		)
	);
	
	$('directions-control').observe('click',ToggleDirs);
});

function HandleDirectionErrors() {
	startPoint = false;

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("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.\n Error code: " + gdir.getStatus().code);
     
   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.");
}

function ParkItDirections(latlng) {		
	if (!gdir) return;
	
	if (!startPoint) {
		startPoint = GetStartPoint(latlng,false);
		return;
	}
	
	if (!startPointData) {
		geocoder.getLatLng(startPoint, function (point) {
			if (!point) {
				ResetAddress();
				GetStartPoint(latlng,true);
				return;
			}
			
			startPointData = point;
			LoadDirections(startPointData, latlng);
		});
	} else {
		LoadDirections(startPointData, latlng);
	}
}

function GetStartPoint(end, err) {
	var errString = '';
	if (err) {
		errString = "&err=1";	
	}
		
	Modalbox.show('/get_address_box.php?end='+end+errString,{title:"Enter Your Address"});
	
}

function ResetAddress() {
	startPoint = false;
	startPointData = false;
}

function LoadDirections(point,latlng) {
	gdir.loadFromWaypoints([point.lat()+', '+point.lng(),latlng]);
	DoAddRemove();
	ShowDirs();
}

function ShowDirs() {
	$('directions-container').show();
	$('directions-control').update("Hide Directions");
	$('directions-container').slideDown();
}

function DoAddRemove() {
	var removeLink = new Element('span', {id: 'remove-link'}).update("Reset Address");
	$('remove-link-container').appendChild(removeLink);
	removeLink.setStyle({cursor: 'pointer',fontWeight: 'bold'});
	removeLink.observe('click', function () {ResetAddress();HideDirs();$('directions-control').update('');$('remove-link').remove();});
}

function HideDirs() {
	$('directions-container').slideUp();
	$('directions-control').update("Show Directions");
}

function ToggleDirs() {
	if ($('directions-container').style.display == 'none') {
		ShowDirs();
	} else {
		HideDirs();
	}
}