if (typeof je != "object") je = new Object();

if (typeof je.productnav != "object") je.productnav = new Object();

je.productnav.aBrands = [];
je.productnav.oFlyoutTimeout = null;
je.productnav.oActiveFlyoutLink = null;
je.productnav.iDelay = 200;
je.productnav.ie6 = false;

$(document).ready(function () {
    je.productnav.ie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7);

    // Preload brand logo and flyout images
    $.each(je.productnav.aBrands, function () {
        this.LogoImagePreload = new Image();
        this.LogoImagePreload.src = this.LogoImage;
        this.FlyoutImagePreload = new Image();
        this.FlyoutImagePreload.src = this.FlyoutImage;
    });

    je.productnav.oFlyoutBottom = new Image();
    je.productnav.oFlyoutBottom.src = "/static/images/flyout_bottom.png";
    je.productnav.oFlyoutTop = new Image();
    je.productnav.oFlyoutTop.src = "/static/images/flyout_top.png";

    // Brand links
    var aBrandLinks = $("#productnav li[class^=\"brand-\"] a");
    $.each(aBrandLinks, function (i) {
        this.onmouseover = function () {
            var brand = je.productnav.aBrands[i];
            if (brand == null)
                return;
            var sContent = "<div class=\"inner\">";
            if (brand.LogoImage != "")
                sContent += "<a href=\"" + brand.BrandWebPath + "\"><img src=\"" + brand.LogoImage + "\" alt=\"" + brand.Name + "\" class=\"logo\" /></a>";
            if (brand.FlyoutImage != "")
                sContent += "<a href=\"" + brand.BrandWebPath + "\"><img src=\"" + brand.FlyoutImage + "\" alt=\"" + brand.Name + "\" /></a>";
            if (brand.FlyoutText != "")
                sContent += "<p class=\"info\">" + brand.FlyoutText + "</p>";
            if (brand.Links.length > 0) {
                sContent += "<ul class=\"brand-links\">";
                var children = brand.Links.length;
                $.each(brand.Links, function (i) {
                    sContent += "<li";
                    if (i == children - 1) sContent += " class=\"last\"";
                    sContent += "><a href=\"" + this.Url + "\"";
                    if (this.Target != "") sContent += " target=\"" + this.Target + "\"";
                    sContent += ">" + this.Title + "</a></li>";
                });
                sContent += "</ul>";
            }
            sContent += "</div>"
            je.productnav.showFlyout(this, sContent);
        };
        this.onmouseout = function () {
            je.productnav.hideFlyout();
        };
    });
});

je.productnav.showFlyout = function (oLink, sHtml) {
	if (je.productnav.oFlyoutTimeout != null)
		clearTimeout(je.productnav.oFlyoutTimeout);
	var oFlyout = je.productnav.getFlyout();
	if (je.productnav.oActiveFlyoutLink != oLink) {
		if (je.productnav.oActiveFlyoutLink != null)
			je.productnav.oActiveFlyoutLink.className = null;
		je.productnav.oActiveFlyoutLink = oLink;
		oFlyout.innerHTML = sHtml;
	}
	je.productnav.positionFlyout(oLink);
	if (document.all) {
		$(oFlyout).show();
		//fixIePng("#flyout");
	} else {
		$(oFlyout).fadeIn(je.productnav.iDelay);
	}
	if (je.productnav.ie6)
		je.productnav.hideOverlappingDropdowns();
}

je.productnav.hideFlyout = function (iDelay) {
	je.productnav.oFlyoutTimeout = setTimeout(function () {
		if (je.productnav.oActiveFlyoutLink != null)
			je.productnav.oActiveFlyoutLink.className = null;
		var oBrandFlyout = $("#flyout");
		if (oBrandFlyout.length > 0) {
			if (document.all)
				oBrandFlyout.hide();
			else
				oBrandFlyout.fadeOut(je.productnav.iDelay);
		}
		if (je.productnav.ie6)
			je.productnav.showOverlappingDropdowns();
	}, (iDelay != null) ? iDelay : je.productnav.iDelay);
}

je.productnav.positionFlyout = function (oLink) {
	// Offset vs position
	var iPageDivX = $("#page").offset().left;
	oLink = $(oLink);
	var iLinkX = oLink.offset().left;
	var iLinkFromPageDivX = iLinkX - iPageDivX;
	var iLinkWidth = oLink.innerWidth();
	var oFlyout = je.productnav.getFlyout();
	var iFlyoutWidth = 275;
	var iFlyoutsLLeft = $("#flyouts").offset().left;
	var iFlyoutsLRight = $("#flyouts").offset().left;
	var iDocumentWidth = $(document).width();
	var iLeft = iLinkFromPageDivX - ((iFlyoutWidth - iLinkWidth) / 2);
	var iRight = 0;
	var iHeight = 36;
	var iTop = oLink.offset().top + iHeight;
	if (iLeft < 0 && iLeft * -1 > iFlyoutsLLeft) {
		iLeft = -iFlyoutsLLeft;
	} else if (iLeft + iFlyoutWidth > iDocumentWidth - iFlyoutsLLeft) {
		iLeft = iDocumentWidth - iFlyoutsLLeft - iFlyoutWidth;
	}
	oFlyout.style.left = iLeft + "px";
	oFlyout.style.top = iTop + "px";
}

je.productnav.getFlyout = function() {
	var oBrandFlyout = document.getElementById("flyout");
	if (oBrandFlyout == null) {
		oBrandFlyout = document.createElement("div");
		oBrandFlyout.id = "flyout";		
		var oFlyouts = document.getElementById("flyouts");
		if (oFlyouts == null) {
			oFlyouts = document.createElement("div");
			oFlyouts.id = "flyouts";
			document.getElementById("page").appendChild(oFlyouts);
		}
		oBrandFlyout.style.display = "none";
		oFlyouts.appendChild(oBrandFlyout);
		oBrandFlyout.onmouseover = function() {
			if (je.productnav.oActiveFlyoutLink != null) {
				je.productnav.oActiveFlyoutLink.className = "hover";
			}
			if (je.productnav.oFlyoutTimeout != null)
				clearTimeout(je.productnav.oFlyoutTimeout);
		}
		oBrandFlyout.onmouseout = function() {
			je.productnav.hideFlyout();
		}
	}
	return oBrandFlyout;
}
