var curFormat_id = 'f_0';
var curCategory_id = 'c_0';
var curOrigin_id = 'o_0';

var format_ids = [];
format_ids['f_1'] = 'glass';
format_ids['f_2'] = 'halfBottle';
format_ids['f_3'] = 'bottle';

var cat_ids = [];
cat_ids['c_1'] = 'cat_bubbly';
cat_ids['c_2'] = 'cat_pink';
cat_ids['c_3'] = 'cat_white';
cat_ids['c_4'] = 'cat_red';
cat_ids['c_5'] = 'cat_dessert';
cat_ids['c_6'] = 'cat_porto';

var origin_ids = [];
origin_ids['o_1'] = 'FR';
origin_ids['o_2'] = 'IT';
origin_ids['o_3'] = 'EU';
origin_ids['o_4'] = 'US';
origin_ids['o_5'] = 'NW';
origin_ids['o_6'] = 'XX';

function AdjustFormatDisplay()
{
	if (curFormat_id == 'f_0') {
		$('.quantite').each( function(){
			if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
			else { $(this).hide(); }
		});
	}
	else {
		if ($('#'+format_ids[curFormat_id]).children('li').children('ul:visible').length > 0) { $('#'+format_ids[curFormat_id]).show(); }
		else { $('#'+format_ids[curFormat_id]).hide(); }
	}
}

function SetCategoriesBgImage()
{
	$('.quantite:visible').each( function(){
		$(this).children('li').children('ul.couleur:visible').not(':last').css('backgroundImage','url(../images/tiret_1px.gif)');
		$(this).children('li').children('ul.couleur:visible:last').css('backgroundImage','none');
	});
}

function SetBottleCntText()
{
	var $lis = $('.quantite:visible .couleur:visible .pays:visible .region li');
	var cnt = $lis.length - $lis.children(':header').length;
	if (cnt == 1) {
		$('#bottleCntText').html('<strong>Une bouteille</strong> correspond');
	} else {
		$('#bottleCntText').html('<strong>'+cnt+' bouteilles</strong> correspondent');
	}
}


$(function(){

	$('#format_select').jSelect({ className : 'customSelect_145 format'});
	$('#category_select').jSelect({ className : 'customSelect_145_20_0 category'});
	$('#origin_select').jSelect({ className : 'customSelect_145_20_0 origin'});
	
	$('#wineListFiltersForm .jSelect.format li').click(function(e){
		e.preventDefault();
		
		var id = $(this).attr('id');
		
		// Don't do anything if we didn't change the selection
		if (id == curFormat_id) { return false; }
		
		// Hide the current ul(s)
		if (curFormat_id == 'f_0') {
			$('.quantite').not('#'+format_ids[id]).each( function(){ $(this).hide(); } );
		}
		else { $('#'+format_ids[curFormat_id]).hide(); }
		
		// Show the selected ul(s)
		if (id == 'f_0') {
			$('.quantite').each(function() {
				if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
				else { $(this).hide(); }
			});
		}
		else {
			$('#'+format_ids[id]).each(function() {
				if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
				else { $(this).hide(); }
			});
		}

		// Set the background image of the categories
		SetCategoriesBgImage();
		
		// Count the number of visible li(s) and display
		SetBottleCntText();
		
		// Set the selected ul as the current one
		curFormat_id = id;
		
		return true;
	});
		
	$('#wineListFiltersForm .jSelect.category li').click(function(e){
		e.preventDefault();
		
		var id = $(this).attr('id');
		
		// Don't do anything if we didn't change the selection
		if (id == curCategory_id) { return false; }
		
		// Hide the current ul(s)
		if (curCategory_id == 'c_0') {
			$('.couleur').not('.'+cat_ids[id]).each( function(){ $(this).hide(); } );
		}
		else { $('.'+cat_ids[curCategory_id]).hide(); }
		
		// Show the selected ul(s)
		if (id == 'c_0') {
			$('.couleur').each(function() {
				if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
				else { $(this).hide(); }
			});
		}
		else {
			$('.couleur.'+cat_ids[id]).each(function() {
				if ($(this).children('li').children('ul:visible').length > 0) {
					$(this).show();
				}
			});
		}
		
		// Set the display of the format ul(s)
		AdjustFormatDisplay();

		// Set the background image of the categories
		SetCategoriesBgImage();
		
		// Count the number of visible li(s) and display
		SetBottleCntText();
		
		// Set the selected ul as the current one
		curCategory_id = id;
		
		return true;
	});
		
	$('#wineListFiltersForm .jSelect.origin li').click(function(e){
		e.preventDefault();
		
		var id = $(this).attr('id');
		
		// Don't do anything if we didn't change the selection
		if (id == curOrigin_id) { return false; }
		
		// Hide the current ul(s)
		if (curOrigin_id == 'o_0') {
			$('.pays').not('.'+origin_ids[id]).each( function(){ $(this).hide(); } );
		}
		else { $('.'+origin_ids[curOrigin_id]).hide(); }
		
		// Show the selected ul(s)
		if (id == 'o_0') {
			$('.pays').show(); // Show all origins
		}
		else { $('.'+origin_ids[id]).show(); } // Show only selected origin
		
		// Set the display of the category ul(s)
		if (curCategory_id == 'c_0') {
			$('.couleur').each( function(){ // Cycle through all categories and show them if they have non-empty children uls
				if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
				else { $(this).hide(); }
			});
		}
		else {
			$('.couleur.'+cat_ids[curCategory_id]).each( function(){ // Cycle through the given categories
				if ($(this).children('li').children('ul:visible').length > 0) { $(this).show(); }
				else { $(this).hide(); }
			});
		}
		
		// Set the display of the format ul(s)
		AdjustFormatDisplay();
		
		// Set the background image of the categories
		SetCategoriesBgImage();
		
		// Count the number of visible li(s) and display
		SetBottleCntText();
				
		// Set the selected ul as the current one
		curOrigin_id = id;
		
		return true;
	});
});
