$(document).ready(function(){
	
	var linked = $('#productsfilter ul li a');
	
	$.historyInit(getProducts, come_from);
	
	linked.bind('click', function(e){
		  
		e.preventDefault();
		var parent = $(this).parents('ul');		
		
		if (parent.hasClass('size'))
		{
			var	sizes = $('#productsfilter ul.size');
			var rel = $(this).attr('rel');
			
			sizes.find('li a.selected').removeClass('selected');
			
			$(this).fadeOut('fast', function(){			
				$(this).addClass('selected').fadeIn('fast');
			});
				
			id_attribute = $(this).attr('rel');
		}
		else if(parent.hasClass('price'))
		{	  
			$(this).parents('ul').find('li a').removeClass('selected');
			$(this).fadeOut('fast', function(){	
				$(this).addClass('selected').fadeIn('fast');
			});	
			var filters = $(this).attr('rel').split('_');
			if (typeof(filters) != 'undefined' && typeof(filters[0]) != 'undefined' && typeof(filters[1]) != 'undefined')
			{
				filterLeft = parseFloat(filters[0]); 
				filterRight = parseFloat(filters[1]); 			
			}			
		}
		else if(parent.hasClass('manufacturer'))
		{
			var img = $(this).find('img');
			img.fadeOut('fast', function(){	
			
				$(this).parents('ul').find('li.s-brands-selected').removeClass('sprite s-brands-selected').find('a').removeClass('selected').find('img').each(function(){
					$(this).attr('src', $(this).attr('src').replace(img_manu_theme_dir, img_manu_dir));
				})
				$(this).parent('a').addClass('selected').parent('li').addClass('sprite s-brands-selected');				
			    $(this).attr('src', img.attr('src').replace(img_manu_dir, img_manu_theme_dir)).fadeIn('fast');
			});	
			id_manufacturer = $(this).attr('rel');	  
		}
		else if(parent.hasClass('category'))
		{
			$(this).parents('ul').find('li a').removeClass('selected');
			$(this).fadeOut('fast', function(){	
				$(this).addClass('selected').fadeIn('fast');
			});	
			id_category = $(this).attr('rel');
		}
		p = 1; // reset pagination
		$('#center_column p.warning').remove();		
		

		var params  = getParams();
		$.historyLoad(params);
				
		return false;		
	});
	rebind();
});

function getParams()
{
	var params = 'content_only=1';
		
	params += '&id_category=' + parseInt(id_category);
	params += '&id_manufacturer=' + parseInt(id_manufacturer);
	if (typeof(id_attribute) != 'undefined')
	{
		if (typeof(id_attribute) == 'string' )
			id_attribute = id_attribute.split('_');
		
		params += '&id_attribute=' + encodeURIComponent(id_attribute);
	}
	if (typeof(filterLeft) != 'undefined')
	params += '&filterLeft=' + parseInt(filterLeft);
	if (typeof(filterRight) != 'undefined')
	params += '&filterRight=' + parseInt(filterRight);
	if (typeof(orderby) != 'undefined')
	params += '&orderby=' + orderby;
	if (typeof(orderway) != 'undefined')
	params += '&orderway=' + orderway;
	if (typeof(n) != 'undefined')
	params += '&n=' + n;
	if (typeof(p) != 'undefined')
	params += '&p=' + p;
	return params;
}

function getProducts(post_url)
{
		var url = baseDir + 'modules/productsfilter/products-filter.php?';
		var params  = getParams();			
		
		if($.browser.msie) { 
	//			post_url = encodeURIComponent(post_url);
		}
		
		if (post_url)
		{
			params = post_url;
		}

		$.ajax({
			type: "GET",
			url: url,
			data: params,
			cache: true,
			async: false,
			success: function(html){
				$('#products_list').fadeOut('fast', function(){
					/*
					regex = '<p class="warning">.*';
					regex = new RegExp(regex);
					result = regex.exec(data);
					//if (result == null)
					//	$(this).html(data);
				//	else
						//$(this).before(data);
					*/
					$(this).html(html);
					$(this).fadeIn('fast', function(){
						rebind();
						/*
						if (scroll == true)
							$.scrollTo('#category');*/
					});
				});
			}
		});
}
function rebind()
{
	$('#pagination ul li a').each(function(){
		var url = $(this).attr('href');
						
		$(this).bind('click', function(f){
			f.preventDefault();
			
			regex = 'p=(.*)';
			regex = new RegExp(regex);
			result = regex.exec(url);
			
			if (result == null)
				p = 1;
			else if (result[1] != 'undefined' && !isNaN(result[1]))
				p = parseInt(result[1]);
			else
				p = 1;
						
			var params  = getParams();
			$.historyLoad(params);
			window.scrollTo(0,0);								
		});
	});
	
	$('#selectPrductSort').attr('onchange', '');			
	$('#selectPrductSort').unbind('change').change(function(){
		
		url = $(this).val();
		regex = 'orderby=(.*)&';
		regex = new RegExp(regex);
		result = regex.exec(url);

		if (result != null && result[1] != 'undefined')
			orderby = result[1];
			
		regex = 'orderway=(.*)';
		regex = new RegExp(regex);
		result = regex.exec(url);

		if (result != null && result[1] != 'undefined')
			orderway = result[1];				
	
		var params  = getParams();
		$.historyLoad(params);
		window.scrollTo(0,0);
		return false;	
	});
	
	$('#delete_filters').click(function(){
	
		$('#productsfilter ul li a.selected').removeClass('selected');
		
		if (typeof(from) != 'undefined' && from == 'category')
		{
			$('#productsfilter ul.manufacturer').find('li.s-brands-selected a img').each(function() {
				$(this).attr('src', $(this).attr('src').replace(img_manu_theme_dir, img_manu_dir));
			});
			$('#manufacturers_block_left ul.manufacturer').find('li.s-brands-selected').removeClass('sprite s-brands-selected');
		}		
		$('#productsfilter ul.size').each(function() {
			$(this).find('li:first a').addClass('selected');
		});
		
		if (typeof(from) != 'undefined' && from == 'category')
			id_manufacturer = 0;
		if (typeof(from) != 'undefined' && from == 'manufacturer')			
			id_category = 0;
			
		id_attribute = 0;
		filterLeft = 0;
		filterRight = 0;
		
		var params  = getParams();
		$.historyLoad(params);
	});
}
jQuery.extend({historyCurrentHash:undefined,historyCallback:undefined,historyIframeSrc:undefined,historyNeedIframe:jQuery.browser.msie&&(jQuery.browser.version<8||document.documentMode<8),historyInit:function(e,d){jQuery.historyCallback=e;if(d){jQuery.historyIframeSrc=d}var c=location.hash.replace(/\?.*$/,"");jQuery.historyCurrentHash=c;if(jQuery.historyNeedIframe){if(jQuery.historyCurrentHash==""){jQuery.historyCurrentHash="#"}jQuery("body").prepend('<iframe id="jQuery_history" style="display: none;" src="javascript:false;"></iframe>');var a=jQuery("#jQuery_history")[0];var b=a.contentWindow.document;b.open();b.close();b.location.hash=c}else{if(jQuery.browser.safari){jQuery.historyBackStack=[];jQuery.historyBackStack.length=history.length;jQuery.historyForwardStack=[];jQuery.lastHistoryLength=history.length;jQuery.isFirst=true}}if(c){jQuery.historyCallback(c.replace(/^#/,""))}setInterval(jQuery.historyCheck,100)},historyAddHistory:function(a){jQuery.historyBackStack.push(a);jQuery.historyForwardStack.length=0;this.isFirst=true},historyCheck:function(){if(jQuery.historyNeedIframe){var a=jQuery("#jQuery_history")[0];var d=a.contentDocument||a.contentWindow.document;var f=d.location.hash.replace(/\?.*$/,"");if(f!=jQuery.historyCurrentHash){location.hash=f;jQuery.historyCurrentHash=f;jQuery.historyCallback(f.replace(/^#/,""))}}else{if(jQuery.browser.safari){if(jQuery.lastHistoryLength==history.length&&jQuery.historyBackStack.length>jQuery.lastHistoryLength){jQuery.historyBackStack.shift()}if(!jQuery.dontCheck){var b=history.length-jQuery.historyBackStack.length;jQuery.lastHistoryLength=history.length;if(b){jQuery.isFirst=false;if(b<0){for(var c=0;c<Math.abs(b);c++){jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop())}}else{for(var c=0;c<b;c++){jQuery.historyBackStack.push(jQuery.historyForwardStack.shift())}}var e=jQuery.historyBackStack[jQuery.historyBackStack.length-1];if(e!=undefined){jQuery.historyCurrentHash=location.hash.replace(/\?.*$/,"");jQuery.historyCallback(e)}}else{if(jQuery.historyBackStack[jQuery.historyBackStack.length-1]==undefined&&!jQuery.isFirst){if(location.hash){var f=location.hash;jQuery.historyCallback(location.hash.replace(/^#/,""))}else{var f="";jQuery.historyCallback("")}jQuery.isFirst=true}}}}else{var f=location.hash.replace(/\?.*$/,"");if(f!=jQuery.historyCurrentHash){jQuery.historyCurrentHash=f;jQuery.historyCallback(f.replace(/^#/,""))}}}},historyLoad:function(d){var e;d=decodeURIComponent(d.replace(/\?.*$/,""));if(jQuery.browser.safari){e=d}else{e="#"+d;location.hash=e}jQuery.historyCurrentHash=e;if(jQuery.historyNeedIframe){var a=jQuery("#jQuery_history")[0];var c=a.contentWindow.document;c.open();c.close();c.location.hash=e;jQuery.lastHistoryLength=history.length;jQuery.historyCallback(d)}else{if(jQuery.browser.safari){jQuery.dontCheck=true;this.historyAddHistory(d);var b=function(){jQuery.dontCheck=false};window.setTimeout(b,200);jQuery.historyCallback(d);location.hash=e}else{jQuery.historyCallback(d)}}}});
