(function($) {
	var gDropdownTimeout = undefined;

	function hideDropdown() {
        $(".dropDown").stop(true, true).fadeOut();
    }
    
    function clearDrop() {
        if (gDropdownTimeout != undefined) {
            clearTimeout(gDropdownTimeout);
            gDropdownTimeout = undefined;
        }
    }

	$(document).ready(function() {
		var $menu = $("#mainMenu");
		var $dd = $(".dropDown").prependTo(document.body)
		.css({top: $menu.offset().top + $menu.height()});

        $(".shimglobal_navigation li").each(function (i) {
            //line up menu columns (overrides the 100px width)
            $(".linkcolumn:nth-child(" + String(i+1) + ")").css("width",$(this).outerWidth(true));
        });
        
        $(".shimglobal_navigation li").mouseenter(function () { 
            clearDrop();
            $(".dropDown").slideDown().mouseenter(function () {
                clearDrop();
                $(this).mouseleave(function () {
                    hideDropdown();
                });
            });
        }).mouseleave(function () {
            gDropdownTimeout = setTimeout(function() {
				hideDropdown();
			},500);
        });
        
        $j(".hoverable").mouseover(function () {
           $(this).attr("offstate", $(this).attr("src"));
           $(this).attr("src", $(this).attr("hoversrc"));
       })
       .mouseout(function () {
           $(this).attr("src", $(this).attr("offstate"));
           $(this).attr("offstate","");
        });

		$(".dropDown").delegate("a", "click", function() {
			if (this.href.match(/\.html$/)) {
				return true;
			}
			
			if ($(this).next(".menuSubLinks").length == 0 && $(this).parents(".menuSubLinks").size() + 2 < depth) {
				var href =  ctxPath + this.rel + ".nav_dropdown_data.json"
					+ (window.location.search.length > 0 ? "?param=1": "");
				var $this = $(this);
				var leftM = $this.outerWidth(true) - $this.outerWidth() + 10;
				$.getJSON(href, function(data) {				
					var $menuIndent = $("<div/>").addClass("menuSubLinks").hide();
					var count = 0;
					for (i = 0; i < data.length; i++) {
						var link = data[i];
						if (link == null) continue;
						count++;
						var $a = $("<a/>", {href: link.url, rel: link.handle})
						.addClass("menuLinkIndented")
						.css("margin-left", leftM)
						.text(link.text);

						if (link.popup == "true") $a.addClass("popup_content");
						if (link.target != "_self") $a.attr("target", link.target);

						$menuIndent.append($a);
						if (i != data.length - 1) $menuIndent.append("<br/>");
					}
					
					$this.data("children", count);
					$this.after($menuIndent);
					showMenu($this);
				});
			} else {
				showMenu($(this));
			}

			return false;
		});

		function showMenu($a) {
			var $col = $a.closest(".linkcolumn");
			var $open = $(".menuSubLinks:visible", $col).not($a.parents());
			if ($open.size() > 0) {
				$open.slideUp(function() {
					$(".menuLinkExpanded", $col).removeClass("menuLinkExpanded");
					$(this).next("br").show();
					$a.addClass("menuLinkExpanded");
					if ($a.data("children") > 0) {
						$a.next(".menuSubLinks").slideDown().next("br").hide();
					}
				});
			} else {
				$(".menuLinkExpanded", $col).removeClass("menuLinkExpanded");
				$a.addClass("menuLinkExpanded");
				if ($a.data("children") > 0) {
					$a.next(".menuSubLinks").slideDown().next("br").hide();
				}
			}
		}
	});
})(jQuery);

