var search_control = {
	neighborhoods: [],
	location_control: null,
	neighborhood_control: null,
	
	bind: function(neighborhoods, location_control, neighborhood_control) {
		search_control.neighborhoods = neighborhoods;
		search_control.location_control = $(location_control);
		search_control.neighborhood_control = $(neighborhood_control);
		
		search_control.location_control.observe('change', search_control.location_changed);
	},
	
	init: function() {
		with (search_control) {
			init_locations();
			init_neighborhoods();
			
			populate_neighborhoods();
		};
	},
	
	init_locations: function() {
		// nothing to do right now
	},
	
	init_neighborhoods: function() {
		with (search_control) {
			clear_neighborhoods();
			add_option(neighborhood_control, 'Select One', '-2');
			add_option(neighborhood_control, 'All', '-1');
		}
	},
	
	populate_neighborhoods: function() {
		with (search_control) {
			init_neighborhoods();
		
			var loc = selected_location();
			//alert(loc);
			var matches = find_neighborhoods(loc);
			matches.each(function(n) { add_option(neighborhood_control, n.name, n.id); });
		};
	},
	
	find_neighborhoods: function(location_id) {
		return search_control.neighborhoods.findAll(function(n) {
			return n.location_id == location_id;
		});
	},
	
	clear_neighborhoods: function() {
		search_control.neighborhood_control.options.length = 0;
	},
	
	selected_location: function() {
		return parseInt($F(search_control.location_control), 10);
	},
	
	// helpers
	add_option: function(ctl, text, value) {
		ctl.options[ctl.options.length] = new Option(text, value);
	},
	
	select_location: function(location) {
		search_control._select_option(search_control.location_control, location);
	},
	
	select_neighborhood: function(neighborhood) {
		search_control._select_option(search_control.neighborhood_control, neighborhood);
	},
	
	_select_option: function(select, option) {
		if (option == "") return;
		for(var i=0; i<select.options.length; i++) {
			if (select.options[i].text == option) {
				select.options[i].selected = "1";
				break;
			}
		}
	},
	
	// events
	location_changed: function() {
		//alert('hi');
		search_control.populate_neighborhoods();
	}
}

var util = {
	show_budget_help: function() {
		window.open("/budgetHelp.htm", "budgetHelp", "status=0,toolbar=0,menubar=0,resizable=0,width=200,height=250,scrollbars=0");
	},
	
	show_help: function(url, title) {
		window.open("/requestExplanation.htm", "requestExplanation", "status=0,toolbar=0,menubar=0,resizable=1,width=300,height=350,scrollbars=1");
	}
}
