$(document).ready(function() {
	$("#nav > ul").navMenu();
});

(function($) {
	$.fn.navMenu = function(o) {
		return this.each(function(){
			var $this = $(this);
			var curMenu  = null;
			var hideTimeout = 0;
			var toHide = null;
			
			var initMenuItem = function(li,top) {
				$li = $(li);
				
				$li.bind("mouseover", onItemMouseOver);
				
				$li.bind("mouseout", onItemMouseOut);
					
				$("> ul > li", $li).each(function() {
					//initMenuItem(this,false);
				});
			};
			
			var onItemMouseOver = function(event) {
				$li = $(this);
				
				$("> li", $this).removeClass("hover");
				$li.addClass("hover");
				
				hideSubMenu();
				window.clearTimeout(hideTimeout);
				
				if ($("> ul", $li).length > 0) {
					showSubMenu($li);
					curMenu = $li;
				}
			};
			
			var onItemMouseOut = function(event) {
				hideTimeout = window.setTimeout(hideSubMenu, 200);
			};
			
			var showSubMenu = function($li) { 
				$("> ul", $li).css("display", "block"); 
				
			};
			var hideSubMenu = function() {
				
				if (curMenu != null) {
					$("> ul", curMenu).css("display", "none");
				}
			};
			$("> li", $this).each(function() {
				initMenuItem(this, true);
			});
		});
	}
})($);
