//let's make the price-bar work
//---------------------------------------------//

$(function(){
	//let's make the price-bar work
	//first catch the click events
	$('#product-search-price-categorie').bind('change',autoSelect);
	$('#product-search-price-categorie .ui-checkbox').bind('click',autoSelect);
	colorbar();

});


	function autoSelect(event) {
	 	var checkBoxes = findCheckboxes('#product-search-price-categorie', true);

		var checkBoxes_selected_length = checkBoxes[1].length;
		
		var checkBoxes_clicked  = $(event.target).parents('div').children("input[type='checkbox']")[0];

		
		if(checkBoxes_selected_length > 1) {
			var select = false;
			reached_last = false;
			$(checkBoxes[0]).each(function(){
				if(checkBoxes[1][0] == this) {
					select = true;
				}

				if(select) {
					if(checkBoxes_clicked != this) {
					 $(this).checkBox('changeCheckStatus',true);
					}else if(!$(this).attr('checked')){
						select = false;
					}
				}else{
					$(this).checkBox('changeCheckStatus',false);
				}
			
				if(checkBoxes[1][checkBoxes_selected_length-1] == this) {
					select = false;
				}
					
			});
		}
		if(checkBoxes_selected_length > 0) {
			colorbar();
		}else{
			clearbar();
		}
		submitSearchForm();
	}
	
	function colorbar() {
		var checkBoxes = findCheckboxes('#product-search-price-categorie', false);
		var checkBoxes_selected_length = checkBoxes[1].length;
		var first = checkBoxes[1][0];
		var last = checkBoxes[1][checkBoxes_selected_length-1];

		if(checkBoxes_selected_length > 0) {
			var offset = $('#product-search-price-bar').position().left;
			var image_x1 = $(first).parents('.product-search-price-categorie-option').position().left;
			var image_x2 = $(last).parents('.product-search-price-categorie-option').position().left;
			var lastOffset = Math.ceil(Number($(last).parents('.product-search-price-categorie-option').width())/2);
			var firstOffset = Math.ceil(Number($(first).parents('.product-search-price-categorie-option').width())/2);
			
			var image_width = image_x2 - image_x1 + lastOffset + firstOffset + 7;
	
			$('#product-search-price-bar-coloring img').width(image_width);
			$('#product-search-price-bar-coloring img').css('marginLeft',image_x1-offset-firstOffset);
		}
	}
	
	function clearbar() {
		$('#product-search-price-bar-coloring img').width(0);
		$('#product-search-price-categorie').find("input[type='checkbox']").each(function() {
			$(this).checkBox('changeCheckStatus',false);
		});
	}
	
	
	function findCheckboxes(selector, addHidden) {
		var checkBoxes_all = [];
		var checkBoxes_selected = [];
		var checkBoxes = new Array(2);
		
		if(addHidden) {
			$(selector).find("input[type='checkbox']").each(function() {
				checkBoxes_all.push(this);
				if($(this).attr('checked')) {
				checkBoxes_selected.push(this); 
				}
			});
		}else{
			$(selector+' div:not(.option-hidden)').find("input[type='checkbox']").each(function() {
				checkBoxes_all.push(this); 
				if($(this).attr('checked')) {
				checkBoxes_selected.push(this);
				}
			});
	
		}
		
		
		checkBoxes[0] = checkBoxes_all;
		checkBoxes[1] = checkBoxes_selected;		//log(checkBoxes);
		return checkBoxes;
	}
	
//---------------------------------------------//
