$(document).ready(function(){
	$('.setPerPage').livequery('change',function(){
		$(this).parent().submit();
	});
    
	if (showcomparateur) {
		$('.cmpbox').livequery('click', function(){
		
			var att = $(this).attr('id').split('_');
			var cid = att.pop();
			var prdid = att.pop();
			var curr = $(this);
			
			$('.cmpbox').attr('disabled', true);
			
			if ($(this).attr('checked') == false) {
				$.ajax({
					'url': '/catalogue/removefromcompare',
					'data': 'prd=' + prdid + '&cid=' + cid,
					timeout: 4000,
					'success': function(ret){
						if (ret == 1) {
							Growl.show('Article retir&eacute; du comparateur.');
						}
						else 
							if (ret == 2) {
								Growl.show('Plus aucun produit dans votre comparateur');
							}
						$('.cmpbox').attr('disabled', false);
					}
				});
			}
			else {
				$.ajax({
					'url': '/catalogue/addtocompare',
					'data': 'prd=' + prdid + '&cid=' + cid,
					timeout: 4000,
					'success': function(ret){
						if (ret == 1) {
							Growl.show('Article ajout&eacute; au comparateur.');
							$('.cmpbox').attr('disabled', false);
						}
						else 
							if (ret == 2) {
								curr.attr('checked', false);
								Growl.show('Ce produit est d&eacute;ja dans votre comparateur');
								$('.cmpbox').attr('disabled', false);
							}
							else 
								if (ret == 3) {
									curr.attr('checked', false);
									Growl.show('Vous avez d&eacute;ja 4 produits dans votre comparateur.');
									$('.cmpbox').attr('disabled', false);
								}
						
					}
				});
			}
			
			
			
		});
	}
    
if (showselecteur) {
	$('#sliderprix .label-1').html(plagePrix.min);
	$('#sliderprix .label-2').html(plagePrix.max);
	
	$('#slidertaille .label-1').html(plageTailles.min);
	$('#slidertaille .label-2').html(plageTailles.max);
	
	$('#sliderprix').slider({
		steps: (plagePrix.max - plagePrix.min) / 10,
		min: plagePrix.min,
		max: plagePrix.max,
		range: true,
		change: function(e, ui){
			var minValue = $('#sliderprix').slider('value', 0);
			var maxValue = $('#sliderprix').slider('value', 1);
			
			$('#priceMin').val(minValue);
			$('#priceMax').val(maxValue);
			if (e != null) {
				$('#filtersForm').submit();
			}
		},
		slide: function(e, ui){
			var minValue = $('#sliderprix').slider('value', 0);
			var maxValue = $('#sliderprix').slider('value', 1);
			
			$('#sliderprix .label-1').html(minValue);
			$('#sliderprix .label-2').html(maxValue);
			
		},
		handles: [{
			start: 0,
			min: plagePrix.min,
			max: plagePrix.max - 10
		}, {
			start: plagePrix.max,
			min: plagePrix.min + 10,
			max: plagePrix.max
		}]
	});
	
	$('#slidertaille').slider({
		stepping: plageTailles.stepping,
		min: plageTailles.min,
		max: plageTailles.max,
		range: true,
		change: function(e, ui){
			var minValue = $('#slidertaille').slider('value', 0);
			var maxValue = $('#slidertaille').slider('value', 1);
			$('#tailleMin').val(minValue);
			$('#tailleMax').val(maxValue);
			if (e != null) {
				$('#filtersForm').submit();
			}
		},
		slide: function(e, ui){
			var minValue = $('#slidertaille').slider('value', 0);
			var maxValue = $('#slidertaille').slider('value', 1);
			
			$('#slidertaille .label-1').html(minValue);
			$('#slidertaille .label-2').html(maxValue);
			
		},
		handles: [{
			start: plageTailles.min,
			min: plageTailles.min,
			max: plageTailles.max
		}, {
			start: plageTailles.max,
			min: plageTailles.min,
			max: plageTailles.max
		}]
	});
	
	
	$('#resetBt').click(function(){
		$('#filtersForm :input').each(function(){
			$(this).attr('checked', false);
		});
		$('#slidertaille').slider('moveTo', plageTailles.min, 0);
		$('#slidertaille').slider('moveTo', plageTailles.max, 1);
		$('#sliderprix').slider('moveTo', plagePrix.min, 0);
		$('#sliderprix').slider('moveTo', plagePrix.max, 1);
		$('#filtersForm').submit();
	});
	
	if (currentcriteres && typeof currentcriteres.taille != 'undefined'){
		$('#slidertaille').slider('moveTo', currentcriteres.taille.min, 0);
		$('#slidertaille').slider('moveTo', currentcriteres.taille.max, 1);
	}
	
	if (currentcriteres && typeof currentcriteres.prix != 'undefined'){
		$('#sliderprix').slider('moveTo', currentcriteres.prix.min, 0);
		$('#sliderprix').slider('moveTo', currentcriteres.prix.max, 1);
	}
	
	$('#showSelBt').click(function(){
		$('#selcenter').slideToggle("slow", function(){
			if ($(this).css('display') == 'none') {
				$('#showSelBt').html('Afficher');
			}
			else {
			
				$('#showSelBt').html('Masquer');
			}
		});
	});
	
	$('#filtersForm').submit(function(){
		$('#filterResults').hide();
		$('#spinner').show();
		
		var filters = [];
		$(':input', this).each(function(){
			if (this.type == 'checkbox') {
				if ($(this).attr('checked')) {
					filters.push(this.name + '=' + escape(this.value));
				}
			}
			else 
				if (this.type == 'hidden' && this.value) {
					filters.push(this.name + '=' + escape(this.value));
				}
		});
		
		if (typeof orderby != 'undefined'){
			filters.push('orderby='+orderby);
			//console.debug('adding order');
		}
		
		//if (!filters.length) return false;
		
		$('#familleContent').fadeOut("slow", function(){
			$(this).children().remove();
			
			var filterurl = stype == 'pack' ? '/catalogue/dopackfilters/id/' : '/catalogue/dofilters/id/';
			
			$.ajax({
				data: filters.join('&'),
				dataType: 'html',
				type: 'post',
				url: filterurl + currentCatId,
				timeout: 10000,
				error: function(){
					//console.log('Error submit...');
					$('#familleContent').html('<div>Erreur</div>').fadeIn('slow');
					$('#spinner').hide();
					$('#filterResults').show();
				},
				success: function(r){
					$('#familleContent').html(r).fadeIn('slow');
					$('#innerResult').html(totalItems.toString());
					$('#filterResults').show();
					$('#spinner').hide();
					$('#filterResults').show();
				}
			});
		});
		
		return false;
	});
	
	$('#filtersForm :input').click(function(){
		$('#filtersForm').submit();
	});
	
	if (showcomparateur) $('.ttip').tooltip({ showURL: false, extraClass: 'myttip' });
}			
});
