﻿// *****************************************************************
// **************************** GLOBAL VARIABLES *******************
// *****************************************************************
var selectedMnus = new Array();
var srcWidth = 0;
var mnuCount = 0;
var OverlapDivName = 'hidden1,hidden2,hidden3,hidden4,hidden5,fb_dd1';
var navnavDomain_url = '';
var navnavSecure_url = '';
var navnavQuery_ids = '';
var mnutimeout;
var stayTime = 1000;
// *****************************************************************
// **************************** GENERAL ****************************
// *****************************************************************
//Show or Hide Menu
function showHide(Menu, Action, ImgName, SrsImg) {
    if (SrsImg != '')
        eval('document.' + ImgName + '.src="' + SrsImg + '"');
    if (Action == "Show")
        showMenu(Menu, ImgName, '0', 'below');
    else
        hideMenu(Menu.name, 0);
}

//Show Hide the form elements
function showHideSelection(Action) {
    if (document.forms[0] != null) {
        var currentElement;
        for (var i = 0; i < document.forms[0].length; i++) {
            currentElement = document.forms[0].elements[i];
            if (currentElement.type == "select-one") {
                if ((currentElement.style.visibility != Action) && (currentElement.name.search("cid") == -1) && (currentElement.className != 'donthide')) {
                    if (Action == 'hidden') {
                        currentElement.setAttribute("hiddenByMenu", "true");
                        currentElement.style.visibility = "hidden";
                    }
                    else if (currentElement.getAttribute("hiddenByMenu") == "true") {
                        currentElement.style.visibility = "visible";
                        currentElement.setAttribute("hiddenByMenu", "false");
                    }
                }
            }
        }
    }
}


//Hide Divs
function ShowHideDivs(Action, Level) {
    showHideSelection(Action);
    var Divs = OverlapDivName.split(",")
    /*if (document.getElementById("divCo") != null)
    alert(document.getElementById("divCo").className);*/
    for (var i = 0; i < Divs.length - 1; i++) {
        if (document.getElementById(Divs[i]) != null)
            document.getElementById(Divs[i]).style.visibility = Action;
    }
}

//Is all the menus are hidden
function isAllHidden() {
    for (var i = 0; i < selectedMnus.length; i++) {
        if (document.getElementById(selectedMnus[i].name).style.visibility == 'visible')
            return false;
    }
    return true;
}

//Keep track of active Menus
function isMnuSelected(menu) {
    for (var i = 0; i < selectedMnus.length; i++)
        if (selectedMnus[i].name == menu)
        return true;
    return false;
}

// Get the details of offset elements
function GetDetails(ElementName) {
    if (ElementName != "") {
        var Element = document.getElementById(ElementName);
        var Left = Element.offsetLeft;
        var Top = Element.offsetTop;
        var Width = Element.offsetWidth;
        var Height = Element.offsetHeight;
        while (Element.offsetParent != null) {
            Element = Element.offsetParent;
            Left += Element.offsetLeft;
            Top += Element.offsetTop;
        }
        return new Array(Left, Top, Width, Height);
    }
    else
        return new Array(0, 0, 0, 0);
}

//Caluclate width based on table row size
function CalculateWidth(objMenu, mnuLevel) {
    var mnuWidth = 0;
    var rowWidth = 0;
    for (var i = 0; i < objMenu.menuItems.length; i++) {
        rowWidth = document.getElementById("mnuItem" + objMenu.mnuNumber + mnuLevel + i).offsetWidth;
        if (rowWidth > mnuWidth)
            mnuWidth = rowWidth;
    }
    return mnuWidth;
}


//Show Menus
function showMenu() {

    window.clearTimeout(mnutimeout);

    var args = showMenu.arguments;
    var isAlreadySelected = isMnuSelected(args[0].name);
    var ElementDetails;
    var offsetLeft = 0;
    var offsetTop = 0;
    var offsetIn = 0;
    var winWidth = 1000;
    var mnuLeft = 0;
    var mnuTop = 0;
    var parentObj;

    //Get details of source image
    if (args[1] != "")
        ElementDetails = GetDetails(args[1]);
    else
        ElementDetails = GetDetails(args[0].Src);

    //Get the position of the menu
    if (args[3] != null && (args[3] == 'right' || args[3] == 'above' || args[3] == 'left'))
        args[0].position = args[3];

    //Calculate the width
    if (args[0].Width == null || args[0].Width == '' || args[0].Width <= 0)
        args[0].Width = CalculateWidth(args[0], args[2]);
    //Offset Left	
    if (args[4] != null && args[4] > 0) {
        args[0].offsetLeft = args[4];
        offsetLeft = args[4];
    }
    else if (args[0].offsetLeft != null && args[0].offsetLeft > 0)
        offsetLeft = args[0].offsetLeft;

    //Offset Top
    if (args[5] != null && args[5] > 0) {
        args[0].offsetTop = args[5];
        offsetTop = args[5];
    }
    else if (args[0].offsetTop != null && args[0].offsetTop > 0)
        offsetTop = args[0].offsetTop;


    //Mouse Out Img
    if (args[7] != null && args[7] != "") {
        args[0].mouseOverImg = document.getElementById(args[1]).src;
        args[0].mouseoutImg = args[7];
    }

    if ((document.body.offsetWidth != null) && (document.body.offsetWidth > 0))
        winWidth = document.body.offsetWidth;
    else
        if ((window.Width != null) && (window.Width > 0))
        winWidth = window.innerWidth - 16;

    //If parent menu
    if (args[2] == 0) {

        if (document.getElementById(args[0].parentControl) != null && args[0].parentActiveCss != null && args[0].parentInActiveCss != null)
            document.getElementById(args[0].parentControl).className = args[0].parentActiveCss;

        //Position the menu
        if (args[0].position == 'right') {
            if (args[0].horizontalPos == "left")
                mnuLeft = ElementDetails[0] - args[0].Width;
            else
                mnuLeft = ElementDetails[0] + ElementDetails[2] + offsetLeft - (args[0].offsetIn + offsetIn);
            mnuTop = ElementDetails[1] + offsetTop + args[0].offsetBelow;
        }
        else if (args[0].position == 'left') {
            if (args[0].horizontalPos == "left")
                mnuLeft = ElementDetails[0] - args[0].Width;
            else
                mnuLeft = ElementDetails[0] + ElementDetails[2] + offsetLeft - args[0].offsetIn;
            mnuTop = ElementDetails[1] + offsetTop + args[0].offsetBelow;
        }
        else if (args[0].position == 'above') {
            mnuLeft = ElementDetails[0] + offsetLeft - args[0].offsetIn;
            if (args[6] != null) {
                mnuTop = ElementDetails[1] - args[6];
                args[0].Height = args[6];
            }
            else
                mnuTop = ElementDetails[1] - args[0].Height;
        }
        else {
            mnuLeft = ElementDetails[0] + offsetLeft - args[0].offsetIn;
            mnuTop = ElementDetails[1] + ElementDetails[3] + offsetTop + args[0].offsetBelow;
        }

        if (winWidth < (mnuLeft + args[0].Width))
            mnuLeft = mnuLeft - ((mnuLeft + args[0].Width + 20) - winWidth);

        document.getElementById(args[0].name).style.left = mnuLeft + "px";
        document.getElementById(args[0].name).style.top = mnuTop + "px";

        //If not already selected
        if (!isAlreadySelected) {
            if (args[0].Width != null && args[0].Width > 0)
                srcWidth = args[0].Width;
            else {
                srcWidth = ElementDetails[2];
                args[0].Width = srcWidth;
            }
            collapseMenu(args[0].name, args[2])
        }
    }
    //if sub menu
    else {
        parentObj = args[0];
        for (var i = args[2]; i > 0; i--) {
            while (parentObj.parentMenu != null)
                parentObj = parentObj.parentMenu

            if (parentObj.cssClass != null && parentObj.MouseOverCssClass != null)
                document.getElementById('mnuItem' + parentObj.Menu.mnuNumber + (i - 1) + parentObj.ItemIndex).className = parentObj.MouseOverCssClass;

            parentObj = parentObj.Menu;
        }

        if (document.getElementById(parentObj.parentControl) != null && parentObj.parentActiveCss != null && parentObj.parentInActiveCss != null)
            document.getElementById(parentObj.parentControl).className = parentObj.parentActiveCss;

        //position the menu
        if (args[0].horizontalPos == "left")
            document.getElementById(args[0].name).style.left = ElementDetails[0] - args[0].Width - offsetLeft + args[0].offsetIn + "px";
        else {
            mnuLeft = ElementDetails[0] + args[0].parentMenu.Menu.Width + offsetLeft - args[0].offsetIn
            if (winWidth < (mnuLeft + args[0].Width))
                mnuLeft = ElementDetails[0] - args[0].Width - offsetLeft + args[0].offsetIn;
            document.getElementById(args[0].name).style.left = mnuLeft + "px"; // - args[0].Width; 
        }
        document.getElementById(args[0].name).style.top = ElementDetails[1] + offsetTop + args[0].offsetBelow + "px";
    }

    //if not already selected, then set the src and add it to selected list
    if (!isAlreadySelected) {
        if (args[0].Src == "")
            args[0].Src = args[1];
        selectedMnus[args[2]] = args[0];
    }

    // Make the menu visible
    for (var i = 0; i <= args[2]; i++) {
        document.getElementById(selectedMnus[i].name).style.width = selectedMnus[i].Width + "px";
        document.getElementById(selectedMnus[i].name).style.visibility = 'visible';
    }
    //Keep original image
    if (selectedMnus[0].mouseOverImg != "" && args[2] != 0)
        document.getElementById(selectedMnus[0].Src).src = selectedMnus[0].mouseOverImg;
    //Hide the site Divs
    if (OverlapDivName != '')
        ShowHideDivs('hidden', args[2]);

}

function hideMenu(menu, level) {
    //set the delay
    mnutimeout = window.setTimeout('collapseMenu(\'' + menu + '\',' + level + ')', stayTime);
}

//Hide Menus
function collapseMenu(menu, level) {

    var isAlreadySelected = isMnuSelected(menu);
    var selectedMnu;

    //Hide all the selected menus and change the parent image source (if passes)
    if (selectedMnus[0] != null) {
        if (document.getElementById(selectedMnus[0].parentControl) != null && selectedMnus[0].parentActiveCss != null && selectedMnus[0].parentInActiveCss != null)
            document.getElementById(selectedMnus[0].parentControl).className = selectedMnus[0].parentInActiveCss
    }

    if (selectedMnus.length > 0 && selectedMnus[0].mouseoutImg != "") {
        document.getElementById(selectedMnus[0].Src).src = selectedMnus[0].mouseoutImg;
    }
    for (var i = 0; i < selectedMnus.length; i++) {
        selectedMnu = selectedMnus[i];
        for (var j = 0; j < selectedMnu.menuItems.length; j++) {
            if (selectedMnu.menuItems[j].cssClass != null && selectedMnu.menuItems[j].MouseOverCssClass != null)
                document.getElementById('mnuItem' + selectedMnu.mnuNumber + i + j).className = selectedMnu.menuItems[j].cssClass;
        }
        document.getElementById(selectedMnus[i].name).style.visibility = 'hidden';
    }

    //if from parent level and the menu is not already selected then set the selectedMnus list length to 0
    if (level == 0 && !isAlreadySelected)
        selectedMnus.length = 0;

    //If any site Divs are hidden and no menu is active, then make them visible
    if (OverlapDivName != '' && isAllHidden()) {
        ShowHideDivs('visible', level);
    }
}


// *****************************************************************
// **************************** MENU *******************************
// *****************************************************************

//Add a menu item
function addMenuItem() {
    var args = addMenuItem.arguments;
    var itemLength = 0;
    for (var i = 0; i < args.length; i++) {
        this.menuItems[this.menuItems.length] = args[i];
        args[i].Menu = this;
        args[i].ItemIndex = this.menuItems.length - 1;
    }
}

//Menu object
function menu() {
    this.menuItems = new Array();
    var args = menu.arguments;
    this.name = args[0];
    if (args[1] != null && args[1] != '' && args[1] > 0)
        this.Width = args[1];
    else
        this.Width = srcWidth;
    if (args[2] != null && args[2] != '') this.cssClass = args[2];
    if (args[3] != null && args[3] != '') this.MouseOverCssClass = args[3];
    if (args[4] != null && args[4] != '') this.DefaultItemCssClass = args[4];
    if (args[5] != null && args[5] != '') this.DefaultItemMouseOverCssClass = args[5];
    if (args[6] != null && args[6] != '') this.pop_up_icon = args[6];
    if (args[7] != null && args[7] != '') this.selected_pop_up_icon = args[7];

    if (args[8] != null && args[8] != '' && args[8] != '')
        this.offsetIn = args[8];
    else
        this.offsetIn = 0;

    if (args[9] != null && args[9] != '' && args[9] != '')
        this.offsetBelow = args[9];
    else
        this.offsetBelow = 0;

    if (args[10] != null && args[10] != '') this.horizontalPos = args[10];
    if (args[11] != null) this.parentControl = args[11];
    if (args[12] != null && args[12] != '') this.parentActiveCss = args[12];
    if (args[13] != null && args[13] != '') this.parentInActiveCss = args[13];

    this.position = 'below';
    this.addItem = addMenuItem;
    this.Build = buildMenu;
    this.mnuNumber = ++mnuCount;
    this.Height = 0;
    this.Src = "";
    this.mouseOverImg = "";
    this.mouseoutImg = "";
}

// Build Menu
function buildMenu(mnuLevel) {
    var menuText = '';
    var cssClass;
    var cssHoverClass;
    var re = /\\/gi;
    var reWith = /\\/gi;

    menuText += '<div id="';
    menuText += this.name + '" ';
    if (this.cssClass != null)
        menuText += 'class="' + this.cssClass + '" ';
    menuText += 'style="top:0; left:0;"';
    menuText += 'onMouseOver="showMenu(';
    menuText += this.name + ',\'' + this.Src;
    menuText += '\',' + mnuLevel + ')" onMouseOut="hideMenu(\'' + this.name + '\',' + mnuLevel + ');">';
    menuText += '<table border="0" cellpadding=0 cellspacing=0';
    if (this.Width != null && this.Width > 0)
        menuText += ' width="' + this.Width + '"';
    menuText += '>';
    for (var i = 0; i < this.menuItems.length; i++) {
        cssClass = '';
        cssHoverClass = '';
        if (this.menuItems[i].cssClass != null)
            cssClass = this.menuItems[i].cssClass;
        else if (this.DefaultItemCssClass != null)
            cssClass = this.DefaultItemCssClass;

        if (cssClass != '') {
            if (this.menuItems[i].MouseOverCssClass != null)
                cssHoverClass = this.menuItems[i].MouseOverCssClass;
            else if (this.DefaultItemMouseOverCssClass != null)
                cssHoverClass = this.DefaultItemMouseOverCssClass;
        }

        menuText += '<tr id="mnuItem' + this.mnuNumber + mnuLevel + i + '" ';
        if (cssClass != '')
            menuText += 'class="' + cssClass + '" ';
        if (this.menuItems[i].subMenu != null) {
            menuText += ' onMouseOver=\'';
            menuText += ' collapseMenu("' + this.menuItems[i].subMenu.name + '",' + (parseInt(mnuLevel) + 1) + ');';
            if (this.pop_up_icon != null && this.pop_up_icon != '' && this.selected_pop_up_icon != null && this.selected_pop_up_icon != '')
                menuText += ' document.getElementById("Img' + this.mnuNumber + mnuLevel + i + '").src = "' + new String(this.selected_pop_up_icon).replace("\\", "\\\\") + '"; '
            if (cssClass != '' && cssHoverClass != '') {
                menuText += ' this.className="' + cssHoverClass + '";';
                menuText += ' showMenu(' + this.menuItems[i].subMenu.name + ',"mnuItem' + this.mnuNumber + mnuLevel + i + '",' + (parseInt(mnuLevel) + 1) + ');'
            }
            else {
                menuText += ' showMenu(' + this.menuItems[i].subMenu.name + ',"mnuItem' + this.mnuNumber + mnuLevel + i + '",' + (parseInt(mnuLevel) + 1) + ');'
            }
            menuText += '\'';
            menuText += ' onMouseOut=\'';
            if (this.pop_up_icon != null && this.pop_up_icon != '' && this.selected_pop_up_icon != null && this.selected_pop_up_icon != '')
                menuText += ' document.getElementById("Img' + this.mnuNumber + mnuLevel + i + '").src = "' + new String(this.pop_up_icon).replace("\\", "\\\\") + '"; '
            if (cssClass != '' && cssHoverClass != '') {
                menuText += ' this.className="' + cssClass + '";';
                menuText += ' collapseMenu("' + this.menuItems[i].subMenu.name + '",' + (parseInt(mnuLevel) + 1) + ');';
            }
            else {
                menuText += ' collapseMenu("' + this.menuItems[i].subMenu.name + '",' + (parseInt(mnuLevel) + 1) + ');';
            }
            menuText += '\'';
        }
        else {
            if (cssClass != '' && cssHoverClass != '') {
                menuText += 'onMouseOver="';
                menuText += 'this.className=\'' + cssHoverClass + '\';';
                menuText += ' "'
                menuText += ' onMouseOut="';
                menuText += 'this.className=\'' + cssClass + '\';';
                menuText += ' "';
            }
            /*else {
            menuText += 'onMouseOver="showMenu(' + this.name + ',\'' + this.Src + '\',' + mnuLevel + ');"' 
            //menuText += 'onMouseOut="hideMenu(\'' + this.name + '\',' + mnuLevel + ');"';
            }*/
        }
        if (this.menuItems[i].link != null) {
            if (!this.menuItems[i].isReadyLink) {
                if (this.menuItems[i].isSecure == 'Yes')
                    menuText += ' onClick="window.open(\'' + navSecure_url + "\/" + this.menuItems[i].link + navQuery_ids + '\', \'' + this.menuItems[i].srcWindowName + '\', \'' + this.menuItems[i].srcWinProperties + '\')"';
                else
                    menuText += ' onClick="window.open(\'' + navDomain_url + "\/" + this.menuItems[i].link + navQuery_ids + '\', \'' + this.menuItems[i].srcWindowName + '\', \'' + this.menuItems[i].srcWinProperties + '\')"';
            }
            else
                menuText += ' onClick="window.open(\'' + this.menuItems[i].link + '\', \'' + this.menuItems[i].srcWindowName + '\', \'' + this.menuItems[i].srcWinProperties + '\')"';
        }
        menuText += '>';
        /*if (this.menuItems[i].text = '160')
        alert(menuText)*/
        if (this.horizontalPos != null && this.horizontalPos == "left") {
            if (this.menuItems[i].subMenu != null && this.pop_up_icon != null && this.pop_up_icon != '') {
                menuText += '<td align="left" nowrap><img src="' + this.pop_up_icon + '" id="Img' + this.mnuNumber + mnuLevel + i + '" name="Img' + this.mnuNumber + mnuLevel + i + '"></td>';
                menuText += '<td nowrap>' + this.menuItems[i].text + '</td>';
            }
            else
                menuText += '<td nowrap colspan=2>' + this.menuItems[i].text + '</td>';
        }
        else {
            if (this.menuItems[i].subMenu != null && this.pop_up_icon != null && this.pop_up_icon != '') {
                menuText += '<td nowrap>' + this.menuItems[i].text + '</td>';
                menuText += '<td align="right" nowrap><img src="' + this.pop_up_icon + '" id="Img' + this.mnuNumber + mnuLevel + i + '" name="Img' + this.mnuNumber + mnuLevel + i + '"></td>';
            }
            else
                menuText += '<td nowrap colspan=2>' + this.menuItems[i].text + '</td>';
        }
        menuText += '</tr>';
        /*if (this.menuItems[i].text == "Item11")
        alert(menuText);*/
    }
    menuText += '</table></div>';
    document.write(menuText);
    for (var i = 0; i < this.menuItems.length; i++) {
        if (this.menuItems[i].subMenu != null) {
            this.menuItems[i].subMenu.left = (this.left + this.width) - 2;
            this.menuItems[i].subMenu.top = this.top + (i * 15) + 2;
            this.menuItems[i].subMenu.width = this.width;
            this.menuItems[i].subMenu.Build(parseInt(mnuLevel) + 1);
        }
    }
}

// *****************************************************************
// **************************** MENU ITEM **************************
// *****************************************************************

//object menuItem
function menuItem() {
    var args = menuItem.arguments;
    this.text = args[0];
    if (args[1] != null && args[1] != '') this.link = args[1];
    if (args[2] != null && args[2] != '') this.cssClass = args[2]
    if (args[3] != null && args[3] != '') this.MouseOverCssClass = args[3]
    if (args[4] != null && args[4] == 'Yes')
        this.isSecure = args[4];
    else
        this.isSecure = 'No';
    if (args[5] != null && args[5] != '')
        this.isReadyLink = args[5];
    else
        this.isReadyLink = false;
    if (args[6] != null && args[6] != '')
        this.srcWindowName = args[6];
    else
        this.srcWindowName = "_self";
    if (args[7] != null && args[7] != '')
        this.srcWinProperties = args[7];
    else
        this.srcWinProperties = "";


    this.addMenu = addSubMenu;
}

//Add a submenu to menu item
function addSubMenu() {
    var args = addSubMenu.arguments;
    this.subMenu = args[0];
    args[0].parentMenu = this;

}


