// Note: remember to avoid creating closures on nodes, and in general
// be aware of closures + event listeners ---> reference never garbage collected
//

// Things with seal of approval for closure:
// -the AutoComplete instance
//
//

// firefox will ignore the modality of an alert() and still trigger keyboard events
// this means if the user presses enter to close the alert, the search will run again
// trapping the user in a loop as long as they use enter
var noRecurseCheck = false; // TODO: the above problem is still happening
function basicSearch()
{
try {
	if (noRecurseCheck)
		return;
	noRecurseCheck = true;
	
	var elms = document.getElementsByName("searchtype");
	var searchtype = "Category";
	for (var i = 0; i < elms.length; ++i)
		if (elms[i].checked) {
			searchtype = elms[i].value;
			break;
		}
	
	var query = document.getElementById('basic-search-textfield').value;
	var cityId = cityAutoComplete.getSelectedValue();//document.getElementById('city-textfield').value;
	cityAutoComplete.validate();

	if (!cityId || (cityAutoComplete.requireValid && !cityAutoComplete.isValid)) {
	
		alert('Please enter a valid city or state.');
		return false;
	}
	switch (searchtype)
	{
	default:
	case "Category":
		if (!categoryAutoComplete.isValid) {
			alert('Please select a category');
		} else {
			searcher.categorySearchByName(query, cityId);
		}
		break;
	case "Keyword":
		searcher.keywordSearch(query, cityId);
		break;
	}
} finally {
	noRecurseCheck = false;
}
}

var categoryAutoComplete;
var cityAutoComplete;

function setSearchBackgroundRandomly() {
	var rand = Math.floor((Math.random()*3)+1);
	document.getElementById('search_box').style.backgroundImage="url(gfx/new/search_background" + rand + ".jpg)"
}

function onBodyLoad()
{

	document.getElementById('city-textfield').autocomplete = "off";
	var basicSearchTextField = document.getElementById('basic-search-textfield');
	basicSearchTextField.autocomplete = "off";
	categoryAutoComplete = new AutoComplete('basic-search-textfield', 'basic-search-autocomplete-window');
	cityAutoComplete = new AutoComplete('city-textfield','city-autocomplete-window');
	cityAutoComplete.requireValid = true;
	cityAutoComplete.autoSelectSingleMatches = true;
//	autoComplete.show();
	basicSearchTextField.focus();

	categoryAutoComplete.onRequestCompletions = function(text)
	{
        window.processAutoComplete("Category", text, ++this.sentRequestId, this.requestHandler, this);
	};
	categoryAutoComplete.onSubmit = function(text)
	{
//		Logger.Log('categoryAutoComplete.onSubmit;text='+text);
		//We don't want the search to automatically go through, they need to click the button
		//if (cityAutoComplete.requireValid && cityAutoComplete.isValid)
		//	basicSearch();
		if(document.getElementById('city-textfield').value != "") ViewResults();
	};

	cityAutoComplete.onRequestCompletions = function(text)
	{
		var index = text.indexOf(',');
		if (index != -1)
			text = text.substr(0, index);
		window.processAutoComplete("City", text, ++this.sentRequestId, this.requestHandler, this);
	};
	cityAutoComplete.onSubmit = function(text)
	{
//		Logger.Log('cityAutoComplete.onSubmit;text='+text);
		//We don't want the search to automatically go through, they need to click the button
		//basicSearch();
		if(document.getElementById('basic-search-textfield').value != "") ViewResults();
	};
}
