﻿var curPath = "";
function navigateTo(path) {
    byClick = true;
    if ((path) && (curPath != path)) {

        var onUnLoad = getOnUnLoadFor(curPath);
        if (onUnLoad) {
            var f = new Function(onUnLoad);
            f();
        }

        loadTopMenu(path, function() {
            loadSideMenuFromPath(path, function() {
                loadTemplateFromPath(path, function() {
                    curPath = path;
                });
            });
        });

    }

}


// --------------------------------------------------------------------------------
// -                               MAIN MENU CODE                                 -
// --------------------------------------------------------------------------------
function loadTopMenu(path, callback) {

    //globServices["MenuService"].GetMainMenuItems(loadMenuCallback, loadMenuError);
    globServices["MenuService"].GetMenuItemsFromPath("/",
        function(result) {
            loadMenuCallback(result, path);
            //loadSideMenuFromPath(path);
            if (callback) {
                callback();
            }
        }, loadMenuError);

}

function loadMenuCallback(result, path) {

    $("#topMenu").html("");

    var arr = new Array();
    arr.push("<table ");
    arr.push("cellpadding=\"0\" ");
    arr.push("cellspacing=\"0\" ");
    arr.push("width=\"564px;\" ");
    //arr.push("border=\"1\" ");
    arr.push(">");

    //code that inserts the notches (9 x 37)
    arr.push("<tr>");
    for (var i = 0; i < result.length; i++) {
        arr.push("<td ");
        arr.push("align=\"center\" ");
        arr.push("valign=\"top\" ");
        arr.push("height=\"9px\" ");
        arr.push(">");
        arr.push("<div ");
        arr.push("class=\"menuNotchOff\" ");
        arr.push("id=\"topMenu" + i + "Notch\" ");
        arr.push("notch=\"" + result[i]._path + "\" ");
        arr.push("/>");
        arr.push("</td>");
        if (i < result.length - 1) {
            arr.push("<td></td>");
        }
    }
    arr.push("</tr>");
    //code that inserts the menu items
    arr.push("<tr>");

    for (var i = 0; i < result.length; i++) {
        arr.push("<td ");
        arr.push("id=\"topMenu" + i + "\" ");
        arr.push("tabindex=\"0\" ");
        arr.push("path=\"" + result[i]._path + "\" ");
        arr.push("class=\"menuLevel" + result[i]._level + "\" ");
        //arr.push("onclick=\"loadTemplateFromPath('" + result[i]._path + "',this); ");
        //arr.push("loadSideMenuFromPath('" + result[i]._path + "');\" ");
        var onload = "";
        var onunload = "";
        if ((result[i]._onLoad) && (result[i]._onLoad !== "")) {
            onload = result[i]._onLoad;
            arr.push("templateOnLoad=\"" + onload + "\" ");
        }
        if ((result[i]._onUnload) && (result[i]._onUnload !== "")) {
            onunload = result[i]._onUnload;
            arr.push("templateOnUnLoad=\"" + onunload + "\" ");
        }
        if (result[i]._altPath) {
            arr.push("onclick=\"topMenuClick('" + result[i]._altPath + "');\" ");
        } else {
            arr.push("onclick=\"topMenuClick('" + result[i]._path + "');\" ");
        }

//        if (result[i]._altPath) {
//            arr.push("altPath=\"" + result[i]._altPath + "\" ");
//        }

        arr.push(">");
        arr.push(result[i]._title);
        arr.push("</td>");
        //seperator dimensions (1 X 24)
        if (i < result.length - 1) {
            arr.push("<td ");
            arr.push("style=\"width: 1px;\" ");
            arr.push("valign=\"middle\" ");
            arr.push("align=\"center\" ");
            arr.push(">");
            arr.push("<div ");
            arr.push("style=\"width: 1px; height: 24px; background-color: White;\" ");
            arr.push("/>");
            arr.push("</td>");
        }

    }

    arr.push("</tr>");
    arr.push("</td>");

    container = arr.join("");

    $("#topMenu").append(container);

    $(".menuLevel0").hover(onHover, onHoverOut);
    $(".menuLevel0").click(doSelection);

    setMenuSelection(path);


}

function loadMenuError(result) {

    alert("Error Loading Menu");

}

function onHover() {
    if ((this.isSelected) && (this.isSelected == true)) {
    } else {
        $("#" + this.id + "Notch").stop().animate({ backgroundColor: menuHoverNotch }, 500);

    }
}
function onHoverOut() {
    if (this.isSelected) {
        if (this.isSelected == true) {
        } else {
            $("#" + this.id + "Notch").stop().animate({ backgroundColor: menuNotch }, 500);
        }
    } else {
        $("#" + this.id + "Notch").stop().animate({ backgroundColor: menuNotch }, 500);
    }
}
function doSelection() {
    clearMenuSelection();
    var thisNotch = $("#" + this.id + "Notch");
    thisNotch.stop();
    thisNotch.css("background-color", "White");
    this.isSelected = true;
}

function clearMenuSelection() {
    var menuItems = $(".menuLevel0");
    for (var i = 0; i < menuItems.size(); i++) {
        var el = menuItems[i];
        var notch = $("#" + el.id + "Notch");
        notch.stop();
        notch.css("background-color", menuNotch);
        el.isSelected = false;
    }
}

function setMenuSelection(path) {
    var test = path.split("/");
    var toSelect = null;

    if ((test) && (test.length) && (test.length > 1)) {
        toSelect = "/" + test[1];
    }
    if (toSelect) {

        var menuItems = $(".menuLevel0");
        for (var i = 0; i < menuItems.size(); i++) {
            var el = menuItems[i];
            if (unescape(el.path) == toSelect) {
                var notch = $("#" + el.id + "Notch");
                notch.stop();
                notch.css("background-color", "White");

                el.isSelected = true;
            }
        }

    }
}

function topMenuClick(path) {
    navigateTo(path);
}

// --------------------------------------------------------------------------------
// -                               SIDE MENU CODE                                 -
// --------------------------------------------------------------------------------

function loadSideMenuFromPath(path, callback) {
    var sideMenu = $("#sideMenu");
    //alert(path);
    if (isDifferentPath(curPath, path) == true) {

        var displayHeader = getByLevel(path, 1);
        displayHeader = displayHeader.toUpperCase();
        if (displayHeader != "HOME") {
            $("#sideMenuHeader").html(displayHeader);
        } else {
            $("#sideMenuHeader").html("");
        }

        var sideMenu = $("#sideMenu");
        var att = sideMenu.attr("alternateMenu");
        var sdMainDisp = $("#sideMain").css("display");
        var sdMenuDisp = sideMenu.css("display");

        if (!path.match(/home/)) {
            sideMenu.attr("curPath", path);

            if ((sdMainDisp == "none") && (sdMenuDisp == "none")) {
                sdMenuDisp = "inline";
            }
        }

        if ((sdMainDisp == "none") && ((!sdMenuDisp) || (sdMenuDisp != "none"))) {
            sideMenu.slideUp("slow", function() {
                $("#sideMenu").css("display", "none");
                globServices["MenuService"].GetMenuItemsFromPath(path,
                function(n) {
                    loadSideMenuCallback(n);
                    sideMenuSetSelection(path);
                }, loadMenuError);
            });

        }
    } else {
        var arrDisplayHeader = curPath.split("/");
        var displayHeader = "";
        if (arrDisplayHeader) {
            if (arrDisplayHeader.length) {
                if (arrDisplayHeader.length == 1) {
                    displayHeader = arrDisplayHeader[0].toUpperCase();
                } else {
                    displayHeader = arrDisplayHeader[1].toUpperCase();
                }
            }
        }
        $("#sideMenuHeader").html(displayHeader);
        sideMenuSetSelection(path);
    }
    if (callback) {
        callback();
    }
}

function loadSideMenuCallback(result) {

    var sideMenu = $("#sideMenu");
    var sideMain = $("#sideMain");
    var smDisplay = sideMain.css("display");
    //sideMenu.html("");
    sideMenu.empty();
    var att = sideMenu.attr("alternateMenu");
    if (((!att) || (att == "false")) && ((!smDisplay) || (smDisplay == "none"))) {
        var arr = new Array();

        var minRows = 9;

        //selected menu color = 590c01
        //bullet color = 74c2be
        //bullet dimensions =  7 high x 10 wide- pixels
        //min height for menu = 305px
        //unselected background #8f340b
        //arr.push("<div ");
        //arr.push("style=\"min-height: 305px; background-color: #8f340b; width: 100px;\" "); //297
        arr.push("<table ");
        arr.push("cellpadding=\"0\" ");
        arr.push("cellspacing=\"0\" ");
        arr.push("style=\"min-height: 297px;\" ");
        arr.push("width=\"297px\" ");
        arr.push(">");
        var rowsAdded = 0;
        for (var i = 0; i < result.length; i++) {
            if (result[i]._level == 1) {
                arr.push("<tr ");
                arr.push("height=\"33px\" ");
                arr.push(">");
                arr.push("<td ");
                arr.push("width=\"31px\" ");
                arr.push("style=\"background-color: #9f3a0d;\" ");
                arr.push("sideMenu=\"" + result[i]._path + "\" ");
                arr.push("menuNum=\"" + i + "\" ");
                arr.push("id=\"sideNibble" + i + "\" ");
                arr.push(">");
                arr.push("</td>");
                arr.push("<td ");
                arr.push("width=\"266px\" ");
                //arr.push("style=\"background-color: #8f340b;\" ");
                arr.push("menuNum=\"" + i + "\" ");
                arr.push("sideMenu=\"" + result[i]._path + "\" ");
                arr.push("style=\"cursor: pointer; background-color: #8f340b;\" ");
                arr.push("onclick=\"sideMenuClick('" + result[i]._path + "');\" ");
                arr.push("tabindex=\"0\" ");
                arr.push(">");

                arr.push("<table ");
                arr.push("cellpadding=\"0\" ");
                arr.push("cellspacing=\"0\" ");
                arr.push("width=\"266px\" ");
                arr.push("height=\"33px\" ");
                //arr.push("menuNum=\"" + i + "\" ");
                //arr.push("sideMenu=\"" + result[i]._path + "\" ");
                //arr.push("style=\"cursor: pointer;\" ");
                //arr.push("onclick=\"sideMenuClick('" + result[i]._path + "');\" ");
                arr.push(">");
                arr.push("<tr ");
                arr.push("height=\"33px\" ");
                arr.push(">");
                arr.push("<td ");
                arr.push("width=\"15px\" ");
                arr.push("valign=\"center\" ");
                arr.push(">");
                arr.push("<div ");
                arr.push("style=\"width: 12px; height: 7px; background-color: #74c2be; margin-left: 3px;\" ");
                arr.push(">");
                arr.push("</div>");
                arr.push("</td>");
                arr.push("<td ");
                arr.push("width=\"243px\" ");
                arr.push("style=\"color: White; font-size: 12pt; padding-left:5px;\" ");
                arr.push(">");
                arr.push(result[i]._title);
                arr.push("</td>");
                arr.push("</tr>");
                arr.push("</table>");

                arr.push("</td>");
                arr.push("</tr>");
                rowsAdded++;
            }
        }

        while (rowsAdded <= minRows) {
            arr.push("<tr height=\"33px\">");
            arr.push("<td ");
            arr.push("width=\"31px\" ");
            arr.push("style=\"background-color: #9f3a0d;\" ");
            arr.push(">");
            arr.push("</td>");
            arr.push("<td ");
            arr.push("width=\"266px\" ");
            arr.push("style=\"background-color: #8f340b;\" ");
            arr.push(">");
            arr.push("</td>");
            arr.push("</tr>");
            rowsAdded++;
        }

        arr.push("</table>");

        sideMenu.append(arr.join(""));
        //sideMenu.css("display", "inline");
        sideMenu.slideDown(1000);

    }

}

function loadSideMenuError(result) {

    alert("Error Load Side Menu");

}

function sideMenuClick(path) {
    var prevElements = $("*[sideSelected=true]");
    if (prevElements.length > 0) {

        prevElements.stop().animate({ backgroundColor: "#8f340b" }, 500);

        if (prevElements[0].sideMenu) {
            var sideNibble = $("#sideNibble" + prevElements[0].menuNum);
            sideNibble.stop().animate({ backgroundColor: "#9f3a0d" }, 500);
        }

        prevElements.attr("sideSelected", "false");

    }
    var curElements = $("*[sideMenu=" + path + "]");
    curElements.attr("sideSelected", "true");
    curElements.stop().animate({ backgroundColor: "#590c01" }, 500);
    loadTemplateFromPath(path);
}

function sideMenuSetSelection(path) {
    path = chopPath(path, 2);
    var prevElements = $("*[sideSelected=true]");
    if (prevElements.length > 0) {

        prevElements.stop().animate({ backgroundColor: "#8f340b" }, 500);

        if (prevElements[0].sideMenu) {
            var sideNibble = $("#sideNibble" + prevElements[0].menuNum);
            sideNibble.stop().animate({ backgroundColor: "#9f3a0d" }, 500);
        }

        prevElements.attr("sideSelected", "false");

    }
    var curElements = $("*[sideMenu=" + path + "]");
    curElements.attr("sideSelected", "true");
    curElements.stop().animate({ backgroundColor: "#590c01" }, 500);
}

// --------------------------------------------------------------------------------
// -                                   HELPERS                                    -
// --------------------------------------------------------------------------------
function getByLevel(path, level) {

    var out = "";

    if ((path) && (level)) {
        var arr = path.split("/");
    }

    var arr = path.split("/");

    if ((arr) && (arr.length > level)) {
        out = arr[level];
    }

    return out;

}

function isDifferentPath(origPath, curPath) {

    var bol = true;

    origPath = chopPath(origPath, 2);
    curPath = chopPath(curPath, 2);

    if ((curPath) && (origPath)) {

        var arrCur = curPath.split("/");
        var arrOrig = origPath.split("/");

        if ((arrCur) && (arrOrig) && (arrCur.length > 1) && (arrOrig.length > 1)) {

            if ((arrCur[0] == arrOrig[0]) && (arrCur[1] == arrOrig[1])) {

                bol = false;

            }

        }

    }

    return bol;

}

function chopPath(path, level) {

    var out = path;

    if ((path) && (level)) {
        var arrPath = path.split("/")

        if (arrPath) {
            var chopped = "";
            var curLevel = 0;
            for (var i = 0; i < arrPath.length; i++) {
                if ((arrPath[i]) && (arrPath[i] != "")) {
                    if (curLevel < level) {

                        chopped = chopped + "/" + arrPath[i];

                        curLevel++;
                    }
                }
            }
            out = chopped;

        }

    }

    return out
}
