// setup a few global vars

// keep counts of how many are visible so we can show/hide a message
// if there are none left showing
if (!COUNTIES_VISIBLE) var COUNTIES_VISIBLE = 0;
if (!CITIES_VISIBLE) var CITIES_VISIBLE = 0;

var subdivision_autocomplete_menu; // stores instance of autocomplete class


// 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 all the county checkboxes for a given state
// and hides the tip
function showCounties(state) {
	$$("#ul_county li[rel=" + state + "]").each( function(c_li) {
		$(c_li).show();
		if (c_li.className != 'county_label') {
			COUNTIES_VISIBLE++;
		}
	});
	// hide tip
	$("county_tip").hide();
	//$('county_count').update(COUNTIES_VISIBLE);
}


// shows all the city checkboxes for a given state
// and hides the tip
function showCities(county) {
	$$("#ul_city li[rel=" + county + "]").each( function(c_li) {
		$(c_li).show();
		if (c_li.className != 'county_label') {
			CITIES_VISIBLE++;
		}
	});
	// hide tip
	$("city_tip").hide();
	//$('city_count').update(CITIES_VISIBLE);
}

// hides all the county checkboxes for a given state
// if no state parameter is passed in, it hides all 
// the county checkboxes
// Also shows the tip if there are no checkboxes visible
function hideCounties(state) {
	if (state) {
		$$("#ul_county li[rel='" + state + "']").each( function(c_li) {
			if ($(c_li).visible()) {
				$(c_li).hide();
				if (c_li.className != 'county_label') {
					COUNTIES_VISIBLE--;
					var checkbox = $(c_li).firstDescendant().firstDescendant();
					// if this was checked, hide the cities for this county
					if (checkbox.checked == true) {
						hideCities(checkbox.value);
					}
					checkbox.checked = false;
					
				}
			}
		});
	} else { // hiding all counties
		$$('#ul_county li[rel]').each( function(c_li) {
			$(c_li).hide();
			if (c_li.className != 'county_label') {
				var checkbox = $(c_li).firstDescendant().firstDescendant();
				// if this was checked, hide the cities for this county
				if (checkbox.checked == true) {
					hideCities(checkbox.value);
				}
				checkbox.checked = false;
			}
		});
		COUNTIES_VISIBLE = 0;
	}
	// show tip if no counties are left showing
	if (COUNTIES_VISIBLE == 0) {
		$("county_tip").show();
	}
	//$('county_count').update(COUNTIES_VISIBLE);
}

// hides all the city checkboxes for a given county and unchecks them
// if no county parameter is passed in, it just hides all
// the city checkboxes
// Also shows the tip if there are no checkboxes visible
function hideCities(county) {
	if (county) {
		$$("#ul_city li[rel='" + county + "']").each( function(c_li) {
			if ($(c_li).visible()) {
				$(c_li).hide();
				if (c_li.className != 'county_label') {
					CITIES_VISIBLE--;
					var checkbox = $(c_li).firstDescendant().firstDescendant(); 
					checkbox.checked = false;
				}
			}
		});
	} else {
		$$('#ul_city li[rel]').each( function(c_li) {
			$(c_li).hide();
			if (c_li.className != 'county_label') {
				var checkbox = $(c_li).firstDescendant().firstDescendant(); 
				checkbox.checked = false;
			}
		});
		CITIES_VISIBLE = 0;
	}
	// show tip if no cities are left showing
	if (CITIES_VISIBLE == 0) {
		$('city_tip').show();
	}
	//$('city_count').update(CITIES_VISIBLE);
}




// this attaches events to all the elements on the advanced search page
function attachAdvancedSearchEvents()
{
	// NEED TO INITIALIZE THE COUNTS OF HOW MANY CITY AND COUNTY INPUTS ARE VISIBLE
	// FOR THE CASE OF MODIFY SEARCH WHERE SOME MAY ALREADY BE SHOWING

	// sets class name switching behavior for all "scroll boxes" (the scrolling divs containing lists of checkboxes)
	$$('ul.scroll_box li label input').each( function(the_input) {
		$(the_input).observe('click', function() {
			// set the class name of the inputs label to selected if it is checked
			if (the_input.checked == true) {
				$(the_input.parentNode).addClassName('selected');
			} else {
				$(the_input.parentNode).removeClassName('selected');
			}
		});
	});
	
	// attach events to "search by" select box
	$("SelectSearchBy").observe('change', function() {
		
		// show the fields for the selected "search by" option and hides the others
		var section = $("SelectSearchBy").getValue();
			
		// DO AN AJAX CALL - GET SMARTY PARTIAL FOR THE SEARCH BY SECTION THAT WE WANT
		// show loading message while the sections are switching out
		$("SearchBySection").update('<div class="one_column" style="height:200px;"><p class="instructions">Loading...</p></div>');
		
		var searchby_url="?";	
		var action = 'get_search_by_inputs_for_' + section;
		new Ajax.Request(searchby_url, {
			method:'post',
			parameters:{
				controller: 'ListingSearch',
				product: 'web',
				action: action
			},
			onSuccess: function(transport) {
				searchby_html = transport.responseText;
				$("SearchBySection").update(searchby_html);
			},
			onFailure: function() {
				alert("The ajax request failed - was trying to retrieve inputs: ListingSearch->" + action);
			}
		});
		
		// if searching by mls number, the other advanced search parameters should not be considered...
		if (section == 'mls_no') {	
			// disable all the other inputs to prevent them from posting
			// and then hide them
			disableInputs("SearchFormMain");
			$("SearchFormMain").hide();
		
		} else { // not search by mls_no
			// re-enable all the other inputs so they can post again
			// and then show them
			enableInputs("SearchFormMain");
			$("SearchFormMain").show();
		}
	});


	// Handling the EULA case where there is an eula
	if ($("accept_eula")) 
	{
		$('AdvancedSearchSubmit').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 {	
				$('AdvancedSearchForm').submit();
			}
			
		});
	} // end if there is an eula

} // end attachAdvancedSearchEvents()



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

});

