var Filter = {};
Filter.visible = null;
Filter.disable = function() {
	Filter.visible = $('#searchContainer').is(':visible');
	Filter.hide();
	$('#searchBottom div').hide();
	$('#searchBottom').get(0).onclick = null;
}
Filter.enable = function() {
	$('#searchBottom').get(0).onclick = function() {
		Filter.toggle();
		return false;
	};
	$('#searchBottom div').show();
	if (Filter.visible) {
		Filter.show();
	}
}

Filter.hide = function() {
	$('#searchContainer').slideUp('fast');
	$('#searchBottom div').html('open');
}
Filter.show = function() {
	$('#searchContainer').slideDown('fast');
	$('#searchBottom div').html('close');	
}
Filter.toggle = function() {
	if ($('#searchContainer').is(':visible')) {
		Filter.hide();
	} else {
		Filter.show();
	}
}

Filter.region = function(elt) {
	// reset other region links
	$('#regionlist a.highlighted').removeClass('highlighted');
	
	// highlight current
	if (elt) {
		$(elt).addClass('highlighted');
		var region = $(elt).attr('value');
	}
	else {
		var region = '';
	}

	if (region) {
		filter.addFilter('region', region);
	}
	else {
		filter.removeFilter('region');
	}
	filter.setOffset(1).search();
	ViewManager.showStep(2);
};
Filter.region.showMap = function(value) {
	$('.regions').removeClass().addClass('regions').addClass(value);
	$('.regions').show();
	$('#premadeTripAction').hide();
	
};
Filter.region.hideMap = function(value) {
	$('.regions').hide();
	$('#premadeTripAction').show();
};

Filter.subtype = function(value) {
	var subtype = value;
	if (subtype) {
		filter.addFilter('subtype', subtype);
	}
	else {
		filter.removeFilter('subtype');
	}
	filter.setOffset(1).search();
}

Filter.getTypes = function(value) {
	var category = value;

	// reset all type, subtype and search
	filter.removeFilter('type');
	filter.removeFilter('subtype');

	if (!category) {
		filter.removeFilter('category');
		filter.setOffset(1).search();
		Filter.resetSelect(typesDropDown, 'Select a Type');
		Filter.resetSelect(subTypeDropDown, 'Select a Subtype');
		return;
	}

	filter.addFilter('category', category).setOffset(1).search();

	ajax('/category/types/' + escape(category), null, function(json) {
		Filter.filterHelper(typesDropDown, 'Select a Type', json.types);

		// reset subtype
		Filter.resetSelect(subTypeDropDown, 'Select a Subtype');
	});
};

Filter.getSubtypes = function(value) {
	var category = categoriesDropDown.getSelectedValue();
	var type = value;

	filter.removeFilter('type');
	filter.removeFilter('subtype');

	if (!type) {
		Filter.resetSelect(subTypeDropDown, 'Select a Subtype');
		filter.setOffset(1).search();
		return;
	}
	
	filter.addFilter('type', type).setOffset(1).search();

	ajax('/category/subtypes/' + escape(category) + '/' + escape(type), null, function(json) {
		Filter.filterHelper(subTypeDropDown, 'Select a Subtype', json.subtypes);
	});
};

Filter.filterHelper = function(dropDown, defaultValue, options) {
	Filter.resetSelect(dropDown, defaultValue);
	if (options) {
		dropDown.addOptionsFromJson(options).enable();
	}
};

Filter.resetSelect = function(dropDown, defaultValue) {
	dropDown.reset().addOption('', defaultValue).disable();
};

Filter.reset = function() {
	$('#regionlist a.highlighted').removeClass('highlighted');
	filter.removeFilter('region');
	categoriesDropDown.selectFirst(); 
	ViewManager.showStep(1);
}

/* deprecated */
Filter.resetRegions = function() {
	Filter.region();
	$('#regions').removeClass();
}

Filter.resetCategories = function() {
	categoriesDropDown.selectFirst();
};
