var cmsRootJs = ((window.location.protocol).toString()) + "//" + ((window.location.host).toString()) + "/qptcms";
var rootEnv = ((window.location.protocol).toString()) + "//" + ((window.location.host).toString());
var strSelChannel = "";
var menuObj;
var bmg_topNavContainer = jQuery("div.bmg_main_nav");
var menuStyles = {};
var minChWidth = "";
var keyboardEnable = true;
(function(a){
    a.fn.setMenuHover = function(b){
        return this.each(function(){
            b == true ? a(this).addClass("menuOver") : a(this).removeClass("menuOver")
        })
    }
})(jQuery);
function getMenuXML(){
    var s;
    jQuery.ajax({
        type: "GET",
        url: menuXMLURL,
        async: false,
        cache: false,
        dataType: (jQuery.browser.msie) ? "text" : "xml",
        success: function(data){
            var xml;
            if (typeof data == "string") {
                xml = new ActiveXObject("Microsoft.XMLDOM");
                xml.async = false;
                xml.loadXML(data)
            }
            else {
                xml = data
            }
            var json = xml2json(xml);
            json = json.replace("undefined", "");
            menuObj = eval("(" + json + ")")
        }
    })
}

var startSeconds = "";
var selChannel = "";
var bmg_topNavItems = new Array();
var defaultMegaWidth = "550";
var altMegaWidth = "450";
var solutionsAdPath = "";
var productAdPath = "";
var symbolSel = "&#171";
var arrChannelItems = [];
var strChannelItemOrder = [];
var arrChannelItems = [];
var arrNewArray = [];
var arrChannelItemTarget = [];
var arrChannelItemLinks = [];
var defaultMegaHeight = "320";
var defWidth = "800";
var defHeight = "600";
var menuProps = {};
function bmg_getSetDimensions(b, a){
    if (typeof b == "object") {
        menuProps.thisText = b.text();
        menuProps.thisWidth = b.outerWidth();
        menuProps.hasAdvert = b.hasClass("adPresent");
        menuProps.thisPosL = b.position().left;
        menuProps.firstMenuItemPosL = b.offset().left;
        menuProps.firstMenuItemPosT = b.offset().top;
        menuProps.thisMenuItemPosT = b.offset().top;
        menuProps.thisMenuItemPosL = b.position().left;
        menuProps.thisMenuItemHt = b.outerHeight();
        menuProps.subMenuWidth = jQuery(b).find("div.bmg_subMenu").width();
        if (a) {
            menuStyles.zIndex = "47";
            menuStyles.display = "block";
            menuStyles.textAlign = "left";
            menuStyles.left = menuProps.thisPosL
        }
        else {
            menuStyles.zIndex = "49";
            menuStyles.display = "block";
            menuStyles.textAlign = "left";
            menuStyles.left = "0"
        }
        setCSS(a, b, menuProps)
    }
    else {
        jQuery("div.bmg_subMenu").css("visibility", "hidden")
    }
}

function setCSS(c, b, a){
    var d = jQuery(b).find("div.bmg_subMenu").hasClass("subMenuMega");
    jQuery(b).find("div.bmg_subMenu").find("div.menu_seperator").height("");
    jQuery(b).find("div.bmg_subMenu").find("div.menu_seperator").height(jQuery(b).find("div.bmg_subMenu").height());
    menuStyles.top = ((a.firstMenuItemPosT - a.thisMenuItemHt) - 3) + "px";
    if (c) {
        menuStyles.visibility = "visible";
        delete menuStyles.width;
        if (d) {
            menuStyles.top = ((a.firstMenuItemPosT - a.thisMenuItemHt) - 3) + "px";
            menuStyles.left = ((a.thisPosL + (a.thisWidth / 2)) - (parseFloat(a.subMenuWidth / 2))) + "px"
        }
        else {
            menuStyles.top = ((a.thisMenuItemPosT - a.thisMenuItemHt) - 3) + "px";
            menuStyles.left = $(b).hasClass("channelSelected") ? (a.thisMenuItemPosL + 2) + "px" : menuStyles.left = a.thisMenuItemPosL - 2 + "px";
            menuStyles.width = $(b).hasClass("channelSelected") ? ($(b).outerWidth() > minChWidth ? jQuery(b).outerWidth() - 2 : minChWidth) : $(b).hasClass("lastMenuBorder") ? $(b).outerWidth() + 1 : $(b).outerWidth() + 2
        }
    }
    else {
        menuStyles.visibility = "hidden";
        menuStyles.left = "0";
        delete menuStyles.width
    }
    jQuery(b).find("div.bmg_subMenu").css(menuStyles)
}

var resourcecenterSelected;
function bmg_buildTopNav(a){
    jQuery("ul.bmg_topNav").empty();
    bmg_userLogin(userloginStatus);
    jQuery.each(menuObj.menus.menu.menuList, function(b, f){
        try {
            resourceSelected == true ? resourcecenterSelected = true : resourcecenterSelected = false;
            manageservicesSelected == true ? managemyservicesSelected = true : managemyservicesSelected = false
        } 
        catch (e) {
        }
        var d = this.link === null ? "javascript:;" : this.link;
        var c = getVal(this.text.toString().toLowerCase().replace(/ /g, "") + "Selected") == true ? "selected" : "";
        jQuery("ul.bmg_topNav").append('<li class="' + c + '"> <a href="' + d + '" class="menuCog"> <span>' + this.text + " </span> </a> </li>");
        bmg_topNavItems.push(this.text)
    });
    if ($("ul", jQuery("div.bmg_main_nav").filter(":not(:empty)")).length >= 2) {
        $("ul:first", jQuery("div.bmg_main_nav")).find("li:first").children("a").addClass("firstMenuTab");
        $("ul:last", jQuery("div.bmg_main_nav")).find("li:last").addClass("lastMenuBorder").children("a").addClass("lastMenuTab")
    }
    else {
        $("ul:first", jQuery("div.bmg_main_nav")).find("li:first").children("a").addClass("firstMenuTab")
    }
    jQuery("ul.bmg_topNav:first li").each(function(b, c){
        jQuery(menuObj.menus.menu.menuList).each(function(e, f){
            if (jQuery.trim(f.text) == jQuery.trim(jQuery(c).text())) {
                var g = this.isMega;
                var d = this.advertisement;
                bmg_isMegaMenu(c, g, f);
                bmg_hasAdverts(c, d);
                if (f.menuItemHead != undefined) {
                    jQuery(f.menuItemHead).each(function(h, i){
                        var j = i.text != null ? "<dt>" + bmg_insertIcon(i.icon, i.text) + " " + bmg_insertHyperlink(i.text, i.link, i.newWindow, i.isModal, i.width, i.height) + "</dt>" : "";
                        jQuery(c).find("div.bmg_subMenu div.bmg_subMenu_container  div.bmg_subMenu_content").children("div.col").append('<dl class="eachBlock">\n' + j + "\n</dl>\n");
                        jQuery(i.menuItems).each(function(k, m){
                            jQuery(this.menuItem).each(function(l, n){
                                jQuery(c).find("div.bmg_subMenu div.bmg_subMenu_container  div.bmg_subMenu_content").children("div.col").append("<dd>" + bmg_insertIcon(n.icon, n.text) + " " + bmg_insertHyperlink(n.text, n.link, n.newWindow, n.isModal, n.width, n.height) + " </dd>\n");
                                return
                            });
                            return
                        });
                        return
                    })
                }
                return
            }
            return
        });
        return
    });
    jQuery("div.bmg_subMenu").each(function(c, b){
        var e = 0;
        var d;
        if ($(this).find("dl.eachBlock").length <= 0) {
            $(this).remove()
        }
        jQuery(b).find("dl.eachBlock").each(function(f, g){
            jQuery(g).nextAll().each(function(j, h){
                if (jQuery(h).is("dd")) {
                    jQuery(g).append(h)
                }
                else {
                    return false
                }
            });
            e += jQuery(g).outerHeight();
            d = jQuery(this);
            return
        })
    });
    bmg_triggerHover(jQuery("ul.bmg_topNav:first li"))
}

var strSelChannelLink = "";
function bmg_buildChannelNav(b){
    var a = "";
    a = new Date();
    startSeconds = a.getTime();
    jQuery("ul.bmg_channelNav").empty();
    strChannelItemOrder.length = 0;
    arrChannelItems.length = 0;
    arrNewArray.length = 0;
    channelE == "Large Business" ? strSelChannel = "Medium-Large Business" : "";
    jQuery.each(menuObj.menus.channel.channelItem, function(d, e){
        strChannelItemOrder.push(this.order);
        arrChannelItems.push(this.text);
        arrChannelItemLinks.push(this.link);
        arrChannelItemTarget.push(this.newWindow);
        (strSelChannel == "") || (strSelChannel == "Large Business") ? strSelChannel = "Medium-Large Business" : "";
        if ((channelE == "") || (channelE == undefined) || (channelE == null)) {
            if (jQuery.trim(this.isSelected) == "true") {
                strSelChannel = jQuery.trim(this.text)
            }
        }
        else {
            channelE == "Large Business" ? strSelChannel = "Medium-Large Business" : strSelChannel = channelE
        }
    });
    jQuery.each(arrChannelItems, function(d, e){
        jQuery.each(strChannelItemOrder, function(g, f){
            if ((d + 1) == f) {
                arrNewArray.push(arrChannelItems[g]);
                arrChannelItemLinks.push(arrChannelItemLinks[g]);
                arrChannelItemTarget.push(arrChannelItemTarget[g])
            }
            return
        });
        strSelChannel = cleanTextInput(strSelChannel);
        if ($.trim(e) == $.trim(strSelChannel)) {
            strSelChannelLink = arrChannelItemLinks[d];
            return
        }
    });
    var c = getVal(channelE.toLowerCase().replace(/ /g, "").replace(/&amp;/g, "") + "Selected") == true ? "selected" : "";
    chWidth = menuObj.menus.channel.channelWidth != "" ? "width:" + menuObj.menus.channel.channelWidth + "px" : "";
    minChWidth = menuObj.menus.channel.channelWidth;
    jQuery("ul.bmg_channelNav").append('<li class="channelSelected ' + c + '"> <div class="bmg_subMenu" style="' + chWidth + '">\n <div class="bmg_subMenu_container">\n<div class="bmg_subMenu_content">\n<div class="col">\n<dl class="eachBlock">\n');
    jQuery.each(arrNewArray, function(d){
        jQuery("ul.bmg_channelNav li.channelSelected div.bmg_subMenu div.col dl.eachBlock").append("<dd>" + bmg_insertHyperlink(this, arrChannelItemLinks[d], arrChannelItemTarget[d], "", "", "") + "</dd>")
    });
    jQuery("ul.bmg_channelNav").find("li.channelSelected").prepend('<a class="menuCog" href="' + strSelChannelLink + '">' + strSelChannel + "</a>").find("div.bmg_subMenu div.col dl dd a").not(".channelSelected").each(function(){
        jQuery(this).text() != strSelChannel ? "" : jQuery(this).parent("dd").addClass("activeChannel").html("<span class='activeChannel channelSelected'>" + strSelChannel + "</span>  <span class='selIcon'>" + symbolSel + " </span>")
    });
    bmg_triggerHover(jQuery("ul.bmg_channelNav:first li"))
}

function cleanTextInput(a){
    a = a.replace(/&amp;/gi, "&");
    return a
}
var hasFocus = false;
function bmg_triggerHover(b){
    jQuery(b).find("a.newWindow").bind("click", function(c){
        c.preventDefault();
        window.open(this, "someName", "", "")
    });
    jQuery("ul.bmg_topNav").children("li").find("a.thickbox").bind("click", function(c){
        c.preventDefault();
        isModalWin(jQuery(this));
        return false
    });
    var a;
	jQuery(b).mousedown(function(e){
		hasFocus = true;
		//alert(e.which)
		if (e.which == 3) {
			jQuery(b).find(".bmg_subMenu").mouseover(function(){
				hasFocus = false;
			})
		}
	
		})
    jQuery(b).hover(function(d, c){
		hasFocus = false;
        $(this).addClass("menuOver");
        $(this).parents("div.bmg_main_nav").find("li").not($(this)).removeClass("menuOver");
        bmg_getSetDimensions($(this), true);
        bmg_getSetDimensions($(this).parents("div.bmg_main_nav").find("li").not($(this)), false);
        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");
            a = true
        }
    }, function(){
		if (!hasFocus) {
			$(this).removeClass("menuOver");
			bmg_getSetDimensions($(this), false);
			if (a) {
				$(this).addClass("selected");
				a = false
			}
		}
    });
    if (keyboardEnable) {
        kbEnable(b)
    }
}

function kbEnable(a){
    jQuery(a).find("a:first").focus(function(c, b){
        $(this).parent("li").addClass("menuOver");
        $(this).parents("div.bmg_main_nav").find("li").not($(this).parent("li")).removeClass("menuOver");
        bmg_getSetDimensions($(this).parent("li.menuOver"), true);
        bmg_getSetDimensions($(this).parents("div.bmg_main_nav").find("li").not($(this).parent("li")), false)
    })
}

function bmg_isMegaMenu(c, a, d){
    var e = d.menuWidth != "undefined" ? d.menuWidth : "";
    var b = d.menuHeight != "undefined" ? d.menuHeight : "";
    if (a == "true") {
        jQuery(c).append(' <div class="bmg_subMenu subMenuMega" style="width: ' + e + "px;height: " + b + 'px;">\n <div class="bmg_subMenu_container">\n<div class="bmg_subMenu_content">\n<div class="col">\n')
    }
    else {
        jQuery(c).append(' <div class="bmg_subMenu" style="width: ' + e + "px;height: " + b + 'px;">\n<div class="bmg_subMenu_container">\n<div class="bmg_subMenu_content">\n<div class="col">\n')
    }
    bmg_getSetDimensions($(c), false)
}

function bmg_hasAdverts(a, c){
    var b = jQuery(document.createElement("div"));
    b.addClass("advertisement col");
    if (jQuery.trim(c) == "true") {
        menuProps.hasAdvert = true;
        jQuery(a).addClass("adPresent");
        jQuery.each(velocityAdPath, function(g, f){
            if (jQuery.trim(jQuery(a).text().toLowerCase().replace(" ", "")) == g) {
                try {
                    if (isCrossDomain) {
                        var e = "";
                        e += '<div class="advertisement" style="float:left;"><div class="bmg_ad_container"><div class="ad_container"><a class="toClick" href="https://www.qwest.com/qptcms/qCmsRepository/resources/pdfs/featured-solutions/service-assurance-service-support-ss090866.pdf" target="_blank"><div class="ad_contTop"><h3 class="f10">Enjoy Peace of Mind with Our Service Assurance</h3><span class="img"><img alt="Enjoy Peace of Mind with Our Service Assurance" src="https://www.qwest.com/qptcms/qCmsRepository/Global/largeBusiness/resources/global-nav/images/ad-images/menu2/004.gif" /> </span><p class="body">Depend on our commitment to create, implement and support nimble solutions for your business.</p></div><p class="learnMore" style="padding-top: 7px">Learn more</p></a></div><dl><dd class="bmg_seeAll"><a href="https://www.qwest.com/business/products/special-offers/list.html">See All</a></dd></dl></div></div><div class="bmg_clear"></div>';
                        jQuery("div.bmg_subMenu div.col:last", a).after('<div class="menu_seperator"></div>');
                        jQuery("div.bmg_subMenu div.menu_seperator:last", a).after(e)
                    }
                } 
                catch (d) {
                    bmg_getAdvert(jQuery("div.bmg_subMenu div.col", a), f, b)
                }
                return false
            }
        })
    }
    else {
    }
    return c
}

function bmg_getAdvert(b, d, f){
    var h;
    var e = (d.path).toString();
    var a = (((window.location.protocol).toString()) + "//" + ((window.location.host).toString()) + "/qptcms" + e).toString();
    if (!d.path == "") {
        var c = "";
        var g = new Date();
        c = g.getTime();
        jQuery.ajax({
            type: "GET",
            url: a,
            cache: false,
            dataFilter: function(i, k){
                var j = i.toString();
                var l = j.replace(/\$\{cmsroot}/gi, cmsRootJs);
                return l
            },
            dataType: "html",
            success: function(i){
                jQuery(b).parent().append(f);
                jQuery(f).html(i);
                jQuery(f).find("a.toClick").addClass("menuCog");
                jQuery(f).find("a.toClick").click(function(){
                    sendOmnitureData(d.omniture)
                });
                var j = jQuery("<div class='menu_seperator'> </div>");
                jQuery(f).before(j);
                jQuery(f).after('<div class="bmg_clear"></div>');
                return false
            },
            error: function(i){
            }
        })
    }
}

function bmg_insertHyperlink(g, d, c, f, a, h){
    var s = location.protocol + "//" + location.host;/*var w=location.protocol+"//www.qwest.com";*/
    var w = location.protocol + "//" + location.host;
    if (typeof d == "string") {
        if (d.indexOf("http") == -1) {
            //d = (w + d)
        }
        else {
            var u = d.indexOf("//");
            var t = d.indexOf("/", (u + 2));
            var v = d.slice(0, (t));
            //d = d.indexOf("qcontrol") != -1 ? d.replace(v, "http://www.qwest.com") : d
        }
    }
    var e = g;
    e == null ? e = "" : "";
    var b = c == "true" ? "newWindow" : "";
    if (b == "" && (f == "true" || f == true)) {
        b = "thickbox";
        var i = b == "thickbox" ? "onclick=tb_open_new('" + d + "')" : "";
        d = d + ((d.indexOf("width=") == -1) ? (((d.indexOf("?") == -1) ? "?" : "") + "&width=" + (a == undefined ? defWidth : a)) : "") + ((d.indexOf("height=") == -1) ? (((d.indexOf("?") == -1) ? "?" : "") + "&height=" + (h == undefined ? defHeight : h)) : "")
    }
    if (d != "" && d != null && d != "undefined" && d != "null") {
        e = '<a href="' + d + '" class="menuCog ' + b + '">' + (g != null ? g = g : "Error retrieving link text") + "</a>"
    }
    return e
}

function bmg_insertIcon(a, c){
    var b = "";
    if (a != "" && a != null && a != "undefined" && a != "null") {
        b = '<span class="" style="display:inline-block;width: 25px; height: 25px; background-image: url(' + a + ');background-repeat: no-repeat"></span>'
    }
    return b
}

function isModalWin(a){
    tb_open_new($(a).attr("href"))
}

function bmg_setTimer(d){
    var c = "";
    c = new Date();
    var b = c.getTime();
    var a = (b - startSeconds) * 0.001;
    return a
}

function bmg_userLogin(d){
    if (d == "true" || d == true) {
        var b = userType == "BMG" || userType == "" || userType == undefined ? true : false;
		log.info("bmg_userLogin","JS: UserType: "+userType,5);
		log.info("bmg_userLogin","JS: b: "+b,5);
		
		log.info("bmg_userLogin","JS: isStatic: "+(typeof isStatic == "undefined")?'undefined':isStatic,5);
		log.info("bmg_userLogin","b && (typeof isStatic == 'undefined'):"+(b && (typeof isStatic == "undefined")),5)
		
        try {
            if (b && (typeof isStatic == "undefined")) {
                menuObj.menus.menu.menuList[3].menuItemHead = {
                    menuItems: {
                        menuItem: [{
                            text: "QControl Dashboard",
                            link: rootEnv + "/business/appmanager/qwest/qcontrolHome?_nfpb=true&_pageLabel=Home_Page&_portlet.async=false"
                        }, {
                            text: "Quick Launch",
                            link: rootEnv + "/business/framework/resources/QuickLaunchDisplay.jsp?modal=true&goTo=false&inlineId=quicklaunch1&goTo=false",
                            isModal: true
                        }]
                    }
                };
                bmg_buildChannelNav(true)
            }
            else {
                if (userType == "WHSL") {
                    var c = "Wholesale";
                    menuObj.menus.menu = {
                        menuList: [{
                            id: "01",
                            text: "Products & Services",
                            isSelected: "false",
                            link: "http://www.qwest.com/wholesale/pcat/index.html"
                        }, {
                            id: "04",
                            text: "Customer Service",
                            isSelected: "false",
                            link: "http://www.qwest.com/wholesale/customerservice.html"
                        }, {
                            id: "05",
                            text: "Manage My Account",
                            isSelected: "false",
                            link: "http://www.qwest.com/wholesale/tools/managemyaccount.html",
                            menuWidth: "155",
                            menuHeight: null
                        }]
                    };
                    if ((typeof isStatic == "undefined")) {
                        menuObj.menus.menu.menuList[2].menuItemHead = {
                            menuItems: {
                                menuItem: [{
                                    text: "QControl Dashboard",
                                    link: rootEnv + "/business/appmanager/qwest/qcontrolHome?_nfpb=true&_pageLabel=Home_Page&_portlet.async=false"
                                }, {
                                    text: "Quick Launch",
                                    link: rootEnv + "/business/framework/resources/QuickLaunchDisplay.jsp?&inlineId=quicklaunch1&modal=true&goTo=false",
                                    isModal: true
                                }]
                            }
                        }
                    }
                    bmg_buildChannelNav(true);
					//bmg_buildTopNav(true);
                }
            }
        } 
        catch (a) {
			
			log.error("bmg_userLogin","Error in updating menu obj: "+a.message);
        }
    }
}

function positionMenu(a){
    var b = 0;
    $("div.bmg_main_nav ul").each(function(){
        b += $(this).width()
    });
    $("div.bmg_main_nav:first").css({
        left: ($(".bmg_head_right").length >= 1 ? ($("div.bmg_head").length >= 1 ? ($("div.bmg_head").width() + $("div.bmg_head").position().left) : ($("div.bmg_flashHead").width() + $("div.bmg_flashHead").position().left)) - b : ($("div.bmg_head_ch").width() + $("div.bmg_head_ch").position().left) - b),
        top: (($(".bmg_head_right").length >= 1 ? $(".bmg_head_right").offset().top : $(".bmg_head_ch_right").offset().top) + ($(".bmg_head_right").length >= 1 ? ($(".bmg_head_right").outerHeight() - 1) : $(".bmg_head_ch_right").outerHeight()) + (33)),
        width: b
    })
}

function getVal(a){
    return window[a]
}

function reOrderMenuItems(b, a){
    if (b) {
        var c = 0;
        jQuery("div.subMenuMega").each(function(d, f){
            var e = 0;
            jQuery(f).find("div.col").each(function(g, h){
                jQuery(this).find("dl.eachBlock").each(function(k, j){
                    e += $(this).outerHeight();
                    if (e > defaultMegaHeight) {
                        var i = jQuery("<div class='menu_seperator'> </div> <div class='col col2'></div>");
                        jQuery(h).after(i);
                        var l = $(this).nextAll("dl");
                        jQuery(f).find("div.col2:last").append(l);
                        jQuery(f).find("div.col2:last").prepend($(this));
                        return false;
                    }
                })
            })
        })
    }
}

jQuery(document).ready(function(){
	try {
		if (isCrossDomain || (typeof isCrossDomain != 'undefined')) {
			var c = document.getElementsByTagName("head")[0];
        	var b = document.createElement("meta");
        	b.httpEquiv = "X-UA-Compatible";
        	b.content = "IE=EmulateIE7";
        	c.appendChild(b)
		}
    }
    catch (a) {
        getMenuXML()
    }
    bmg_buildChannelNav(true);
    bmg_buildTopNav(true);
    if (keyboardEnable) {
        jQuery("a,input,img,select,object,body").focus(function(){
            if (!($(this).hasClass("menuCog"))) {
                bmg_getSetDimensions("", false);
                $("div.bmg_main_nav").find("li").removeClass("menuOver");
            }
        })
    }
    positionMenu(true);
    $(window).resize(function(){
        positionMenu(true)
    });
    reOrderMenuItems(true, "")
});

