/**
 * @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+'" >';

				selectText=selectText+'<option value="">';
				selectText=selectText+$(this).children('li:first').html();
				selectText=selectText+'</option>';
				
				
				$(this).children('li').children('a').each(function(){
					
					if (($(this).attr("class")=="group"))
					{
						selectText=selectText+'<optgroup label="'+$(this).html()+'">';
						$(this).parent().children("ul").children().children().each(function(){
							selectText=selectText+'<option value="'+$(this).attr("href")+'">';
							selectText=selectText+$(this).html();
							selectText=selectText+'</option>';
						});
						selectText=selectText+'</optgroup>';
					}	
					else 
					{
						if (($(this).attr("class")=="inGroup")==false)
						{
						selectText=selectText+'<option value="'+$(this).attr("href")+'">';
						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(){

					  if (options.ordine == 1) {
						  window.location=$("#comboSef"+options.ordine+" option:selected").attr("value");
					  }
					  else
					  {
						  window.open($("#comboSef"+options.ordine+" option:selected").attr("value"), 'Brochure');
					  }

				});
				/*================= END FUNCTION ========================*/
				
			});
		}
	});
})(jQuery);
