//in case the other script isn't running.
if (typeof(application_root_url) == "undefined") application_root_url = "/";
fixMozillaZIndex = true;
//Fixes Z-Index problem with Mozilla browsers but causes odd scrolling problem, toggle to see if it helps
_menuCloseDelay = 500;
_menuOpenDelay = 250;
_subOffsetTop = 0;
_subOffsetLeft = 0;
horizontalMenuDelay = "0";

//object to create navigation
var newportMenuData = {
    mcount: 0,
    horizStyle: null,
    vertStyle: null,
    allmenus: null,
    log: function (msg) {
        if (typeof(console) != "undefined") {
            //console.log(msg);
        }
    },
    toUnicode: function (theString) {
        var unicodeString = '';
        for (var i = 0; i < theString.length; i++) {
            var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
            while (theUnicode.length < 4) {
                theUnicode = '0' + theUnicode;
            }
            theUnicode = '\\u' + theUnicode;
            unicodeString += theUnicode;
        }
        return unicodeString;
    },
    buildMenu: function (menuNode) {
        this.allmenus = new Array();
        var id = menuNode.getAttribute("ID");
        var mymenuholder = this.getNewMenu(id);
        ////this.log("b1");
        var menuLinks = this.selectNodes(menuNode, "MenuColumn/*");
        for (var j = 0; j < menuLinks.length; j++) {
            this.buildMenuLink(mymenuholder, menuLinks[j], 0);
        }
        this.log("b2");
        this.log(document.referrer);
        this.log("b2x");
        for (var x = 0; x < this.allmenus.length; x++) {
            var tm = this.allmenus[x];
            //console.log(tm.id);
            var item;
            item = null;
            var item = new menuname(tm.id);
            var dodefault = true;
            var percolumn = 10;
            with(item) {
                orientation = "vertical";
                style = this.vertStyle;
                margin = "10px 10px 10px 10px";
                maxwidth = 220;
                menuwidth = 210;
                rawcss = 'font-face:Courier New';
                if (tm.items.length > percolumn) {
                    //this.log("length=" + tm.items.length);
                    var j0 = Math.floor(tm.items.length / percolumn)
                    //this.log("div=" + j0);
                    if (window.location.search.substring(1).indexOf("div") >= 0) {
                        dodefault = false;
                        divides = j0 + 1;
                    }
                }
            }

            //            var tstdivide = item.divides;
            //            if (typeof (tstdivide) == "undefined") {
            //            }
            //            else {
            //            }
            if (dodefault) {
                for (var y = 0; y < tm.items.length; y++) {
                    item.aI(tm.items[y]);
                }
            } else {

                var split = Math.floor(tm.items.length / (item.divides));
                //this.log(split);
                //                if (split >1) {
                for (var y = 0; y < split; y++) {
                    for (var x = 0; x < (item.divides); x++) {
                        var test = x * split + y;
                        //this.log("test=" + test);
                        if (test < tm.items.length) {
                            //this.log('adding');
                            item.aI(tm.items[test].replace("text=", "text=" + test + "." + x + "." + y + "."));
                            //                            //                        }
                            //                            //                    }
                            //                            //                }
                        }
                    }
                }
            }
        }
        ////this.log("b3");
    },
    selectNodes: function (src, xpath) {
        ////this.log("s1");
        //return src.selectNodes(xpath);
        if (typeof ActiveXObject != "undefined") {
            ////this.log("s1.1");
            return src.selectNodes(xpath);
        } else {
            ////this.log("s1.15");
            var evaluator = new XPathEvaluator();
            var result = new Array();
            var resultIterator;
            resultIterator = evaluator.evaluate(xpath, src, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            if (resultIterator != null) {
                while (resultNode = resultIterator.iterateNext()) {
                    result.push(resultNode);
                }
            }
            ////this.log("s1.2");
            return result;
        }

    },
    nulltoblank: function (x) {
        if (x == null) {
            return "";
        } else {
            return x;
        }
    },
    testAppend: function (attr, appendas) {
        if (attr != null) {
            if (appendas == "url") {
                attr =attr.replace("!/",search_url)
                return appendas + "=`" + attr.replace("~/", application_root_url) + "`;";
            } else return appendas + "=`" + attr + "`;";
        } else return "";
    },
    nextMenuId: function () {
        var id = "menu" + this.mcount;
        this.mcount++;
        return id;
    },
    getNewMenu: function (id) {
        if (typeof(id) == "undefined") {
            id = this.nextMenuId();
        } else if (id == null) {
            id = this.nextMenuId();
        }
        var mymenuholder = {
            id: id,
            items: new Array()
        };
        this.allmenus.push(mymenuholder);
        return mymenuholder;
    },
    getNodeText: function (anyNode) {
        //return 'testdata';
        //if(typeof(anyNode.nodeValue)!="undefined") this.log(anyNode.nodeValue);
        //this.log(anyNode.childNodes.length + " " + typeof (anyNode.textContent) + " " + typeof (anyNode.innerText) + " " + typeof(anyNode.nodeValue))
        if (anyNode.childNodes.length == 1) {
            return anyNode.childNodes[0].nodeValue;
        } else {
            return '';
        }
    },
    buildMenuLink: function (currentItem, menuLinkNode, level) {
        var debug = level > 0 ? true : false;
        var b2 = null;
        var buildit = null;
        //console.log(menuLinkNode.nodeName);
        //this.log(menuLinkNode.nodeName);
        switch (menuLinkNode.nodeName) {
		case "MenuNoLink":
			buildit = "offclass=milonic_no_link;" +  this.testAppend(menuLinkNode.getAttribute("Text"), "text") + "oncolor=#000;offcolor=#000;rawcss=`margin-left:5px`";
            currentItem.items.push(buildit);
		break;
		case "MenuEmailLink":
		    var menulinks = this.selectNodes(menuLinkNode, "*");
            if (menulinks.length > 0) {
                var newmenu = this.getNewMenu();
                ////this.log("bl2");
                newmenu.level = level + 1;
                newmenu.parent = currentItem;
                newmenu.parentname = currentItem.id;
                for (var cc = 0; cc < menulinks.length; cc++) {
                    this.buildMenuLink(newmenu, menulinks[cc], level + 1);
                }
                ////this.log("bl3");
                //console.log(newmenu);
                // b2 = "offclass=milonic_link_withchildren;onclass=milonic_link_withchildren_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";showmenu=" + newmenu.id + ";rawcss=`margin-left:5px;`";
                b2 = "offclass=milonic_email_link;onclass=milonic_email_link_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";" + this.testAppend(menuLinkNode.getAttribute("NavigateUrl"), "url") + ";showmenu=" + newmenu.id + ";rawcss=`margin-left:5px;`";
                //console.log(b2);
                newmenu.text = b2;
                currentItem.items.push(b2);
                ////this.log("bl4");
            } else {
                buildit = "offclass=milonic_email_link;onclass=milonic_email_link_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";" + this.testAppend(menuLinkNode.getAttribute("NavigateUrl"), "url") + ";rawcss=`margin-left:5px;`";
                currentItem.items.push(buildit);
            }
            break;
		break;
        case "MenuLink":
            ////this.log("bl1.1");
            var menulinks = this.selectNodes(menuLinkNode, "*");
            if (menulinks.length > 0) {
                var newmenu = this.getNewMenu();
                ////this.log("bl2");
                newmenu.level = level + 1;
                newmenu.parent = currentItem;
                newmenu.parentname = currentItem.id;
                for (var cc = 0; cc < menulinks.length; cc++) {
                    this.buildMenuLink(newmenu, menulinks[cc], level + 1);
                }
                ////this.log("bl3");
                //console.log(newmenu);
                // b2 = "offclass=milonic_link_withchildren;onclass=milonic_link_withchildren_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";showmenu=" + newmenu.id + ";rawcss=`margin-left:5px;`";
                b2 = "offclass=milonic_link_withchildren;onclass=milonic_link_withchildren_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";" + this.testAppend(menuLinkNode.getAttribute("NavigateUrl"), "url") + ";showmenu=" + newmenu.id + ";rawcss=`margin-left:5px;`";
                //console.log(b2);
                newmenu.text = b2;
                currentItem.items.push(b2);
                ////this.log("bl4");
            } else {
                buildit = "onclass=milonic_link_nochildren_rollover;text=" + this.nulltoblank(menuLinkNode.getAttribute("Text")) + ";" + this.testAppend(menuLinkNode.getAttribute("NavigateUrl"), "url") + ";rawcss=`margin-left:5px;`";
                currentItem.items.push(buildit);
            }
            break;
            // case "MenuText":
            //     buildit = "offclass=milbase milentry;onclass=milbase milentry milentryon;text=`<div>" + this.getNodeText(menuLinkNode).replace("\n", "").replace("&", "") + "</div>`;rawcss=`margin-left:5px;white-space:normal;`" // ;rawcss=`margin-left:5px;background-color:" + color + "`";
            //     currentItem.items.push(buildit);
            //     //this.log(currentItem.id);
            //     //this.log(buildit);
            //     break;
        case "MenuHeading":
            //this.log(menuLinkNode.getAttribute("Text"));
            buildit = "offclass=milonic_header;" + this.testAppend(menuLinkNode.getAttribute("Text"), "text") + "oncolor=#000;offcolor=#000;rawcss=`padding-bottom:8px;padding-top:15px;margin-bottom:6px;vertical-align:middle;border:none; border-bottom:dashed 1px #666666;`";
            currentItem.items.push(buildit);
            //this.log(buildit);
            break;
        }
    },
    getInternetExplorerVersion: function ()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null) rv = parseFloat(RegExp.$1);
        }
        return rv;
    },


    main: function () {
        ////this.log("a");
        this.log(window.location.search.substring(1) == "undefined");
        with(this.vertStyle = new mm_style()) {
            overflow = "scroll";
            styleid = 1;
            bordercolor = "#000";
            borderstyle = "solid";
            borderwidth = 1;
            //style="miltest";
            //onclass =  "milbase milentry milentryon";
            offclass = "milonic_link_nochildren";
            //offbgcolor = "transparent";
            //offcolor = "#000000";
            url = "";
            width = 220;
            onsubimage = application_root_url + "common/images/nav/submenu-pointer-over.png";
            //outfilter = "randomdissolve(duration=0.2)";
            //overfilter = "Fade(duration=0.1);Alpha(opacity=95);Shadow(color=#777777', Direction=135, Strength=3)";
            padding = 0;
            //pageclass='miltest';
            subimagecss = "padding-left:5px";
            image = "";
            //divides = 2;
            subimage = application_root_url + "common/images/nav/submenu-pointer.png";
            target = "_top";
            valign = "middle";
            menubgcolor = "#FFFFFF";
			color= "#000000";
            //rawcss = "padding:10px;max-width:180px";
        }
        ////this.log("b");
        with(this.horizStyle = new mm_style()) {
            bordercolor = "#999999";
            borderstyle = "none";
            borderwidth = 1;
            fontstyle = "normal";
            separatorcolor = "#bbbbbb";
            offbgcolor = "#f0f0f0";
            separatorsize = 1;
            //            menuheight=28;
            if (this.getInternetExplorerVersion() != -1) {
                itemheight = 29;
            } else itemheight = 27;
        }
        ////this.log("c");
        var namespaceURL = "";
        var rootTagName = "";
        var doc = null;
        //console.log(menuitems_xml);
        menuitems_xml = menuitems_xml.replace("~/", application_root_url);
        if (typeof DOMParser != "undefined") {
            // Mozilla, Firefox, and related browsers
            doc = (new DOMParser()).parseFromString(menuitems_xml, "application/xml");
        } else if (typeof ActiveXObject != "undefined") {
            // Internet Explorer.
            var doc = new ActiveXObject("MsXml.DOMDocument");
            // Create an empty document
            doc.loadXML(menuitems_xml);
            // Parse text into it
        }
        this.allmenus = new Array();
        ////this.log("d");
        var menus = this.selectNodes(doc, "/Menus/Menu");
        for (var i = 0; i < menus.length; i++) {
            this.buildMenu(menus[i]);
        }
        ////this.log("e");
        with(mainmenu = new menuname("Main Menu")) {
            alwaysvisible = 1;
            orientation = "horizontal";
            style = this.horizStyle;

            aI("showmenu=megamenu1;image={0}common/quantum/img/gnav-btn-products.png;overimage={0}common/quantum/img/gnav-btn-products-over.png;url=#;".format(application_root_url));
            aI("showmenu=megamenu2;image={0}common/quantum/img/gnav-btn-solutions.png;overimage={0}common/quantum/img/gnav-btn-solutions-over.png;url=#;".format(application_root_url));
            aI("showmenu=megamenu3;image={0}common/quantum/img/gnav-btn-resourcesmedia.png;overimage={0}common/quantum/img/gnav-btn-resourcesmedia-over.png;url=#;".format(application_root_url));
            aI("showmenu=megamenu4;image={0}common/quantum/img/gnav-btn-salessupport.png;overimage={0}common/quantum/img/gnav-btn-salessupport-over.png;url=#;".format(application_root_url));
            aI("showmenu=megamenu5;image={0}common/quantum/img/gnav-btn-company.png;overimage={0}common/quantum/img/gnav-btn-company-over.png;url=#;".format(application_root_url));



              
        }
        ////this.log("f");
        doc = null;
    }
};
newportMenuData.main();
drawMenus();
