// Hopefully this behaves like jquery's document ready handler
document.observe("dom:loaded", function(){

	// Deprecated
	function NLPAjaxSearch(){
		new Ajax.Request('/larks' + location.pathname.match(/\/[^\/]+$/), {
			method: 'get',
			parameters: {'nlp_query': $F('nlp_search_input')}
		});
	}
	
	// perform search
	function NLPSearch(){
		var new_href = 'http://' + location.host;
		new_href += (location.pathname.match(/larks_map/)) ? '/top_larks_map' : '/top_larks';		// if user is currently viewing map, keep them there
		location.href = new_href + '?today=' + fmtDate("%Y%m%d") + '&nlp_q=' + $F('nlp_search_input')
	}

	// If the user clicks the go button or the user hits enter, perform search
	$('nlp_search_input').observe('keypress', function(e){if (e.keyCode == 13) NLPSearch()});
	$('nlp_search_sub').observe('click', function(e){e.stop(); NLPSearch()});  // stop is there so href isn't followed

	// search bar should have focus on page load
	$('nlp_search_input').focus();
	
	// Remember the user's control preference (i.e. shown or hidden)
	if ($('filterLink')){			// Only try to attach listener if filterLink exists
		$('filterLink').observe('click', function(){
			new Ajax.Request('/larks/toggle_controls', {method: 'get'})
		});
	}
	
});