// Google Maps API Geocoder
var geocoder = new GClientGeocoder();

// 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
	)
);

// On Load Event...
Event.observe(window,'load',function () {
	$('search-form').observe('submit', showLocation);
});

// Update the results...
function showLocation(ev) {
	ev.stop();
	geocoder.getLatLng($('search-input').value, plotAndPanTo);
}

function plotAndPanTo(point) {
	if (!point) {
		locationNotFound();
	} else {
		map.panTo(point);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindow(infoWindowText(point));
	}
}

// lookup failed
function locationNotFound() {
	alert("Couldn't find that location.");	
}

// This is the text that show up in the info box when they've searched for a point
function infoWindowText(point) {
	var infoText = "Would you like to narrow the list of parking spaces to the ones closest to this point?"
	var infoLink = "<a href=\"public_parking.php?point=#{lat},#{lng}&limit=#{limit}\"><br />Yes</a>";
	return infoText+infoLink.interpolate({lat: point.lat(),lng: point.lng(),limit: 16});
}