function FilterGoods(filter, val){
	var f_input = $('.g_filter[name="filter[' + filter + ']"]');
	if (f_input.length == 0) {
		f_input = jQuery('<input>', {
			type: 'hidden',
			name: 'filter[' + filter + ']'
		});
		$(f_input).addClass('g_filter');
		$(f_input).appendTo('#goods_filt_sort');
	}
	$(f_input).val(val);

	$('#current_page_num').val(1);
	$('#goods_filt_sort').submit();
}

function SortGoods(sort){
	if (sort == 'dd') {
		switch ($('#dd_sort').val()) {
			case 'name_a':
				$('.g_sort').val('name');
				$('#in_sort_dir').val('asc');
				break;
			case 'name_d':
				$('.g_sort').val('name');
				$('#in_sort_dir').val('desc');
				break;
			case 'price_a':
				$('.g_sort').val('price');
				$('#in_sort_dir').val('asc');
				break;
			case 'price_d':
				$('.g_sort').val('price');
				$('#in_sort_dir').val('desc');
				break;
			case 'rating':
				$('.g_sort').val('rating');
				$('#in_sort_dir').val('desc');
				break;
			case 'promo':
				$('.g_sort').val('promo');
				$('#in_sort_dir').val('desc');
				break;
			case 'novelty':
				$('.g_sort').val('default');
				$('#in_sort_dir').val('desc');
				break;
			case 'bestsellers':
				$('.g_sort').val('bestsellers');
				$('#in_sort_dir').val('desc');
				break;
			case 'author_a':
				$('.g_sort').val('author');
				$('#in_sort_dir').val('asc');
				break;
			case 'author_d':
				$('.g_sort').val('author');
				$('#in_sort_dir').val('desc');
				break;
			default:
				return;
		}
	} else {
		$('.g_sort').val(sort);
		var dir = $('#in_sort_dir').val();
		if(dir == 'asc') $('#in_sort_dir').val('desc');
		else $('#in_sort_dir').val('asc');
	}
	$('#goods_filt_sort').submit();
}

function SetGoodsType() {
	$('#filter_type').val($('#dd_g_t').val());
	$('#goods_filt_sort').submit();
}

function SetGoodsPerPage() {
	$('#g_pp').val($('#dd_g_pp').val());
	$('#goods_filt_sort').submit();
}

function CatalogPage(page){
	$('.g_page').val(page);
	$('#goods_filt_sort').submit();
}

function SetGroup(group){
	$('.g_page').val(1);
	$('.g_group').val(group);
	$('#goods_filt_sort').submit();
}
function zoomImg(obj, width, height, step, check) // картинка, желаемая ширина, желаемая высота, шаг, проверка (не указывается, нужна для корректной работы)
{
	var IE = navigator.userAgent.toString().match(/MSIE\s*([\.0-9]+)/);
	if (IE != null && IE[1] < 8) return;
	if(isNaN(check)) check = width>0 ? obj.clientWidth : obj.clientHeight;
	var flag = false;
	var sibling = $(obj).parent().siblings('.sale_sticker')[0];
	if(width > 0) { // меняем картику по ширине
		if((step > 0 && obj.clientWidth < width) || (step < 0 && obj.clientWidth > width)) {
			if(step > 0 && check != obj.clientWidth) return;
			check = obj.width = obj.clientWidth + step;
			flag = true;
		}
		else if(step > 0)
		check = obj.width = width;
	}
	else if(height > 0) { // меняем картику по высоте
		if((step > 0 && obj.clientHeight < height) || (step < 0 && obj.clientHeight > height)) {
			if(step > 0 && check != obj.clientHeight) return;
			check = obj.height = obj.clientHeight + step;
			flag = true;
		}
		else if(step > 0)
		check = obj.height = height;
	}
	if(flag) { // меняем положение картинки
		obj.style.zIndex = step > 0 ? '2' : '0';
		obj.style.top = obj.offsetTop - step/2 + 'px';
		obj.style.left = obj.offsetLeft - step/2 + 'px';
		if (sibling !== undefined) sibling.style.right = parseInt(sibling.style.right, 10) - step/2 + 'px';

		setTimeout(function(){ zoomImg(obj, width, height, step, check) }, 1);
	}
	else if(step < 0) {
		obj.style.top = obj.style.left = 'auto';
		if (sibling !== undefined) sibling.style.right = '2px';
	}
}

function ShowRemains(id) {
	if ($('#remains_' + id).html() == '') {
		var pref = '';
		if (language_code != 'ru') {
			pref = '/' + language_code;
		}
		var hide_tail = 0;
		//If we are in hint - do not show text about world deliwery, etc.
		if ($('#remains_' + id).closest('div.hint').length > 0) hide_tail = 1;
		$.post(pref + "/article/StockRemains", { "art_id" : id, "hide_nearest_delivery" : 1, "hide_tail" : hide_tail},
		function(data){
			if (data.length > 0) {
				$('#remains_' + id).html(data);
				$('#remains_' + id).show(300);
			}
		});
	} else {
		$('#remains_' + id).toggle(300);
	}
}

$(document).ready(function(){
	if (g_IE === undefined)	var g_IE = navigator.userAgent.toString().match(/MSIE\s*([\.0-9]+)/);
	if (g_IE != null && g_IE[1] < 8) $('.sale_sticker').css('right', '-2px');
	$('div.list td > div > a > img').each(function(ind, obj){
		if(obj.height > $(obj).parents('table')[0].clientHeight + 25) {
			$(obj).parents('div').first().css('height', (obj.height) + 'px');
		}
	});
});
