function init_dropdown(){
	$("select.dropdown").each(
		function (intIndex ){
			code="<div class=\"replace_dropdown\">"+
			"<span onclick=\"$('div#choices_"+intIndex+"').toggle('fast');\" class=\"input_text_dropdown\" name=\"drop_down_list_"+intIndex+"\" "+
			"id=\"drop_down_list_"+intIndex+"\"></span>"+
			"<span class=\"fleche\" onclick=\"$('div#choices_"+intIndex+"').toggle('fast'); \">"+
			"&nbsp;</span><br>"+
				"<div id=\"choices_"+intIndex+"\"  class=\"choices\" >"+
				"<a class=\"close\"><span class=\"none\">Valider </span>X</a>";
			if($(this).attr("multiple")){
				code+=multiple(this,intIndex);
			}else{
				code+=simple(this,intIndex);
			}
			
			code+="</div>"+
			"</div>";
			//Insert le code
			$(this).before(code);
			$(this).toggle();
			
			calculerDropDown(intIndex);
		}
	);
	$('.close').click(function(){ $(this).parent().hide(); });
}
function simple(elem, index){
	
	code='<ul>';
	$(elem).find('option').each(
		function (intIndex){
				code+='<li>';
				code+='<input type="radio" value="'+$(this).attr('value')+'" ';
				code+=' onchange="calculerDropDown('+index+'); $(\'div#choices_'+index+'\').toggle(\'fast\');" ';
				if(this.selected){
					code+='checked';
				}
				code+=' id="choices_'+index+'_'+intIndex+'" name="choices_'+index+'">';
				code+='<label for="choices_'+index+'_'+intIndex+'" id="label_'+index+'_'+intIndex+'">'+this.innerHTML+'</label>';	
				code+='</li>';
			
		}
	)
	code+='</ul>';
	return code;
}

function checkAllChilds(input){
	input_rel=$(input).attr('id');
	$('input[rel='+input_rel+']').attr('checked',$(input).is(':checked'));
}
gid=0;
function multiple(elem, index){
	if($(elem).find('optgroup')){
		optgroup='';
		
	}
	code='<ul>';
	
	$(elem).find('option').each(
		function (intIndex){
			if($(elem).find('optgroup')){
				if($(this).parent('optgroup').attr('label')){
					lopt=$(this).parent('optgroup').attr('label');
					if(optgroup!=lopt){
						optgroup=lopt;
						gid++;
						code+='</ul><li>';
						code+='<input class="optgroup" type="checkbox" value="all_childs" ';
						code+=' onchange="checkAllChilds(this); calculerDropDown('+index+')" ';
						code+=' id="choices_'+gid+'_group" name="choices_'+gid+'_group">';
						code+='<label for="choices_'+gid+'_group" id="label_'+gid+'"><b>'+optgroup+'</b></label>';	
						code+='<ul>';
					}
				}
			}
			if($(this).attr('value')){
				code+='<li>';
				code+='<input type="checkbox" value="'+$(this).attr('value')+'" ';
				code+=' onchange="calculerDropDown('+index+')" ';
				if(this.selected){
					code+='checked';
				}
				code+=' id="choices_'+index+'_'+intIndex+'" rel="choices_'+gid+'_group" name="choices_'+index+'[]">';
				code+='<label for="choices_'+index+'_'+intIndex+'" id="label_'+index+'_'+intIndex+'">'+this.innerHTML+'</label>';	
				code+='</li>';
			}else{
				code+='<li>';
				code+='<input type="checkbox" style="display:none" value="'+$(this).attr('value')+'" ';
				code+=' onchange="calculerDropDown('+index+')" ';
				code+=' id="choices_'+index+'_'+intIndex+'" rel="choices_'+gid+'_group" name="choices_'+index+'[]">';
				code+='<label for="choices_'+index+'_'+intIndex+'" id="label_'+index+'_'+intIndex+'">'+this.innerHTML+'</label>';	
				code+='</li>';
			}
		}
	)
	code+='</ul>';
	return code;
}

function calculerDropDown(index){
	va='';
	selects=$("select.dropdown");
	select=selects[index];
	/*for(i in select.options){
		//select.options[i].selected=false;
	}*/
	related=0;
	list=new Array();
	$('div#choices_'+index+' input.optgroup').each(
	function(){
		list[$(this).attr('id')]=1;
	}
	);
	$('div#choices_'+index+' input').each(
		function (intIndex){
			if(!$(this).hasClass('optgroup')){
				if(!$(this).is(':checked')){
					list[$(this).attr('rel')]=0;
				}
			}
		}
	);
	for(id in list){
		$('#'+id).attr('checked',list[id]);
	}
	$('div#choices_'+index+' input').each(
		function (intIndex){
			    	if($(this).hasClass('optgroup')){
			    		related++;
				    	if($(this).is(':checked')){
				    		if(va!=''){
								va+=" , ";
							}
							id=$(this).attr('id');
							
							va+=$('label[for='+id+']').html();
							
				    	}
			    	}else{
					
						if($(this).attr('checked')){
							rel=$(this).attr('rel')
							if(!rel){
								if(va!=''){
										va+=" , ";
									}
									//select.options[(intIndex)].selected="selected";
								
									va+=document.getElementById('label_'+index+'_'+(intIndex-related)).innerHTML;
							}else{
								if($('#'+rel).is(':checked')){
								}else{
									if(va!=''){
										va+=" , ";
									}
									//select.options[(intIndex)].selected="selected";
								
									va+=document.getElementById('label_'+index+'_'+(intIndex-related)).innerHTML;
								}
							}
						}
					}
		}
	);
	if(va.length>35)
		va=va.substr(0,35)+'...';
	document.getElementById('drop_down_list_'+index).innerHTML=va;
}
