function toggleFilter(o)
{
	var filter = $("#product-filter");
	var btn = $(o);
	if (filter.css('display') == 'block')
	{
		$('#btn-filterToggle .btn_block_center').html(tradClicDisp);
		$('#btn-filterReset').attr('defWidth', $('#btn-filterReset').css('width'));
		$('#btn-filter').attr('defWidth', $('#btn-filter').css('width'));
		$('#btn-filterReset, #btn-filter').toggle('slow');
		$('#btn-filterToggle .btn_block_center').animate({
			  width:604
					}, 'slow', 'swing');
	}
	else
	{
		$('#btn-filterToggle .btn_block_center').html(tradClicHide);
		$('#btn-filterToggle .btn_block_center').animate({
				  width: 250
				}, "medium", "swing", function() {

				});
		$('#btn-filterReset, #btn-filter').toggle('slow');
	}
	filter.slideToggle();
}

function setSort(obj, event, s)
{
	currentPage = 1;
	sort = s;
	$('.sort-product').removeClass('current');
	obj.parent('.sort-product').eq(0).addClass('current');
	submitFilters(event);
}

function setPage(sens, event)
{
	currentPage = sens + currentPage;
	submitFilters(event);
}

function procAjax(type, data)
{
	datas = '';
	pagination = '';
	if (AjReload)
	{
		AjLock = false;
		setTimeout('submitFilters(false); AjReload = false;', 50);
		return false;
	}
	if (type == 1)
	{
		datas = data.data;
		pagination = data.pagination;
	}
	else
	{
		datas = 'Ajax error : '+data;
		pagination = '';
	}
	loader(0);
	AjLock = false;
	return true;
}

function toto()
{
	return true;
}

function submitFilters(event)
{
	setColors();
	if (event)
	{
		if (event.stopPropagation)
			event.stopPropagation();
		event.cancelBubble = true;
	}

	if (AjLock)
	{
		AjReload = true;
		return false;
	}
	AjLock = true;
	if (!AjReload)
		loader(1);

	var formData = $(document.formFilter).formSerialize();
	var urlajax = 'ajax-product-list.php';
	if (issearch)
	{
		formData += '&p='+parseInt(currentPage);
		urlajax = 'ajax-search.php';
	}
	else
		formData += '&sortby='+parseInt(sort)+
			'&p='+parseInt(currentPage)+
			'&id_category='+parseInt(category);
	var tab = location.href.split('#');
	location.href = tab[0] + '#' + parseInt(currentPage);
	$.ajax({url: base_url + urlajax,
			dataType:'json',
			type: 'post',
			data: formData,
			success: function (ret)
			{
				if (ret.error == '')
					procAjax(1, ret);
				else
					procAjax(0, ret.error);
			},
			error: function (ret, ajaxerr)
			{
				procAjax(0, '('+ret.status+') '+ajaxerr+'.');
			}
		});
	return false;
}

function loader(action)
{
	if (action == 0)
	{
		$('#product-list-loader').stop(true, true);
		$('#products-list').stop(true, true);
		$('#products-list').html(datas);
		$('.pagination-ajax div').html(pagination);
		$('#product-list-loader').fadeTo("fast", 0, function () {
			});
		$('#products-list').fadeTo("fast", 1, function () {
				$('#product-list-loader').hide();
			});
		setfiltertags();
	}
	else
	{
		$('#product-list-loader').show();
		$('#product-list-loader').fadeTo("slow", 1);
		$('#products-list').fadeTo("slow", 0);
	}
	return true;
}

function setfiltertags()
{
	$('#product-dispfilter .filtertag').each(function () {
			var html = '';
			var name = $(this).attr('id');
			name = name.substring(7);
			var div = $('#product-filter [name='+name+']').eq(0);
			if (div.size() <= 0 && $('#product-filter [rel=filtag-'+name+']').size())
			{
				var ok = false;
				if (!$('#filtag-'+name+' .filtertag-left').attr('defValue'))
					$('#filtag-'+name+' .filtertag-left').attr('defValue', 'on');
				$('#product-filter [rel=filtag-'+name+']').each(function (){
						if ($(this).attr('checked'))
							ok = true;
					});
				if (ok)
					html = ' ';
			}
			else if (div.is('input[type=checkbox]'))
			{
				if (!$('#filtag-'+name+' .filtertag-left').attr('defValue'))
					$('#filtag-'+name+' .filtertag-left').attr('defValue', div.attr('checked'));
				if (div.attr('checked'))
					html = $('#product-filter label[for='+div.attr('id')+']').html();
			}
			else if (div.is('input[type=radio]'))
			{
				div = $('#product-filter input[name='+name+'][checked=1]');
				if (!$('#filtag-'+name+' .filtertag-left').attr('defValue'))
					$('#filtag-'+name+' .filtertag-left').attr('defValue', div.attr('id'));
				if (name == 'filterFree')
				{
					if (div.attr('value') != '-1')
						html = $('#product-filter label[for='+div.attr('id')+']').html();
				}
				else if (div.attr('value') != '' && div.attr('value') != '0')
					html = $('#product-filter label[for='+div.attr('id')+']').html();
			}
			else if (div.is('select'))
			{
				if (!$('#filtag-'+name+' .filtertag-left').attr('defValue'))
					$('#filtag-'+name+' .filtertag-left').attr('defValue', div.val());
				if (div.val() != '' && div.val() != '0')
					html = $('#product-filter select[name='+name+'] option[value='+div.val()+']').html();
			}
			else if (div.is('input'))
			{
				if (!$('#filtag-'+name+' .filtertag-left').attr('defValue'))
					$('#filtag-'+name+' .filtertag-left').attr('defValue', div.val());
				if (div.val() != '')
					html = ' ';
			}
			if (html != '' && html)
			{
				if (typeof(delfilter) != 'undefined')
					$('#filtag-'+name+' .filtertag-left').unbind('click', delfilter);
				$('#filtag-'+name+' .filtertag-left').click(function delfilter() {
						var name = $(this).parent('.filtertag').attr('id');
						name = name.substring(7);
						if (typeof($(this).attr('defValue')) != "undefined")
						{
							var div = $('#product-filter [name='+name+']').eq(0);
							if (div.size() <= 0 && $('#product-filter [rel=filtag-'+name+']').size())
							{
								$('#product-filter [rel=filtag-'+name+']').attr('checked', false);
							}
							else if (div.is('input[type=checkbox]'))
							{
								if ($(this).attr('defValue'))
									div.removeAttr('checked');
								else
									div.attr('checked', false);
							}
							else if (div.is('input[type=radio]'))
							{
								$('#product-filter [name='+name+']').attr('checked', false);
								$('#product-filter #'+$(this).attr('defValue')).attr('checked', true);
							}
							else if (div.is('select'))
							{
								div.val($(this).attr('defValue'));
							}
							else if (div.is('input'))
							{
								div.val($(this).attr('defValue'));
							}
							submitFilters(false);
						}
						return false;
					});
				$('#filtag-'+name+' .filtertag-center span').html(html);
				$('#filtag-'+name).css('display', 'inline');
			}
			else
				$('#filtag-'+name).css('display', 'none');
		}
		);
}

function toggleColor(obj)
{
	obj.toggleClass('color-selected', 'slow');
	return false;
}

function setColors()
{
	$('.filter-colors').each(function () {
			var input = $(this).find('input');
			var html = '';
			input.attr('value', '');
			$(this).find(' a.filter-color.color-selected img').each(
				function() {
					html += $(this).attr('alt')+'-';
				});
			input.attr('value', html);
		});
}