var filters=new Object;	
jQuery(document).ready(function(){

	if(window.location.hash.split('=')[0]=='#current_link'){
		var filterlink=window.location.hash.replace('#current_link=', '');
		
		 var urlparts=filterlink.split('?');
		 var filterparts=urlparts[1].split('&');
		 var baselink=urlparts[0];
		 
		 $.each(filterparts, function(key, value){
			 
			 var foo=value.split('=');
			 filters[foo[0]]=foo[1];
			 
			 if(typeof(foo[1])!=undefined && foo[1]){
				 
				 if(foo[1].indexOf(',')!=-1){
					 $.each(foo[1].split(','), function(k, v){
						 $('.'+foo[0].replace('[', '').replace(']', '')+'_'+v).addClass('active');
						 $('.'+foo[0].replace('[', '').replace(']', '')+'_'+v+' input[type=checkbox]').attr('checked', 'checked');
					 });
				 }
				 else{
					 $('.'+foo[0]+'_'+foo[1]).addClass('active');
					 
					 $('select.'+foo[0]+' option[value*="author='+foo[1]+'"]').attr('selected','selected');
					 $('.'+foo[0]+'_'+foo[1]+' input[type=checkbox]').attr('checked', 'checked');
				 }
				 
			 }
			 
		 });		 
		 var newurl='?';
		
		 $.each(filters, function(key, value){
			 newurl=newurl+key+'='+value+'&';
		 });

		 if(typeof(globalQuantity) == "undefined")
		 getAjaxProducts(baselink+newurl);
		 
	}
	
	 $('.filterSearchModule a').click(function(){
		 
		 var filterlink=$(this).attr('href');

		 $(this).parent().parent().find('li.active').removeClass('active');
		 $(this).parent().parent().find('input[type=checkbox]').attr('checked', '');
		 $(this).parent().addClass('active');
		 $(this).parent().find('input[type=checkbox]').attr('checked', 'checked');
		 		 
		 var urlparts=filterlink.split('?');
		 var filterparts=urlparts[1].split('&');
		 var baselink=urlparts[0];
		 
		 $.each(filterparts, function(key, value){
			 var foo=value.split('=');
			 if(foo[0] && typeof(foo[1])!=undefined)
			 filters[foo[0]]=foo[1];
		 });
		 var newurl='?';

		 $.each(filters, function(key, value){
			 if(key)
			 newurl=newurl+key+'='+value+'&';
		 });

		 getAjaxProducts(baselink+newurl);
		 buildLink(baselink, filters);
		return false; 
		 
	 });
	 

		
	 $('.filterSearchModule select').change(function(){
		 
		 var filterlink=$(this).val();

		 $(this).parent().parent().find('li.active').removeClass('active');
		 $(this).parent().parent().find('input[type=checkbox]').attr('checked', '');
		 $(this).parent().addClass('active');
		 $(this).parent().find('input[type=checkbox]').attr('checked', 'checked');
		 		 
		 var urlparts=filterlink.split('?');
		 var filterparts=urlparts[1].split('&');
		 var baselink=urlparts[0];
		 
		 $.each(filterparts, function(key, value){
			 var foo=value.split('=');
			 if(foo[0] && typeof(foo[1])!=undefined)
			 filters[foo[0]]=foo[1];
		 });
		 var newurl='?';

		 $.each(filters, function(key, value){
			 if(key)
			 newurl=newurl+key+'='+value+'&';
		 });

		 getAjaxProducts(baselink+newurl);
		 buildLink(baselink, filters);
		return false; 
		 
	 });
	 
	 
	 
	 $('.filterSearchModule input[type=checkbox]').click(function(){
		 
		 if($(this).attr('checked')){
		 
			 var filterlink=$(this).parent().find('a').attr('href');

			 $(this).parent().addClass('active');
			 
			 var urlparts=filterlink.split('?');
			 var filterparts=urlparts[1].split('&');
			 var baselink=urlparts[0];
			 
			 
			 $.each(filterparts, function(key, value){
				 var foo=value.split('=');
				 
				 if(foo[0]=='price_from'){
					 if(filters[foo[0]]){
						 if(parseFloat(filters[foo[0]])>parseFloat(foo[1])){
							 filters[foo[0]]=foo[1];	 
						 }
					 }
					 else{
						 filters[foo[0]]=foo[1];
					 }
					 
				 }
				 else if(foo[0]=='price_to'){
					 if(filters[foo[0]]){
						 if(parseFloat(filters[foo[0]])<parseFloat(foo[1])){
							 filters[foo[0]]=foo[1];	 
						 }
					 }
					 else{
						 filters[foo[0]]=foo[1];
					 }				 
				 }
				 else{
					 if(filters[foo[0]])
						 filters[foo[0]]=filters[foo[0]]+','+foo[1];
					 else
						 filters[foo[0]]=foo[1];
				 }
			 });
			 var newurl='?';

			 $.each(filters, function(key, value){
				 newurl=newurl+key+'='+value+'&';
			 });
	
			 getAjaxProducts(baselink+newurl);

		}
		 else{
			 
			 var filterlink=$(this).parent().find('a').attr('href');

			 $(this).parent().removeClass('active');
			 
			 
			 var urlparts=filterlink.split('?');
			 var filterparts=urlparts[1].split('&');
			 var baselink=urlparts[0];
			 
			 $.each(filterparts, function(key, value){
				 var foo=value.split('=');
				 
				 if(foo[0]=='price_from'){
					 filters[foo[0]]=$('.filterSearchModule .pricefilter:checked').first().val().split(';')[0];
				 }
				 else if(foo[0]=='price_to'){
					 filters[foo[0]]=$('.filterSearchModule .pricefilter:checked').last().val().split(';')[1];
				 }
				 else{
					 var values=filters[foo[0]].split(',');
					 var new_values=[];
					 $.each(values, function(k, v){
						 
						 if(v!=foo[1])
						 new_values.push(v);
						 
					 });
					 
					 if(filters[foo[0]])
						 filters[foo[0]]=new_values.join(',');
					 else
						 filters[foo[0]]=new_values.join(',');
				 }
			 });
			 var newurl='?';
			
			 $.each(filters, function(key, value){
				 newurl=newurl+key+'='+value+'&';
			 });
	
			 getAjaxProducts(baselink+newurl);
			 
		 }
		 buildLink(baselink, filters);
		 
	 });
	 

	 function buildLink(baselink, filters){
		 
		 var newurl='?';
		
		 $.each(filters, function(key, value){
			 if(key && value)
			 newurl=newurl+key+'='+value+'&';
		 });
		 
		 window.location.hash="current_link="+baselink+newurl;
		 
	 }
	 
	
 function getAjaxProducts(link){
	 
	 
	 $.ajax({
		 url: link+'ajax=true',
		 dataType: 'html',
		 type: 'GET',
		 success: function(data){
			
			$('#center_column').html(data);
			
			$('.ajax_block_product a').attr('href', function(i,val){
			return val+window.location.hash;	
			});
			
			$('#category_holder .pagination a').attr('href', function(i,val){
			return window.location.hash.replace('#current_link=', '')+'p='+$(this).attr('rel')+'&';	
			});
			
			$('#category_holder .pagination a').click(function(){
				getAjaxProducts($(this).attr('href'));
				return false;
			});
			
			$('.product-per-page select#nb_item').change(function(){
				getAjaxProducts(window.location.hash.replace('#current_link=', '')+'n='+$('.product-per-page select#nb_item').val()+'&');
				return false;
			});
			

			$('#selectPrductSort').change(function(){
				var splitData = $(this).val().split(':');

				if(window.location.hash)
					getAjaxProducts(window.location.hash.replace('#current_link=', '')+ 'orderby=' + splitData[0] + '&orderway=' + splitData[1]+'&');
				else
					$(this).parent().parent().submit();
				
				return false;
			});
			

		 }
	 });
	 
 }
 
});
