jQuery(function($){
		var lis=$('#main-menu li:has(div)');
		lis.addClass('w-sub')
		.each(function(i){
			var tm=null,t=$(this);
			t		// qualche piccola complicazione, per gestire sottomenų adiacenti
				.bind('hide',function(e){if(tm)clearTimeout(tm);t.removeClass('on');})
				.bind('show',function(e){if(tm)clearTimeout(tm);t.addClass('on');})
				.mouseenter(function(e){t.trigger('show');lis.not(t).trigger('hide');})
				.mouseleave(function(e){tm=setTimeout(function(){t.trigger('hide');},700);})
			.find('div:eq(0)')
				.mouseenter(function(e){t.trigger('show')})
				.mouseleave(function(e){t.trigger('hide')});
		})
		// spezza liste lunghe in 2 parti
		.find('ul:eq(0)').each(function(){
			var t=$(this);var l1=t.find('li');
			if(!(l1.length>4)) return;
			var l2=l1.slice(Math.max(3,Math.ceil(l1.length/2))).detach();
			var u2=$('<ul/>').append(l2).insertAfter(t);
			var w2=20,w=0,w1=0;
			w1 = t.closest('div').width();
			t.closest('div').find('ul').each(function(){w+=$(this).outerWidth(true);}).css({clear:'none'})
			// allarga tutto (ma non troppo) come l'ultima riga di link
			.end().find('small a').each(function(){w2+=$(this).outerWidth(true);})
			.end().width( (w2>w && w2<w*3/2)?w2:w );
		});

		// correzione layers IE7
		if ( $.browser.msie && parseInt($.browser.version)<8 ) { $('#main-menu').detach().appendTo('#container'); } 
});
