jQuery(document).ready(function () {

	jQuery('.ssgroupe').hide().parent().find('input[@type=checkbox]').bind('click',function (){
		lul=$(this).parent().find('ul').toggle()
		lul.find('select option').attr('selected','');
		lul.find('input[@type=checkbox]').attr('checked','');
	});
	jQuery('.filtre_metier > fieldset > ul > li > select').bind('change',function (){
		jQuery(this).parent().find('ul.ssgroupe select').val(0);
		jQuery(this).parent().find('ul.ssgroupe').hide();
		if(jQuery(this).val())
			jQuery(this).parent().find('ul.ssgroupe'+jQuery(this).val()).show();
	});


	jQuery("li.autossgroupe").each(function(){
		var tab=new Array();
		var i=0;
		var y='';
		jQuery(this).find("select").bind('change',function(){
			if (jQuery(this).val()>0) jQuery('#'+jQuery(this).attr('rel')).attr('disabled','disabled');
			else jQuery('#'+jQuery(this).attr('rel')).attr('disabled','');
			if (jQuery(this).val()=='0')
				jQuery(this).parent().find('select.autossgroupe').show();
			else {
				lval=jQuery(this).parent().find('select.autossgroupe').hide().val();
				jQuery(this).parent().find("select.autossgroupe option[@value='"+lval+"']").attr('selected','');
			}
		});
		idselect=jQuery(this).find("select").attr('id');
		jQuery(this).find("ul.ssgroupe").each(function(){
			idoption=jQuery(this).attr('class').replace('ssgroupe ssgroupe','');
			jQuery(this).find("select").each(function(){
				idssselect=jQuery(this).attr('id');
				jQuery(this).find("option[@value!=0]").each(function(){
					option_val=jQuery(this).attr('value');
					option_txt=jQuery(this).html();
					option=option_txt+' <option rel="'+idoption+'" value="'+option_val+'">';
					tab[i]=option;
					i++;
				});
			});
		});
		tab.sort();
		for (x=0;x<i;x++) {
			y+=tab[x].substring(tab[x].indexOf(' <option rel='),tab[x].length)+tab[x].substring(0,tab[x].indexOf(' <option rel='))+'</option>';

		}
		if (y!='')
			jQuery(this).append('<ul><li><select rel="'+idselect+'" class="autossgroupe" name="id_mot[]"><option rel="0" value="0">-----</option>'+y+'</select></li></ul>');

	});
	jQuery("select.autossgroupe").bind('change',function(){
		loption=jQuery(this).find("option[@value='"+jQuery(this).val()+"']").attr('rel');
		jQuery("#"+jQuery(this).attr('rel')+" option[@value='"+loption+"']").attr('selected','selected');
		if (jQuery(this).val()=='0')
			jQuery("#filtre_ou #"+jQuery(this).attr('rel')+"").attr('disabled','');
		else
			jQuery("#filtre_ou #"+jQuery(this).attr('rel')+"").attr('disabled','disabled');
	});


});

