$(function(){
	var busy = new Array();
	var timers = new Array();
	$('ul#menu li div.sub').each(function(){
		var index = $('ul#menu li div.sub').index(this);
		busy[index] = false;
		timers[index] = null;
		var width = 0;
		$(this).find('ul').each(function(){
			width += $(this).outerWidth(true);
		});
		$(this).css('width', width+'px');
	});
	$('ul#menu li div.sub').each(function(){
		var el = this;
		$(this).parents('ul#menu > li').mouseenter(function(){
			var elm = $(this);
			var index = $('ul#menu li div.sub').index($(this).find('div.sub'));
			timers[index] = clearTimeout(timers[index]);
			if(busy[index] == false){
				busy[index] = true;
				$(el).parent().find('.line').show();
				$(el).slideDown(500, function(){ busy[index] = false; });
			}
			else{
				timers[index] = setTimeout(function(){ $(elm).mouseenter(); },510);
			}
		}).mouseleave(function(){
			var elm = $(this);
			var index = $('ul#menu li div.sub').index($(this).find('div.sub'));
			timers[index] = clearTimeout(timers[index]);
			if(busy[index] == false){
				busy[index] = true;
				$(el).slideUp(500, function(){
					busy[index] = false;
					$(el).parent().find('.line').hide();
				});
			}
			else{
				timers[index] = setTimeout(function(){ $(elm).mouseleave(); },510);
			}
			
		});
	
	}).hide();
	$('ul#menu li div.line').hide();
});
