$(function() {

    $("ul.left-navigation li").mouseenter(function() {
		
		var $this = $(this);
		
		if ($this.attr("timeoutid") != undefined && $this.attr("timeoutid") != "") {
			window.clearTimeout(parseInt($this.attr("timeoutid")));
			$this.removeAttr("timeoutid");
		}
		
		$this.siblings().filter(function() {
			return $(this).children("a").is(".selected");
		}).each(function() {
			var $sibling = $(this);
			$sibling.children("ul").hide();
			$sibling.children("a").removeClass("selected");
			if ($sibling.attr("timeoutid") != undefined && $sibling.attr("timeoutid") != "") {
				window.clearTimeout(parseInt($sibling.attr("timeoutid")));
				$sibling.removeAttr("timeoutid");
			}
		});
		
		var subMenu = $this.children("ul");
		
		if (subMenu.length > 0) {
			subMenu.css({
				"position": "absolute",
				"top": $this.position().top - 6 + "px",
				"left": ($this.position().left + $this.width()) + 1 + "px"
			}).show();
			$this.children("a").addClass("selected");

			if ($.browser.msie && parseInt($.browser.version) == 6) {
				$this.height(20).css("overflow", "hidden");
			}
		}
		
	}).mouseleave(function() {
		var $this = $(this);
		
		var subMenu = $this.children("ul");
		
		if (subMenu.length > 0 && subMenu.is(":visible")) {
			
			var timeoutid = window.setTimeout(function() {
				$this.children("ul").hide();
				$this.children("a").removeClass("selected");
				
				$this.find("li").filter(function() {
					return $(this).children("a").is(".selected");
				}).each(function() {
					var $submenu = $(this);
					$submenu.children("ul").hide();
					$submenu.children("a").removeClass("selected");
					if ($submenu.attr("timeoutid") != undefined && $submenu.attr("timeoutid") != "") {
						window.clearTimeout(parseInt($submenu.attr("timeoutid")));
						$submenu.removeAttr("timeoutid");
					}
				});
				
				$this.removeAttr("timeoutid");
			}, 3000);
			
			$this.attr("timeoutid", timeoutid);
			
		}
	});
	
	$("body").click(function(event) {
		if ($(event.target).closest("ul.left-navigation").length == 0) {
			$("ul.left-navigation li").filter(function() {
				return $(this).children("a").is(".selected");
			}).each(function() {
				var $menu = $(this);
				$menu.children("ul").hide();
				$menu.children("a").removeClass("selected");
				if ($menu.attr("timeoutid") != undefined && $menu.attr("timeoutid") != "") {
					window.clearTimeout(parseInt($menu.attr("timeoutid")));
					$menu.removeAttr("timeoutid");
				}
			});
		}
	});

});
