window.reloadTimeoutId = 0;
window.reloadTimeout = 1000;

function reloadWithFilters(newhref) {
	window.location.href = newhref;
}

function reloadWithAjax() {
//	alert (window.pageCurrent);
	reloadWithAjaxMain(window.slider_min, window.slider_max, window.currentCurrency, window.currentPO, window.currentPS, window.currentPP,window.pageCurrent,window.location.href);
	return false;
}

function reloadWithAjaxPrice() {
//	window.pageCurrent = 1;
//	reloadWithAjaxMain(window.slider_min, window.slider_max, window.currentCurrency, window.currentPO, window.currentPS, window.currentPP,window.pageCurrent,window.location.href);
//	var maxpage = Math.ceil(window.itemCounter/window.currentPP);
//	$('.page_filter_list li').remove();
//	for(var i=1;i<=maxpage;i++) { 
//		var pmin = (i-1)*window.currentPP+1;
//		var pmax = i*window.currentPP;
//		if (pmax > window.itemCounter) pmax = window.itemCounter;
//		$('.page_filter_list').append('<li><a>' + pmin + '-' + pmax + '</a></li>');
//	}

	var newhref = 'p:' + window.currentCurrency + ':' + window.slider_min + ':' + window.slider_max + '/';
	var href = window.location.href;
	
	href = href.replace(/p:([0-9]+):([0-9]+):([0-9]+)\//gi, '');	
	href = href.replace(/pg([0-9]+)\//gi, '');	
	href=href+newhref; 			
	
	reloadWithFilters(href);

	return false; 
}

function reloadWithAjaxMain(min,max,cid,cpo,cps,cpp,cpc,chref) {
	$('.product_list').fadeTo(200,0.05);
//	alert (min + '-' + max + '-' +  cid + '-' + cpo + '-' + cps + '-' + cpp + '-' + cpc + '-' + chref);
	$('.product_list').load('/shop/~list/',{from:min,to:max,cur:cid,po:cpo,ps:cps,pp:cpp,pc:cpc,href:chref}, function(){
				$('.product_list').fadeTo(200,1);
				
				var maxpage = Math.ceil(window.itemCounter/window.currentPP);
				if (cpc == 1) { $('.page_filter_button_left').fadeTo(200,0.1) } else { $('.page_filter_button_left').fadeTo(200,1); }
				if (cpc == maxpage) { $('.page_filter_button_right').fadeTo(200,0.1) } else { $('.page_filter_button_right').fadeTo(200,1); }
				var htmlStrMin = (window.pageCurrent-1) * window.currentPP + 1;
				var htmlStrMax = window.pageCurrent * window.currentPP; 	
				if (htmlStrMax > window.itemCounter) htmlStrMax = window.itemCounter;
				$('a.page_filter_text').text(htmlStrMin + '-' + htmlStrMax + ' из ' + window.itemCounter);					
				
				});	
		
	return false;
}


function sliderChange(min, max) {
	window.slider_min = min;
	window.slider_max = max;
	
	if (window.reloadTimeoutId)
		clearTimeout(window.reloadTimeoutId);
		window.reloadTimeoutId = setTimeout('reloadWithAjaxPrice()', window.reloadTimeout);
}

$(document).ready(function(){
	$('.checkbox_filter .toggle').click(function(){
		$(this).blur();
		var link = $(this);
		if ($(this).parent().find('.hidden:hidden').length>0) {
			$(this).addClass('close');
			$(this).parent().find('.hidden').slideDown(function(){
				link.text('скрыть');
				window.reHight();
			});
		}
		else {
			$(this).removeClass('close');
			$(this).parent().find('.hidden').slideUp(function(){
				link.text('показать все');
				window.reHight();
			});
		}
		return false;
	});

	$('.char_filter').change(function(){			
		var pId  = $(this).attr('char_id');
		var vId  = $(this).attr('value');
		var newhref = pId+vId + '/';
		var href = window.location.href;
		if($(this).attr('checked')) { 	
			href = href.replace(new RegExp(newhref,'gi'), '');	
			href=href+newhref; 			
		} else {		
			href = href.replace(new RegExp(newhref,'gi'), '');						
		}
//		href = href.replace(/p:([0-9]+):([0-9]+):([0-9]+)\//gi, '');	
		href = href.replace(/pg([0-9]+)\//gi, '');
		reloadWithFilters(href);
	});
});
