FilterPanel = {
	CATEGORY_ID: null,
	STR_EXPAND: 'развернуть',
	STR_COLLAPSE: 'свернуть',
	alreadyToggled: {'sorting-caps': true},// columns that have already been toggled
	expandListCookies: [],
	toggleSortPanel: function() {
		FilterPanel.toggle('sorting-caps', 'sorting-caps');
	},
	/**
	 * toggle expanded items visibility
	 */
	toggle: function(id, column, column_count, selected) {
		var show;
		if (column in FilterPanel.alreadyToggled)
		{
			show = FilterPanel.doToggle(id);
		} else {
			FilterPanel.expand(id, column, column_count, selected);
			show = true;
		}
		var cookieName = 'f' + column + '_' + FilterPanel.CATEGORY_ID;
		if (show) {
			Util.setCookie(cookieName, 'on');
			FilterPanel.expandListCookies.push(cookieName);
		} else {
			Util.delCookie(cookieName);
		}
	},
	/**
	 * we already have all items for the list on the page: simply change visibility
	 */
	doToggle: function(id) {
		var list = document.getElementById(id).getElementsByTagName('li'), li, hide;
		for (var i = 0; i < list.length; ++i)
		{
			li = list.item(i);
			if ('true' == li.getAttribute('x-toggle'))
			{
				show = (li.style.display == 'none');
				if (show)
				{
					li.style.display = 'block';
				} else {
					li.style.display = 'none';
				}
			}
		}
		var trigger = document.getElementById(id + '-trigger');
		if (show) {
			trigger.innerHTML = FilterPanel.STR_COLLAPSE;
		} else {
			trigger.innerHTML = FilterPanel.STR_EXPAND;
		}
		
		return show;
	},
	/**
	 * Use AJAX to expand filter panel
	 *
	 * this method implements a sort of "Template-method" pattern, see FilterPanel.finalizeListFilterLink
	 */
	expand: function(id, column, column_count, selected) {
		var url = "/expand-filter.php?column=" + column + 
			"&category_id=" + FilterPanel.CATEGORY_ID + 
			"&type=" + column_count + 
			"&from=" + encodeURIComponent(location.toString());
		new jdom.request(url).
			setSuccessHandler(function(response){
				var c = document.getElementById(id);// c => container
				var liCount = c.getElementsByTagName('li').length;
				var li, a, n = liCount;
				for (var i = 0; i < response.json.length; ++i) {
					li = document.createElement('li');
					li.setAttribute('x-toggle', 'true');
					li.className = (n % 2) ? "right" : "left";
					n++;
					c.appendChild(li);
					a = document.createElement('a');
					a.href = response.json[i].href;
					a.innerHTML = response.json[i].text;
					
					// on the category base page, the list-filters links behaviour is different from that
					// in the usual list filter.
					// in order to make FilterPanel extensible,
					// the finalizeListFilterLink() is called here.
					FilterPanel.finalizeListFilterItem(li, a, response.json[i]);
					
					li.appendChild(a);
				}
				document.getElementById(id + '-trigger').innerHTML = FilterPanel.STR_COLLAPSE;
				FilterPanel.alreadyToggled[column] = true;
			}).
			setFailureHandler(function(){}).
			go();
	},
	rangeFocusIn: function(input, text, event) {
		if (isNaN(parseFloat(input.value)))
		{
			input.value = text;
		}
		if (event == 'in')
		{
			input.select();
		}
	},
	resetExpandListCookies: function() {
		for (var i = 0; i < FilterPanel.expandListCookies.length; ++i)
		{
			Util.delCookie(FilterPanel.expandListCookies[i]);
		}
		return true;
	},
	finalizeListFilterItem: function(li, a, data) {
		// do nothing here, override this method elsewhere
	}
}