window.addEvent('domready', function() {

	function replaceOptions(el, new_options) {
		var options;
		var selected = el.value;
		
		options = el.getChildren('option');
		for(var idx = 2; idx < options.length - 2; idx++) {
			options[idx].dispose();
		}
		var last_option = options[1];
		Array.each(new_options, function(option){
			var option = new Element('option', {
				value: option.key,
				html: option.value,
				selected: (selected == option.key ? 'selected' : '')
			});
			option.inject(last_option, 'after');
			last_option = option;
		});
	}
	
	function updateSearch() {
		var request = new Request.JSON({
			url: '/cgi-bin/search_dropdowns.pl',
			onSuccess: function(json){
				replaceOptions($('category'), json.categories);
				replaceOptions($('country'), json.countries);
				replaceOptions($('price'), json.prices);
			}
		}).post({'category':$('category').value,'country':$('country').value,'price':$('price').value});
	};

	$('category').addEvent('change', function(){
		updateSearch();
	});
	$('country').addEvent('change', function(){
		updateSearch();
	});
	$('price').addEvent('change', function(){
		updateSearch();
	});
	updateSearch();
});
