
// $Revision: #5 $

// menu style vars
var width = 165;
var height = 25;
var border = "#B8B8B8";
var menubgOn = "#CCCCCC";
var menubgOff = "#EBEBEB";


var menuXoffset = 0;
var menuYoffset = 3;

// functionality variables
var delay = 500;
var delayStart = 250;
var useMask = false;
var useDHTML = false;
var hideSels = false;
var start;
var hideFlag;
var hideTimer;
var showTimer;
var curMenu;
var curItem;
var curNav;
var activeMenu;
var theMenu;
var theImg;
var thePos;
var theDiv;
var theCont;
var theTable;
var theMask;
var onMenu = false;
var menusWritten = false;


// menu building variables;
var menucount = 0;

var menustyle = "width:" + width + "px; text-align:left; border-left:" + border + " 1px solid; border-right:" + border + " 1px solid; background-color:" + menubgOff + "; cursor: hand;"

var separator = "<div style=\"background-image:url('"+imageBaseURL+"menudot.gif'); layer-background-image:url('"+imageBaseURL+"menudot.gif');\"><img src=\""+imageBaseURL+"spacer.gif\" width=\"1\" height=\"1\" alt=\"\" border=\"0\"></div>";

function menuobject (label, url, newwin) {
    this.label = label;
    this.url = url;
    this.newwin= newwin;
    return this;
}


function renderdata () {
    menuData[catCount++] = tmpData;
    tmpData = new Array();
    count = 0;
}

// Turn iFrame mask on for IE 5.5+ / PC only
if (is_ie && !is_mac_ie) {
    if (is_ie5_5up) {
        useMask = true;
    }
    else {
        hideSels = true;
    }
}

// Turn on DHTML for 5+ browsers only
if (is_ie && !is_ie5up) {
    useDHTML = false;
} else if(!nn4) { useDHTML = true; }

function writeMenus() {
    if(useDHTML) {

        for (i = 0; i < menuData.length; i++) {
            document.writeln("<div id=\"menu" + i + "\" style=\"position:absolute; z-index:100; top:0px; left:0px; visibility:hidden;\">");
            if (useMask) {
                document.writeln("<table id=\"table" + i + "\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td>");
            }


            for (j = 0; j < menuData[i].length; j++) {


if (menuData[i][j].newwin=="Y"){
 newwinJS="Y";
 newwinHTML="target='new'";
}else{
 newwinJS="N";
 newwinHTML="";
}

basicCellStyle="dhtml_menuitem";

linkStyleUse="dhtml_menulink";


               if (j == 0 && j == menuData[i].length - 1){
		    document.write("<div id=\"menuitem" + menucount + "\" onMouseOver=\"startShow('" + i + "'); changebg('menuitem" + menucount + "'); changeStatus('" + menuData[i][j].url + "');\" onMouseOut=\"startHide(); changeStatus('');\" onClick=\"goTo('" + menuData[i][j].url + "','" + newwinJS + "');\" style=\"" + menustyle + "border-top:" + border + " 1px solid;\" style=\"" + menustyle + "border-bottom:" + border + " 1px solid;\"><div class=\"" + basicCellStyle + "\"><a " + newwinHTML + " href=\"" + menuData[i][j].url + "\" class=\"" + linkStyleUse + "\">" + menuData[i][j].label + "</a></div></div>");
                }
               else{ 

                if (j == 0) {
                    document.write("<div id=\"menuitem" + menucount + "\" onMouseOver=\"startShow('" + i + "'); changebg('menuitem" + menucount + "'); changeStatus('" + menuData[i][j].url + "');\" onMouseOut=\"startHide(); changeStatus('');\" onClick=\"goTo('" + menuData[i][j].url + "','" + newwinJS + "');\" style=\"" + menustyle + "border-top:" + border + " 1px solid;\"><div class=\"" + basicCellStyle + "\"><a " + newwinHTML + " href=\"" + menuData[i][j].url + "\" class=\"" + linkStyleUse + "\">" + menuData[i][j].label + "</a></div>" + separator + "</div>");
                }
                else if (j < menuData[i].length - 1) {
                    document.write("<div id=\"menuitem" + menucount + "\" onMouseOver=\"startShow('" + i + "'); changebg('menuitem" + menucount + "'); changeStatus('" + menuData[i][j].url + "');\" onMouseOut=\"startHide(); changeStatus('');\" onClick=\"goTo('" + menuData[i][j].url + "','" + newwinJS + "');\" style=\"" + menustyle + "\"><div class=\"" + basicCellStyle + "\"><a " + newwinHTML + " href=\"" + menuData[i][j].url + "\" class=\"" + linkStyleUse + "\">" + menuData[i][j].label + "</a></div>" + separator + "</div>");
                }
                else {
                    document.write("<div id=\"menuitem" + menucount + "\" onMouseOver=\"startShow('" + i + "'); changebg('menuitem" + menucount + "'); changeStatus('" + menuData[i][j].url + "');\" onMouseOut=\"startHide(); changeStatus('');\" onClick=\"goTo('" + menuData[i][j].url + "','" + newwinJS + "');\" style=\"" + menustyle + "border-bottom:" + border + " 1px solid;\"><div class=\"" + basicCellStyle + "\"><a " + newwinHTML + " href=\"" + menuData[i][j].url + "\" class=\"" + linkStyleUse + "\">" + menuData[i][j].label + "</a></div></div>");
                }
                
               }                              
             menucount++;
            }	                              
            if (useMask) {
                document.writeln("</td></tr></table>");
            }    
            document.writeln("</div>");
            moveMenu(i);
        }
    }
    menusWritten = true;
}

function moveMenu(num) {
    tmpMenu = "menu" + num;
    thePos = getObj("pos" + num);
    theImg = "nav" + num;
    xPos = offsetLeft(thePos) + menuXoffset;
    yPos = offsetTop(thePos) + height + menuYoffset;
    /*
    if (num == 0) {
        xPos += 4;
    }
    if (num == (menuData.length - 1)) {
        xPos -= width - document.images[theImg].width;
    }
    if (num == (menuData.length - 2)) {
        xPos -= 30;
    }*/
            
    var theObj=getObj(tmpMenu);
    if (theObj!=null){
     theObj.style.left = xPos+"px";
     theObj.style.top = yPos+"px";
    }   
    theObj=null;
}

function showMenus() {
 
    unfocus();
    if (hideSels) {
        hideSelect();
    }
    onMenu = true;
    theMenu = "menu" + curMenu;
    activeMenu = theMenu;
    moveMenu(curMenu);
    if (useMask) {
        theMask = getObj("iMask");
        theTable = "table" + curMenu;
        theMask.style.left = xPos+"px";
        theMask.style.top = yPos+"px";
        theMask.style.width = getObj(theTable).offsetWidth;
        theMask.style.height = getObj(theTable).offsetHeight;
    }
    getObj(theMenu).style.visibility = "visible";
}

function startShow(menu) {

    if (curNav) {
        navOff(curNav);
    }
    navOver(menu);
    curNav = menu;
    if (menusWritten && !nn4) {
        curMenu = menu;
        if (activeMenu) {
            getObj(activeMenu).style.visibility = "hidden";
        }
        clearTimer();
        if (!onMenu) {
            if (curItem) {
                curItem.style.backgroundColor = menubgOff;
            }
            showTimer = setTimeout("showMenus()", delayStart);
        }
        else {
        showMenus();
        }
    }
    else {
        return false;
    }
}

function startHide() {

    if (!onMenu) {
        if (curNav) {
            navOff(curNav);
            curNav = null;
        }
    }
    if (menusWritten && !nn4) {
        if (showTimer) clearTimeout(showTimer);
        start = new Date();
        hideFlag = true;
        hideTimer = setTimeout("hideMenus()", delay);
    }
    else {
        return false;
    }
}

function clearTimer() {
    if (hideTimer) clearTimeout(hideTimer);
    hideTimer = null;
    hideFlag = false;
}

function hideMenus() {

    if (!hideFlag) return;
    var elapsed = new Date() - start;
    if (elapsed < delay) {
        hideTimer = setTimeout("hideMenus()", delay - elapsed);
        return;
    }
    hideFlag = false;
    hideAllMenus();
    if (hideSels) {
        showSelect();
    }
}

function hideAllMenus() {
    changeStatus('');
    if (curNav) {
        navOff(curNav);
        curNav = null;
    }
    
    if (menuData) {
      for (i = 0; i<menuData.length; i++) {
        theMenu = "menu" + i;
        var menuObject = getObj(theMenu);
        if (menuObject) {
          menuObject.style.visibility = "hidden";
        }
      }
    }
    
    if (useMask) {
        if (theMask) {
            theMask.style.left = -300;
            theMask.style.top = -300;
        }
    }
    if (curItem) {
        curItem.style.backgroundColor = menubgOff;
    }
    onMenu = false;
}

function goTo(url, newwin) {
 if (newwin!="Y"){
    document.location.href = url;
 }
}

//return [object]
function getObj(theId) {
    if (document.getElementById) {
        theObj = document.getElementById(theId); 
    } else if (document.all) { 
        theObj = document.all[theId];
    }
    return theObj;
}

//return [img] object NS4 fix
function getImg(imgName, divName) {
    if (nn4) {
        imgObj = eval("document." + divName + ".document.images." + imgName);
    }
    else {
        imgObj = eval("document.images." + imgName);
    }
    return imgObj;
}

//Mac IE offset fix
function offsetLeft(o){
    var i = 0;
    while (o.offsetParent!=null) {
        i += o.offsetLeft;
        o = o.offsetParent;
    }
    return i + o.offsetLeft;
}
function offsetTop(o){
    var i = 0;
    while (o.offsetParent!=null) {
        i += o.offsetTop;
        o = o.offsetParent;
    }
    return i + o.offsetTop;
}

function changebg(id) {
 
    if (useDHTML) {
        var tmp = getObj(id);
        if (curItem) {
            if (tmp != curItem) {
                curItem.style.backgroundColor = menubgOff;
                tmp.style.backgroundColor = menubgOn;
            }
        } else { tmp.style.backgroundColor = menubgOn; }
        curItem = tmp;
    }
}

function unfocus() {
    for (x = 0; x < document.forms.length; x++) {
        for (y = 0; y < document.forms[x].elements.length; y++) {
            if (document.forms[x].elements[y].type == "text") {
                document.forms[x].elements[y].blur();
            }
        }
    }
}

function hideSelect() {
    var f = document.forms;
    for(var i=0;i<f.length;i++) {
        for (var j=0; j<f[i].elements.length; j++) {
            if (f[i].elements[j].type == 'select-one') {
                f[i].elements[j].style.visibility = 'hidden';
            }
        }
    }
}

function showSelect() {
    var f = document.forms;
    for(var i=0;i<f.length;i++) {
        for (var j=0; j<f[i].elements.length; j++) {
            if (f[i].elements[j].type == 'select-one') {
                f[i].elements[j].style.visibility = 'visible';
            }
        }
    }
}

function changeStatus(text) {
    window.status = text;
}

function clearbg() {

    if (curItem) {
        curItem.style.backgroundColor = menubgOff;
    }
    curItem = null;
}

if (document.addEventListener) {
    document.addEventListener("mouseup", mouseUp, false);
}

document.onmouseup = mouseUp;

function mouseUp() {
    hideAllMenus();
    return true;
}

