﻿//Top Nav Bar I v2.1- By Constantin Kuznetsov Jr.
//Modified by Dynamic Drive for various improvements
//Visit http://www.dynamicdrive.com for this script
if (document.all) { n = 0; ie = 1; ns6 = 0; fShow = "visible"; fHide = "hidden"; }
if (document.getElementById && !document.all) { n = 0; ie = 0; ns6 = 1; fShow = "visible"; fHide = "hidden"; }
if (document.layers) { n = 1; ie = 0; ns6 = 0; fShow = "show"; fHide = "hide"; }

//Top Nav bar script v2.1- http://www.dynamicdrive.com/dynamicindex1/sm/index.htm

opr6 = ie && navigator.userAgent.indexOf("Opera") != -1

window.onerror = new Function("return true")
////////////////////////////////////////////////////////////////////////////
// Function Menu()                                                        //
////////////////////////////////////////////////////////////////////////////
curMenuItem = "";
curSubItem = "";

rightX = 0;

// variables used to adjust for server based IE
positionType = "relative";
barHeightAttribute = ""; // determined on the fly ... some version of IE are having a problem
// with relative positioning and when set to absolute, the height
// attribute must also be set
subMenuOffset = 20;

function Menu() {
    
    this.bgColor = menucolor;

    if (ie) this.menuFont = "bold 12px Arial"; //default font settings. Don't change. Instead, modify stylesheet in sample.htm
    if (n) this.menuFont = "bold 12px Verdana";
    this.fontColor = "black";

    this.addItem = addItem;
    this.addSubItem = addSubItem;
    this.showMenu = showMenu;
    this.mainPaneBorder = 0;
    this.subMenuPaneBorder = 0;

    this.subMenuPaneWidth = submenuwidth;

    if (ie) {
        positionType = "absolute"; // "relative";
        barHeightAttribute = "height='18'";
        /*
        if((navigator.appVersion.indexOf("Windows NT 5.2") != -1)  &&
        (navigator.appVersion.indexOf("NET CLR 1.1.4322") != -1)
        )
        {
        positionType="absolute";
        barHeightAttribute="height='30'";
        subMenuOffset=32
        }
        */
    }

    lastMenu = null;

    rightY = 0;
    leftY = 0;
    leftX = 0;


    // controls used to toggle the submenu display
    lastMenuID = "";
    subMenuDisplayed = false;

    HTMLstr = "";
    HTMLstr += "<!-- MENU PANE DECLARATION BEGINS -->\n";
    HTMLstr += "\n";
    //	if (ie||ns6) HTMLstr += "<div id='MainTable' style='position:absolute;top:0;left:0;'>\n";
    if (ie || ns6) HTMLstr += "<div id='MainTable' >\n";
    //	if (n)  HTMLstr += "<layer name='MainTable'>\n";   border="0" cellpadding='0' cellspacing='0'
    HTMLstr += "<table width='100%' Class='MainMenutable' cellspacing='0' cellpadding='0'>\n";
    HTMLstr += "<tr>";
    if (n) HTMLstr += "<td>&nbsp;";
    HTMLstr += "<!-- MAIN MENU STARTS -->\n";
    HTMLstr += "<!-- MAIN_MENU -->\n";
    HTMLstr += "<!-- MAIN MENU ENDS -->\n";
    if (n) HTMLstr += "</td>";
    HTMLstr += "</tr>\n";
    HTMLstr += "</table>\n";
    HTMLstr += "\n";
    HTMLstr += "<!-- SUB MENU STARTS -->\n";
    HTMLstr += "<!-- SUB_MENU -->\n";
    HTMLstr += "<!-- SUB MENU ENDS -->\n";
    HTMLstr += "\n";
    if (ie || ns6) HTMLstr += "</div>\n";
    //	if (n)  HTMLstr+= "</layer>\n";
    HTMLstr += "<!-- MENU PANE DECALARATION ENDS -->\n";
   
} // menu()
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Add or create new items in main menu
function addItem(idItem, text, hint, location, altLocation, curPageFlg, Style) {
    
    var Lookup = "<!-- ITEM " + idItem + "-->";
    var itemBgColor = this.bgColor;
    var curPageFlag = 0;

    if (idItem != curMenuItem) {
        curPageFlag = curPageFlg;
    }
    else {
        curPageFlag = 1;
    }

    if (curPageFlag != 0) {
        itemBgColor = curPageColor;
    }

    if (HTMLstr.indexOf(Lookup) != -1) {
        alert(idParent + " already exist");
        return;
    }
    var MENUitem = "";
    
    MENUitem += "\n<!-- ITEM " + idItem + " -->\n";
    /*
    MENUitem += "<!-- DEBUG "+idItem+"\n"+
    "  --        text = "+ text + "\n"+
    "  --        hint = "+ hint + "\n"+
    "  --    location = "+ location + "\n"+
    "  -- altLocation = "+ altLocation + "\n"+
    "  -- curPageFlag = "+ curPageFlag + "\n"+
    "  -->";
    */

    
    if (n) {
        MENUitem += "<ilayer name=" + idItem + ">";
        MENUitem += "<a href='.' class=clsMenuItemNS onmouseover=\"displaySubMenu('" + idItem + "')\" onclick=\"return false;\">";
        MENUitem += "&nbsp;";
        MENUitem += text;
        MENUitem += "</a>";
        MENUitem += "</ilayer>";
    }
    if (ie || ns6) {
        if (ie == 1) {
            Style = Style;
        }
        else {
            Style = Style + "Moz";
        }


        MENUitem += "<td class='MenuBarTop'  >\n";
       
        //this.positionType="relative" "absolute"
        MENUitem += "<div id='" + idItem + "' class='" + Style +"'>\n";
        MENUitem += "<a ";
        MENUitem += "class='MenuBarTopText'";
        if (curPageFlag != 0) {
            MENUitem += "style='text-decoration: none; font: " + this.menuFont + "; color: #ffffff; cursor: hand;' ";
        }
       
   
        if (hint != null)
            MENUitem += " title='" + hint + "' ";
        if (location != null) {
            MENUitem += "href='" + location + "' ";
            if ((altLocation != null) && (altLocation != "")) { 
                MENUitem += "target='" + altLocation + "' "
            }
            MENUitem += "onmouseover=\"hideAll()\" ";
            //			MENUitem +=     "onclick=\"alert('trying to go to "+location+"')\"";
        }
        else {
            if (altLocation != null)
                MENUitem += "href='" + altLocation + "' ";
            else
                MENUitem += "href='javascript:toggleSubMenu(\"" + idItem + "\")' ";
            //toggleSubMenu	<< displaySubMenu			
            MENUitem += "onmouseover=\"toggleSubMenu('" + idItem + "')\" ";
            //			MENUitem +=     "onclick=\"toggleSubMenu('"+idItem+"');return false;\" ";			
            //			MENUitem +=     "onclick=\"hideAll();return false;\" "
        }
        MENUitem += ">";       
        MENUitem += text;
        MENUitem += "</a>\n";
        MENUitem += "</div>\n";
        MENUitem += "</td>\n";
    }
    MENUitem += "<!-- END OF ITEM " + idItem + " -->\n\n";
    MENUitem += "<!-- MAIN_MENU -->\n";

    HTMLstr = HTMLstr.replace("<!-- MAIN_MENU -->\n", MENUitem);
} // addItem

//------------------------------------------------------------------------------------------
// Add or create new items in pull-down menu
function addSubItem(idParent, text, hint, location, linktarget, curPageFlg) {
       var MENUitem = "";
    // var itemBgColor = this.bgColor;
    var itemBgColor = "#ffffff";
    var curPageFlag = 0;

    if (idParent != curMenuItem) {
        curPageFlag = curPageFlg;
    }
    else {
        if (text != curSubItem) {
            curPageFlag = curPageFlg;
        }
        else {
            curPageFlag = 1;
        }
    }


    if (curPageFlag != 0) {
        itemBgColor = curPageColor;
    }

    Lookup = "<!-- ITEM " + idParent + " -->";
    if (HTMLstr.indexOf(Lookup) == -1) {
        alert(idParent + " not found");
        return;
    }
    Lookup = "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->";
    if (HTMLstr.indexOf(Lookup) == -1) {
        if (n) {
            MENUitem += "\n";

            MENUitem += "<layer id='" + idParent + "submenu' visibility=hide bgcolor='" + this.bgColor + "'>\n";
            MENUitem += "<table border='" + this.subMenuPaneBorder + "' bgcolor='" + this.bgColor + "' width=" + this.subMenuPaneWidth + ">\n";
            MENUitem += "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n";
            MENUitem += "</table>\n";
            MENUitem += "</layer>\n";
            MENUitem += "\n";
        }
        if (ie || ns6) {
            MENUitem += "\n";
            //			MENUitem += "<div id='"+idParent+"submenu' onmouseout=operahide() style='position:absolute; visibility: hidden; z-index:100; width: "+this.subMenuPaneWidth+"; font: "+this.menuFont+"; top: -300;'>\n";
            if (ie == 1) {
                MENUitem += "<div id='" + idParent + "submenu' onmouseout=operahide() " +
			                 "Class='SubMenudivStyle'>\n";
            }
            else {
                MENUitem += "<div id='" + idParent + "submenu' onmouseout=operahide() " +
			                 "Class='SubMenudivStyleMoz'>\n";
            }
        
            MENUitem += "<table border='0' class='subMenuTable' cellspacing='0' cellpadding='0'" +
            //			                   "onmouseout='hideAll()' "+ 
			             ">\n";
            MENUitem += "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n";
            MENUitem += "</table>\n";
            MENUitem += "</div>\n";
            MENUitem += "\n";
        }
        MENUitem += "<!-- SUB_MENU -->\n";
        HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
    }

    Lookup = "<!-- NEXT ITEM OF SUB MENU " + idParent + " -->\n";
    if (n) {
        MENUitem = "<tr><b><td class='menu_SUB'><a class='menu_SUB' title='" + hint + "' href='" + location + "' target='" + linktarget + "'>" + text + "</a><br></td></b></tr>\n";
    }
    if (ie || ns6) {
        MENUitem = "<tr><td class='menu_SUB'>";
        MENUitem += "<a class=menu_SUB title='" + hint + "' ";
        if (curPageFlag != 0) {
            MENUitem += "class='menu_SUB' ";
            MENUitem += "onclick='return false;'>" + text + "</a><br></td></tr>\n";
        }
        else {
            MENUitem += "href='" + location + "' target='" + linktarget + "'>" + text + "</a></td></tr>\n";
        }
    }
    MENUitem += Lookup;
    HTMLstr = HTMLstr.replace(Lookup, MENUitem);

} // addSubItem

//------------------------------------------------------------------------------------------
function showMenu() {     
       document.writeln(HTMLstr);
}

////////////////////////////////////////////////////////////////////////////
// Private declaration
//--------------------------------------------------------------------------
function toggleSubMenu(idMainMenu) {


    window.status = "inside Toggle" + idMainMenu
    var displayStat = subMenuDisplayed;


    if (subMenuDisplayed) {
        window.status = subMenuDisplayed
        hideAll();
        subMenuDisplayed = false;
        lastMenuID = idMainMenu;
    }
    else {
        window.status = subMenuDisplayed
        hideAll();
        displaySubMenu(idMainMenu);
    }
    if (lastMenuID == idMainMenu) {
        window.status = subMenuDisplayed
        hideAll();
        displaySubMenu(idMainMenu);
    }

} //toggleSubMenu

//--------------------------------------------------------------------------
function displaySubMenu(idMainMenu) {
    var menu;
    var submenu;
    lastMenuID = idMainMenu;

    subMenuDisplayed = true;
    if (n) {
        submenu = document.layers[idMainMenu + "submenu"];
        if (lastMenu != null && lastMenu != submenu) hideAll();
        submenu.left = document.layers[idMainMenu].pageX;
        menuBarTop = document.layers[idMainMenu].pageY;
        submenu.top = menuBarTop + 25;
        submenu.visibility = fShow;

        leftX = document.layers[idMainMenu + "submenu"].left;
        rightX = leftX + document.layers[idMainMenu + "submenu"].clip.width;
        leftY = document.layers[idMainMenu + "submenu"].top +
  			     document.layers[idMainMenu + "submenu"].clip.height;
        rightY = leftY;
        // window.alert("... using layers x: "+ leftX + " to " + rightX + " y:" + leftY + " to "+rightY);
    }
    else if (ie || ns6) {
        menu = ie ? eval(idMainMenu) :
  		           document.getElementById(idMainMenu);
        submenu = ie ? eval(idMainMenu + "submenu.style") :
  		              document.getElementById(idMainMenu + "submenu").style;
        submenu.left = calculateSumOffset(menu, 'offsetLeft');
        menuBarTop = calculateSumOffset(menu, 'offsetTop')

        submenu.top = menuBarTop + subMenuOffset;
        //		submenu.top  = menu.style.bottom + 5;

        if (lastMenu != null && lastMenu != submenu) hideAll();

        submenu.visibility = fShow;
        leftX = ie ? document.all[idMainMenu + "submenu"].style.posLeft :
  		             parseInt(document.getElementById(idMainMenu + "submenu").style.left);
        rightX = ie ? leftX + document.all[idMainMenu + "submenu"].offsetWidth :
  		             leftX + parseInt(document.getElementById(idMainMenu + "submenu").offsetWidth);

        leftY = ie ? document.all[idMainMenu + "submenu"].style.posTop + document.all[idMainMenu + "submenu"].offsetHeight :
  		             parseInt(document.getElementById(idMainMenu + "submenu").style.top) +
  		             parseInt(document.getElementById(idMainMenu + "submenu").offsetHeight);
        rightY = leftY;
        //window.alert("... using DOM x: "+ leftX + " to " + rightX + " y:" + leftY + " to "+rightY +
        //             "\n    menuBarTop:"+menuBarTop);

    }
    lastMenu = submenu;

} // displaySubMenu

//------------------------------------------------------------------------------------------
function hideAll() {
    subMenuDisplayed = false;
    if (lastMenu != null) {
        lastMenu.visibility = fHide;
        lastMenu.left = 0;
        lastMenu = null;
    }
}

//------------------------------------------------------------------------------------------
function calculateSumOffset(idItem, offsetName) {
    var totalOffset = 0;
    var item = eval('idItem');
    //window.status= idItem.value + eval('MainTable')+ item[0]
    do {
        totalOffset += eval('item.' + offsetName);
        item = eval('item.offsetParent');
        //window.status="item=" +item + " totalOffset=" + totalOffset
    } while (item != null);
    return totalOffset;
}

//------------------------------------------------------------------------------------------
function updateIt(e) {
    var x = leftX;
    var y = rightY;

    // alert("Move detected");
    //window.status = 'move detected ...';
    //defaultStatus = 'move detected ...';

    // alert('updateIt ie: '+ie+' ns:'+ ns6+' opera:'+ opr6+' n:'+ n);

    if (ie) {
        x = window.event.clientX;
        y = window.event.clientY;

    }

    if (n || ns6) {
        x = e.pageX;
        y = e.pageY;

    }
    if (!opr6) {
        //m = menuBarTop - document.body.scrollTop
        //window.status= " y =" + y + " menubartop=" + m + " x= " + x + " RightX=" + rightX +" leftX=" + leftX + " rightY=" + rightY
+" scrollTop=" + document.body.scrollTop
        if ((x > rightX - document.body.scrollLeft) || (x < leftX - document.body.scrollLeft)) hideAll();
        // else if ((y<menuBarTop) || (y > rightY))  hideAll();
        else if ((y < menuBarTop - document.body.scrollTop) || (y > rightY - document.body.scrollTop)) hideAll();
    }

}

//------------------------------------------------------------------------------------------
function operahide() {
    if (opr6) {
        if (!MainTable.contains(event.toElement))
            hideAll()
    }
}

//------------------------------------------------------------------------------------------
function setMenuMoveHandler() {
    if (ie || ns6) {

        //document.body.onclick=hideAll;
        document.body.onscroll = hideAll;
        document.body.onmousemove = updateIt;
    }
    if (document.layers) {

        window.captureEvents(Event.MOUSEMOVE);
        // window.captureEvents(Event.CLICK);
        window.onmousemove = updateIt;
        window.onclick = hideAll;
    }
}
