$(document).ready(function(){
	$('#nav>ul>li').each(function(i,item){
		var mover = false;
		var hideTimeout = 0;
		var elem = this;
		
		var clearHideTimeout = function(){
			if ( hideTimeout > 0 ){
				window.clearTimeout(hideTimeout);
			}
			hideTimeout = 0;
		}
		var setHideTimeout = function() {
			clearHideTimeout();
			hideTimeout = window.setTimeout(function(){
				$(elem).removeClass("hover");
			},300);
		};
		
		$(item).bind('mouseover', function(event){
			if (!mover) {
				clearHideTimeout();
				$('#nav>ul>li').removeClass("hover");
				$(this).addClass('hover');
				mover = true;
			}
		});
		$(item).bind('mouseout', function(event){
			if (mover) {
				setHideTimeout();
			}
			mover = false;
		});
		
		if ($.browser.msie && $.browser.version < 8 &&  $("ul",elem).length) {
			var w = $("ul",elem).outerWidth();
			if (w < $(elem).outerWidth()) {
				$("ul",elem).css("width",$(elem).outerWidth());
				w = $(elem).outerWidth(true);
			}
			var pl,pr;
			$("ul a", elem).each(function(){
				pl = parseInt($(this).css("paddingLeft")) || 0;
				pr = parseInt($(this).css("paddingRight")) || 0;
				$(this).css({width:(w-pl-pr)+'px'});
			});
			
		}
		
		
		var links = $('a', item);
		if (links.length) {
			links = links[0];
			$(links).bind('focus', function(event){
				$(item).addClass('hover');
			});
		}
	});
});