/**
 * @author Alberto Narda - TSW
 *
 */
 
 /**
 * comboSEF
 *
 */
 
(function($){
	$.fn.extend( {
		combosef: function(options){
			var defaults = {
					ordine : ''					
				};
			var options = $.extend(defaults, options);
			
			return this.children('ul').each(function(){
				var __e = $(this);
				
				/*================= START FUNCTION ========================*/
				var selectText='<select id="comboSef'+options.ordine+'" >';

				indent='';
				if ($(this).children('li:first').css("text-indent")!='0px') indent='style="text-indent:'+$(this).children('li:first').css("text-indent")+'"';

				selectText=selectText+'<option value="" '+indent+' >';
				selectText=selectText+$(this).children('li:first').html();
				selectText=selectText+'</option>';
				
				
				$(this).children('li').children('a').each(function(){
					
					if (($(this).attr("class")=="group"))
					{	
						indent='';
						if ($(this).css("text-indent")!='0px') indent='style="text-indent:'+$(this).css("text-indent")+'"';	
						selectText=selectText+'<optgroup label="'+$(this).html()+'" '+indent+' >';
						
						$(this).parent().children("ul").children().children().each(function(){
							indent='';
							if ($(this).css("text-indent")!='0px') indent='style="text-indent:'+$(this).css("text-indent")+'"';																
																							
							selectText=selectText+'<option value="'+$(this).attr("href")+'" '+indent+' >';
							selectText=selectText+$(this).html();
							selectText=selectText+'</option>';
						});
						selectText=selectText+'</optgroup>';
					}	
					else 
					{
						if (($(this).attr("class")=="inGroup")==false)
						{
						indent='';
						if ($(this).css("text-indent")!='0px') indent='style="text-indent:'+$(this).css("text-indent")+'"';
						selectText=selectText+'<option value="'+$(this).attr("href")+'" '+indent+' >';
						selectText=selectText+$(this).html();
						selectText=selectText+'</option>';
						}
					}
					
				});
				
				selectText=selectText+'</select>';
				$(this).parent().html(selectText);
				
				//USO LIVE PERCHE' L'ELEMENTO NON ESISTE SULL'HTML E' STATO AGGIUNTO DINAMICAMENTE VIA JS
				$("#comboSef"+options.ordine).bind("change", function(){
					  window.location=$("#comboSef"+options.ordine+" option:selected").attr("value");
				});
				/*================= END FUNCTION ========================*/
				
			});
		}
	});
})(jQuery);
