﻿
//in case the other script isn't running.
if (typeof (Newport) == "undefined") {
    var Newport = { application_root_url: "/" };
} else if (typeof (Newport.application_root_url) == "undefined") {
    Newport.application_root_url = "/";
}
    


function getNodeText(anyNode) {
    //alert(anyNode.childNodes.length + " " + typeof (anyNode.textContent) + " " + typeof (anyNode.innerText))
    if (anyNode.childNodes.length == 1) {
        return anyNode.childNodes[0].nodeValue;
    }
    else
        return '';
}

function setHtmlText(anyNode, text) {
    anyNode.innerHTML = text;
}

function copyAttribute(fromElement, fromAttribute, toElement, toAttribute) {
    var test = fromElement.getAttribute(fromAttribute) ;
    if (test != null) {
        toElement.setAttribute(toAttribute, test);
    }
}

function insertHref(parentHTML, activeNode) {
    var href;
    
    if (activeNode.getAttribute("NavigateURL") != null)
        href = document.createElement("a");
    else
        href = document.createElement("span");
        
    parentHTML.appendChild(href);
    copyAttribute(activeNode, "NavigateURL", href, "href");
    var test = href.getAttribute("href");
    if (test!=null && test.substring(0,2) == "~/" && typeof(Newport.application_root_url) !="undefined"){
        href.href = Newport.application_root_url + test.substring(2);
    }
//    if (typeof (Newport.application_root_url) != "undefined" && typeof (activeNode.href) != "undefined") {
//        activeNode.href = "#";
//        alert("1");
//    }
//    else
//        alert("0");
    copyAttribute(activeNode, "CssClass", href, "class");
    copyAttribute(activeNode, "Style", href, "style");
    copyAttribute(activeNode, "ID", href, "id");
    setHtmlText(href, activeNode.getAttribute("Text"));
}

function insertChildItems(container, containerNode) {

    var listelement = null;
    for (var i = 0; i < containerNode.childNodes.length; i++) {
        var childItemNode = containerNode.childNodes[i];
        switch (childItemNode.nodeName) {
            case "MenuLink":
                if (listelement == null) {
                    listelement = document.createElement("ul");
                    container.appendChild(listelement);
                }
                var li = document.createElement("li");
                insertHref(li, childItemNode);
                listelement.appendChild(li);
                insertChildItems(li, childItemNode);
                break;
            case "MenuHeading":
                var h3 = document.createElement("h3");
                insertHref(h3, childItemNode);
//                setHtmlText(h3, childItemNode.getAttribute("Text"))
                container.appendChild(h3);
                listelement = null;
                break;
            case "MenuText":
                var span = document.createElement("span");
                //alert(getNodeText(childItemNode));
                setHtmlText(span, getNodeText(childItemNode));
                //setHtmlText(span, "&lt;&gt;");
                container.appendChild(span);
                listelement = null;
                break;
        }
    }
}

function buildColumn(menuColumn) {
    var columndiv = document.createElement("div");
 //   alert(menuColumn);
    if (menuColumn.getAttribute("ID")!=null)
        columndiv.setAttribute("id", menuColumn.getAttribute("ID"));
    columndiv.setAttribute("class", "column");
    //for IE6, have to set classNAME.
    columndiv.setAttribute("className", "column");
    insertChildItems(columndiv, menuColumn);


    return columndiv;
}

function buildMenu(menuNode) {
    var div = document.createElement("div");
    if (menuNode.getAttribute("ID") != null) {
        div.setAttribute("id", menuNode.getAttribute("ID"));
       // alert("id set to " + menuNode.getAttribute("ID"));
    }
    //div.setAttribute("display", "none");
    div.style.cssText = "display:none";
    div.setAttribute("class", "megamenu");
    //for IE6, have to set classNAME.
    div.setAttribute("className", "megamenu");
    for (var i = 0; i < menuNode.childNodes.length; i++) {
        var thisnode = menuNode.childNodes[i];
        switch (thisnode.nodeName) {
            case "MenuColumn":
                var result = buildColumn(thisnode);
                div.appendChild(result);
                break;
            case "MenuNewRow":
                var result = document.createElement("br")
                result.style.cssText="clear:left;";
                div.appendChild(result);
                break;
        }
    }
    return div;
}

function buildMenus(){
    var namespaceURL = "";
    var rootTagName = "";
    var menu_location = document.getElementById("menu_location");
    var doc = null;
    //alert(menuitems_xml);
    menuitems_xml = menuitems_xml.replace("~/", Newport.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("MSXML2.DOMDocument");   // Create an empty document
        doc.loadXML(menuitems_xml);              //  Parse text into it
    }

    var menuNodes = doc.documentElement.getElementsByTagName("Menu");
    if (menuNodes.length == 0) {
        alert("Invalid document structure");
    }
    else {
        for (var i = 0; i < menuNodes.length; i++) {
            result = buildMenu(menuNodes[i]);
            document.body.appendChild(result);
            //menu_location.parentNode.insertBefore(result, menu_location.nextSibling);
        }
    }

}
