// QUICK SEARCH FORM
// the Quick Search form has it's own subpage and is also available as a homepage widget.
// note: we need to allow for the possibility of multiple quick search forms on a single page.


// these are used to store instances of the autocomplete class, so their methods can be called externally
var city_autocomplete_menu;
var zip_autocomplete_menu;
var county_autocomplete_menu;

var AUTOCOMPLETES = new Hash(); // associative array of autocomplete objects, indexed by a unique id
//AUTOCOMPLETES.set(this.content_src, LB); // index, autocomplete obj


// disables all inputs inside of a parent element
function disableInputs(parentId) {
	$$('#'+parentId+' input').each( function(s) {
		s.disabled = true;
	});
	$$('#'+parentId+' select').each( function(s) {
		s.disabled = true;
	});
}

// enables all inputs inside of a parent element
function enableInputs(parentId) {
	$$('#'+parentId+' input').each( function(s) {
		s.disabled = false;
	});
	$$('#'+parentId+' select').each( function(s) {
		s.disabled = false;
	});
}

// shows the "search by" title and input for the id selected
// hides all others
// called onchange
function toggleSearchBySection(id) {
	$$('#SearchBy div.input_wrapper').each( function(s) {
		if (id == s.id) {
			s.show();
		} else {
			s.hide();
		}
		// when switching sections be sure to reset all the autocomplete input values
		$("SEARCH_city_autocomplete").value = "";
		$("SEARCH_zip_autocomplete").value = "";
		$("SEARCH_county_autocomplete").value = "";
		$("SEARCH_mls_no").value = "";
		// and hide the autocomplete menu if it is open
		city_autocomplete_menu.hide();
		zip_autocomplete_menu.hide();
		county_autocomplete_menu.hide();
		
	});
	
	// if searching by mls number, the other parameters should not be considered...
	if (id == 'SearchBy_mls_no') {	
		// disable all the other inputs to prevent them from posting
		// and then hide them
		disableInputs("SearchFormMain");
		$("SearchFormMain").hide();
	} else {
		// re-enable all the other inputs so they can post again
		// and then show them
		enableInputs("SearchFormMain");
		$("SearchFormMain").show();
	}
}





// this attaches events to all the elements on the quick search form
function attachQuickSearchEvents() {

	// ** need to make sure this action is in ListingSearch controller *****
	common_request_parameters = Object.toQueryString({
		controller : 'Search',
		action : 'autocomplete_from_mls'
	});
	
	
	// QUICK SEARCH SUBPAGE ONLY
	
	// attach autocomplete class to autocomplete fields
	if ($("SEARCH_city_autocomplete") && $("SEARCH_city_autocomplete").id == 'SEARCH_city_autocomplete') {
		city_autocomplete_menu = new Ajax.Autocompleter("SEARCH_city_autocomplete",  "AutoCompleteMenu", "?", {
			method : 'post',
			parameters : common_request_parameters,
			minChars: -1
		});
	}
	
	
	if ($("SEARCH_zip_autocomplete")) {
		zip_autocomplete_menu = new Ajax.Autocompleter("SEARCH_zip_autocomplete",  "AutoCompleteMenu", "?", {
			method : 'post',
			parameters : common_request_parameters,
			minChars: -1
		});
	}
	if ($("SEARCH_county_autocomplete")) {
		county_autocomplete_menu = new Ajax.Autocompleter("SEARCH_county_autocomplete",  "AutoCompleteMenu", "?", {
			method : 'post',
			parameters : common_request_parameters,
			minChars: -1
		});
	}
	
	
	// select box for search by
	if ($("SelectSearchBy")) {
		$("SelectSearchBy").observe('change', function() {
			var index = $("SelectSearchBy").selectedIndex;
			var div_id = 'SearchBy_' + $('SelectSearchBy').options[index].value;
			toggleSearchBySection(div_id);
		});
	}
	
	
	
	// "View All" links should pop open their respective autocomplete menus
	// populated with all the possible options
	// invoke the method of instance of the autocomplete class
	if ($("ViewAllCities")) {
		$("ViewAllCities").observe('click', function(event) {
			Event.stop(event); // stop the href from resolving
			$("SEARCH_city_autocomplete").value = "";
			$("SEARCH_city_autocomplete").focus();
			city_autocomplete_menu.activate();
		});
	}
	if ($("ViewAllZips")) {
		$("ViewAllZips").observe('click', function(event) {
			Event.stop(event); // stop the href from resolving
			$("SEARCH_zip_autocomplete").value = "";
			$("SEARCH_zip_autocomplete").focus();
			zip_autocomplete_menu.activate();
		});
	}
	if ($("ViewAllCounties")) {
		$("ViewAllCounties").observe('click', function(event) {
			Event.stop(event); // stop the href from resolving
			$("SEARCH_county_autocomplete").value = "";
			$("SEARCH_county_autocomplete").focus();
			county_autocomplete_menu.activate();
		});
	}
	
	
	
	// QUICK SEARCH WIDGET ONLY
	
	// attach autocomplete class to city autocomplete fields
	$$('div.quick_search_frame div.search_by_city input').each( function(ac_field) {
		// create new AutoComplete obj: pass in ids for the input and the menu
		var ac_obj = new Ajax.Autocompleter(ac_field.id,  ac_field.up().next('.auto_complete_menu').id, "?", {
			method : 'post',
			parameters : common_request_parameters,
			minChars: -1
		});
		
		// add to global hash of autocomplete menu objects for future reference.
		// indexed by the id of the text input
		AUTOCOMPLETES.set(ac_field.id, ac_obj);
	});
	
	// view all cities link should open the appropraite autocomplete menu
	$$('div.quick_search_frame div.search_by_city a.view_all').each( function(va_link) {
		va_link.observe('click', function(event) {
			Event.stop(event); // stop the href from resolving
			// set the value of this widget's autocomplete field to empty and activate the menu
			va_link.next('input').value = "";
			va_link.next('input').focus();
			var ac_id = va_link.next('input').id;
			AUTOCOMPLETES.get(ac_id).activate();
		});
	});
	
	
	// BOTH SUBPAGE AND WIDGET
	
	// property type inputs - make "Any" an exclusinve choice from the other options
	$$('ul.property_type_list input').each( function(cb) {
		cb.observe("click", function() {
			if (cb.checked) {
				if (cb.value == "Any") {
					cb.up(2).select('input[value!="Any"]').each( function(cb2) {
						cb2.checked = false;
					});
				} else {
					cb.up(2).select('input[value="Any"]').each( function(cb2) {
						cb2.checked = false;
					});
				}
			}
		});
	});

	// Handling the EULA case where there is an eula
	if ($("accept_eula")) 
	{
		$('QuickSearchSubmit').observe("click", function (click) {
			Event.stop(click);
			if ($('accept_eula').checked == false) {
				alert ("Please read our search terms and check the 'Yes I agree to the search terms' box to continue");
			} else {	
				$('QuickSearchForm').submit();
			}
			
		});
	} // end if there is an eula

} // end attachQuickSearchEvents()



Event.observe(window, 'load', function() {
	attachQuickSearchEvents();
});
