$(document).ready(function() {       
	var districtOptionNames = {};
	$('#L_SchoolDistrict option').each(function(i,e){districtOptionNames[e.value]=e.text;});

	function regeneratePullDown(selector) {
	       $('.rentalList #L_SchoolDistrict option')
        	  .not('[value="Red Hook Central"], [value="Rhinebeck Central"]')
	          .remove();

		$("#L_City, #L_SchoolDistrict").dropdownchecklist("destroy");
		$("#L_City, #L_SchoolDistrict").dropdownchecklist({
				emptyText: " Any",
				width: 170,
					maxDropHeight: 250,
					onComplete: function(sel) {
					  if ($(sel).attr('id') == 'L_City') {
						  $('#radio').attr('checked', true);
						  $('#L_SchoolDistrict').val('');
						  regeneratePullDown('#L_SchoolDistrict');						  
					  } else {
						  $('#radio2').attr('checked', true);
						  $('#L_City').val('');
						  regeneratePullDown('#L_City');
					  }
					}
				});
	}
		
	var updater = function(selector) {
		var data = sys.fireEventShift('hb:get-county-data', selector.value);
		$('SELECT#L_City').each(function(index,sel) {
			if (sel.options.length) sel.options.length = (sel.options[0].value?0:1);
			var values = $(this).get(0).SelectedValue;
			if (!values) values = {};

			for (var i=0;i<data.cities.length;i++) {
				sel.options[sel.options.length] = new Option(data.cities[i],data.cities[i],false,values[data.cities[i]] == 1);
			}

		});
		$('SELECT#L_SchoolDistrict').each(function(index,sel) {
			if (sel.options.length) sel.options.length = (sel.options[0].value?0:1);
			var values = $(this).get(0).SelectedValue;
			if (!values) values = {};
			for (var i=0;i<data.schools.length;i++) {
				sel.options[sel.options.length] = new Option(districtOptionNames[data.schools[i]],data.schools[i],false,values[data.schools[i]] == 1);
			}

		});
		regeneratePullDown('#L_City, #L_SchoolDistrict');
	}

	$('#L_City,#L_SchoolDistrict').each(function() {
		if ($(this).get(0).tagName.toLowerCase() == 'select') {
			var elem = $(this).get(0);
			var opts = elem.options;
			elem.SelectedValue = {};
			for (var i=0;i<opts.length;i++) {
				if (opts[i].selected) elem.SelectedValue[opts[i].value] = 1;
			}
			return;
		}

		var id=$(this).attr('id');
		$(this).replaceWith('<select id="'+id+'" name="'+$(this).attr('name')+'" />');
		var el=$('#'+id).append("<option value=''>Any</option>");
		el.attr('value', $(this).attr('value')); // preserve selected
		el.SelectedValue = {};
		el.SelectedValue[$(this).attr('value')] = 1;  // preserve selected
		if ($(this).attr('value')) el.append("<option selected>"+$(this).attr('value')+"</option>");
	});

	$('SELECT#L_Area').change(function(event) { updater(event.target); });
	$('SELECT#L_Area').each(function(index,elem) { updater(elem); });
	$('SELECT#L_City,SELECT#L_SchoolDistrict').change(function(event) { 
								if (event.target.id=='L_City') $('SELECT#L_SchoolDistrict').attr('value',''); 
								if (event.target.id=='L_SchoolDistrict') $('SELECT#L_City').attr('value',''); 
							});

	$('.buttonSave').click(function() {
		document.forms.searchForm.appendChild(document.getElementById('saveSearchFlag'));
		document.forms.searchForm.appendChild(document.getElementById('saveSearchItemType'));
		document.getElementById('btnSubmitSearchForm').click();         
	});
});

