var bIsLoaded;
topCount = 0;
if (!window.imgHspace) imgHspace=0;
    
function Initialise() {
	//alert("Init")
    bIsLoaded = false;
    bAllFinished = false;
    bBeingCreated = true;
    bOverMenu = false;
    
    iHighestZ = 50000;
    mSecsVis = iSecondsVisible * 1000;
    topCount = 1;
    allTimer = null;

    iImageSpan = (IE4) ? iImageSize : iImageSize + 12;
    sImageHTML = "<SPAN STYLE='width:" + iImageSpan + ";height:100%;float:right'>";
    sImageHTML += "<IMG SRC='" + sImage + "' WIDTH=" + iImageSize + " HEIGHT=" + iImageSize + " BORDER=0>";
    sImageHTML += "</SPAN>";
}

function LoadMenus() {
    //alert("LoadMenus")
	Initialise();
    
    while(eval("window.arMenu" + topCount)) {
       
        (IE4) ? GenerateTreesIE(false, topCount) : makeMenuNN(false, topCount);
        topCount++ }
    bIsLoaded = true;
    //                                      status = (topCount-1) + " Hierarchical Menu Trees Created"
    bAllFinished = true;
    bBeingCreated = false;
}

function makeMenuNN(bIsChild, menuCount, parentMenu, parItem) {
    menu = eval("Menu" + menuCount + "= new Layer(null, window)");
    menu.array = eval("arMenu" + menuCount);
    menu.SetTreeProperties = SetTreeProperties;
    menu.SetTreeProperties(bIsChild,parentMenu);

    while (menu.itemCount < menu.maxItems) {
        menu.itemCount++;
        // status = "Creating Hierarchical Menus: " + menuCount + " / " + menu.itemCount;
        prevItem = (menu.itemCount > 1) ? menu.item : null;
        sArrayName = "item" + menuCount + "_" + menu.itemCount;

        menu.item = eval(sArrayName + "= new Layer(width, menu)");
        menu.item.prevItem = prevItem;
        menu.item.setup = itemSetup;
        menu.item.setup(menu.itemCount,menu.array);
        if (menu.item.hasChildren) {
            makeMenuNN(true, menuCount + "_" + menu.itemCount, menu, menu.item);
            menu = menu.parentMenu; }
    }
    menu.lastItem = menu.item;
    menu.setup(bIsChild, parentMenu, parItem);
    menu.xPos = aPosX[menuCount - 1];
    menu.yPos = aPosY[menuCount - 1];
}

function GenerateTreesIE(bIsChild, menuCount, parentMenu) {
   	// alert("GenerateTreesIE")
	sMenuID = "Menu" + menuCount;
    sElementHTML = "<DIV ID=" + sMenuID + " STYLE='position:absolute; z-index:2; visibility:hidden;'></DIV>";
    window.document.body.insertAdjacentHTML("BeforeEnd", sElementHTML);
    
    menu = eval(sMenuID);
    menu.array = eval("arMenu" + menuCount);
    menu.SetTreeProperties = SetTreeProperties;
    menu.SetTreeProperties(bIsChild, parentMenu);
    menu.itemStr = "";
    
    while (menu.itemCount < menu.maxItems) {
        menu.itemCount++;
            //              status = "Creating Hierarchical Menus: " + menuCount + " / " + menu.itemCount;
        sItemID = "item" + menuCount + "_" + menu.itemCount;
        iArrayNum = (menu.itemCount - 1) * 3;
        text = menu.array[iArrayNum];
        bHasChildren = menu.array[iArrayNum + 2];
        sTextHTML = (bHasChildren) ? sImageHTML + text : text;
        menu.itemStr += "<SPAN ID=" + sItemID + " STYLE=\"width:" + width + "\">" + sTextHTML + "</SPAN><BR>";
        if (bHasChildren) {
            GenerateTreesIE(true, menuCount + "_" + menu.itemCount, menu);
            menu = menu.parentMenu; }   
    }
    menu.innerHTML = menu.itemStr;
    itemCol = menu.children.tags("SPAN");
    for (i=0; i<itemCol.length; i++) {
        it = itemCol(i);
        it.setup = itemSetup;
        it.setup(i+1, menu.array);
  }
    menu.lastItem = itemCol(itemCol.length-1);
    menu.setup(bIsChild, parentMenu);
    menu.xPos = aPosX[menuCount - 1];
    menu.yPos = aPosY[menuCount - 1];
}

function SetTreeProperties(bIsChild, parentMenu) {
    this.width = width;
    this.fontColour = sFontColour;
    this.backColour = backCol;
    this.overColour = overCol;
    this.borderColour =  borCol;
    this.maxItems = this.array.length / 3;
    this.hasParent = bIsChild;
    this.setup = menuSetup;
    this.itemCount = 0;
    
    if (!bIsChild) {
        this.parentTree = this;
        this.startChild = this; }
    else { this.parentTree = parentMenu.parentTree; }
}

function itemSetup(whichItem, whichArray) {
    this.onmouseover = itemOver;
    this.onmouseout = itemOut;
    this.container = (IE4) ? this.offsetParent : this.parentLayer;
    iArrayNum = (whichItem - 1) * 3;
    this.text = whichArray[iArrayNum];
    this.URL = whichArray[iArrayNum + 1];
    this.hasChildren = whichArray[iArrayNum + 2];

    if (IE4 && this.hasChildren) {
        this.child = eval("Menu" + this.id.substr(4));
        this.child.parentMenu = this.container;
        this.child.parentItem = this;
    }
    if (this.URL) {
        if (IE4) { this.onclick = linkIt; this.style.cursor = "hand"; }
        if (NN4) { this.captureEvents(Event.MOUSEUP); this.onmouseup = linkIt; }
    }
    if (IE4) {
        with (this.style) {
            padding = itemPad;
//          paddingRight = itemPad; //Removed by marc
            color = sFontColour;
            fontSize = iFontSize + "pt";
            fontWeight = "bold";
            fontFamily = sFontFace;
//          borderBottomWidth = "0px"; //Removed by marc
//          borderBottomColor = "#FFFFFF"; //Removed by marc
//          borderBottomStyle = "solid"; //Removed by marc
            backgroundColor = backCol; }
    }
    if (NN4) {
        this.HTML = "<FONT FACE='" + sFontFace + "' POINT-SIZE=" + iFontSize + " COLOR='" + sFontColour + "'><b>" + this.text + "</b></FONT>";
        if(this.hasChildren) { this.document.write(sImageHTML); this.document.close(); }  
        this.visibility = "inherit";
        this.bgColor = backCol;
        this.zindex = 100;
        if (whichItem == 1) { this.top = itemPad; }
//      else { this.top = (this.prevItem.top + this.prevItem.clip.height + 1) } //Removed by marc
        else { this.top = (this.prevItem.top + this.prevItem.clip.height) }
        this.left = itemPad;
        this.clip.top = this.clip.left = -itemPad + 1;
        this.clip.right = (this.container.width - itemPad - 1);
        iMaxSpace= this.container.width - (itemPad*2);
        this.textLayer = new Layer(iMaxSpace, this);
        this.textLayer.document.write(this.HTML);
        this.textLayer.document.close();
        this.textLayer.visibility = "inherit";
        this.clip.bottom = this.textLayer.document.height + itemPad;
        this.dummyLyr = new Layer(100, this);
        this.dummyLyr.left = this.dummyLyr.top = -itemPad;
        this.dummyLyr.clip.width = this.clip.width;
        this.dummyLyr.clip.height = this.clip.height;
        this.dummyLyr.visibility = "inherit"; }
}   

function menuSetup(hasParent, openCont, openItem) {
    this.onmouseover = menuOver;
    this.onmouseout = menuOut;
    
    this.showIt = showIt;
    this.keepInWindow = keepInWindow;
    this.hideTree = hideTree
    this.hideParents = hideParents;
    this.hideChildren = hideChildren;
    this.hideTop = hideTop;
    this.hasChildVisible = false;
    this.isOn = false;
    this.hideTimer = null;
    this.childOverlap = childOverlap;
    this.currentItem = null;
    this.hideSelf = hideSelf;
        
    if (hasParent) {
        this.hasParent = true;
        this.parentMenu = openCont;
        if (NN4) { this.parentItem = openItem; this.parentItem.child = this }}
    else { this.hasParent = false; }
    if (IE4) {
        with (this.style) {
            width = this.width;
            padding = 0; //added by marc
            backgroundColor = backCol; //Added by marc ie5Mac fix
            borderWidth = 1;
            borderColor = this.borderColour;
            borderStyle = "solid";
            zIndex = iHighestZ;
            }
        this.lastItem.style.border="";
        this.showIt(false);
        this.onselectstart = cancelSelect;
        this.moveTo = moveTo;
        this.moveTo(0,0);
    }
    if (NN4) {
        this.bgColor = this.borderColour;
        this.fullHeight = this.lastItem.top + this.lastItem.clip.bottom + 1;
        this.clip.right = this.width;
        this.clip.bottom = this.fullHeight;
    }
}

function Show(menuName, e, navName){
    if (!bIsLoaded) return;
    LinkElement = (IE4) ? event.srcElement : e.target;
    if (!bBeingCreated && !bAllFinished) LoadMenus();
    LinkElement.menuName = menuName;    
    ShowIt(e);

}

function ShowIt(e){
   	// alert("ShowIt")
    if (!bIsLoaded || !bAllFinished) return true;
    hideAll();
    
    LinkElement = (IE4) ? event.srcElement : e.target;
    currentMenu = eval(LinkElement.menuName);
    currentMenu.hasParent = false;
    currentMenu.parentTree.startChild = currentMenu;
	currentMenu.moveTo(currentMenu.xPos, currentMenu.yPos);
    currentMenu.isOn = true;
    currentMenu.showIt(true);
    return false;
}

function menuOver(e) {
    this.isOn = true;
    bOverMenu = true;
    currentMenu = this;
    if (this.hideTimer) clearTimeout(this.hideTimer);
}

function menuOut() {
    if (IE4) { if (window.event.srcElement.contains(window.event.toElement)) return; }
    this.isOn = false;
    bOverMenu = false;
    window.status = "";
    allTimer = setTimeout("currentMenu.hideTree()",10);
}

function inactAll(){
    for (i=2; i<=8; i++) {
        if ("nav" + i == NavName) {}
        else { inact('nav' + i) }
    }
}

function itemOver(){
    if (keepHilite) {
        if (this.container.currentItem && this.container.currentItem != this) {
            if (IE4) with (this.container.currentItem.style) { backgroundColor = backCol }
            if (NN4) this.container.currentItem.bgColor = backCol
        }}
    if (IE4) {
        theEvent = window.event;
        if (theEvent.srcElement.tagName == "IMG") return;
//      this.style.backgroundColor = this.container.overColour; } // ie5Fix by Marc
        this.style.backgroundColor = overCol; } // ie5Fix by Marc
    else { this.bgColor = this.container.overColour; }

    window.status = this.text;
    this.container.currentItem = this;
    if (this.container.hasChildVisible) { this.container.hideChildren(this); }
    if (this.hasChildren) { 
        horOffset = (this.container.width - this.container.childOverlap);
        if (NN4) {
            this.childX = this.container.left + horOffset;
            this.childY = (this.pageY+this.clip.top) + childOffset; }
        else {
            this.childX = this.container.style.pixelLeft + horOffset;
            this.childY = this.offsetTop + this.container.style.pixelTop + childOffset + 1; }
        this.child.moveTo(this.childX, this.childY);
        this.child.keepInWindow();
        this.container.hasChildVisible = true;
        this.container.visibleChild = this.child;
        this.child.showIt(true);
    }
}

function itemOut() {
    if (IE4) {
        theEvent = window.event;
       	if (theEvent.srcElement.contains(theEvent.toElement)
     || (theEvent.fromElement.tagName=="IMG" && theEvent.toElement.contains(theEvent.fromElement)))
        return;
        if (!keepHilite) this.style.backgroundColor = backCol; }
    else {
        if (!keepHilite) this.bgColor = backCol;
        if (!bOverMenu) { allTimer = setTimeout("currentMenu.hideTree()",10); }
    }
}

function moveTo(xPos, yPos) {
    this.style.pixelLeft = xPos;
    this.style.pixelTop = yPos;
}

function showIt(on) {
    if (NN4) {
        this.visibility = (on) ? "show" : "hide";
        if (keepHilite && this.currentItem) { this.currentItem.bgColor = this.backColour; }}
    else {
        this.style.visibility = (on) ? "visible" : "hidden";
        if (keepHilite && this.currentItem) {
            with (this.currentItem.style) {
                backgroundColor = this.backColour;
                color = this.fontColour; }}}
    this.currentItem = null;
}

function keepInWindow() {
    scrBars = 20;
    botScrBar = scrBars;
    rtScrBar = scrBars;
    if (NN4) {
        winRight = (window.pageXOffset + window.innerWidth) - rtScrBar;
        rightPos = this.left + this.width;
        if (rightPos > winRight) {
            if (this.hasParent) {
                parentLeft = this.parentMenu.left;
                newLeft = ((parentLeft-this.width) + this.childOverlap);
                this.left = newLeft;}
            else {
                dif = rightPos - winRight;
                this.left -= dif; }}
        winBot = (window.pageYOffset + window.innerHeight) - botScrBar ;
        botPos = this.top + this.fullHeight;
        if (botPos > winBot) {
            dif = botPos - winBot;
            this.top -= dif; }
        winLeft = window.pageXOffset;
        leftPos = this.left;
        if (leftPos < winLeft) {
            if (this.hasParent) {
                parentLeft = this.parentMenu.left;
                newLeft = ((parentLeft+this.width) - this.childOverlap);
                this.left = newLeft; }
            else { this.left = 5;}}}
    else {
    winRight = (window.document.body.scrollLeft + window.document.body.clientWidth) - rtScrBar;
        rightPos = this.style.pixelLeft + this.width;
        if (rightPos > winRight) {
            if (this.hasParent) {
                parentLeft = this.parentMenu.style.pixelLeft;
                newLeft = ((parentLeft - this.width) + this.childOverlap);
                this.style.pixelLeft = newLeft; }
            else {
                dif = rightPos - winRight;
                this.style.pixelLeft -= dif; }}

        winBot = (window.document.body.scrollTop + window.document.body.clientHeight) - botScrBar;
        botPos = this.style.pixelTop + this.fullHeight;

        if (botPos > winBot) {
            dif = botPos - winBot;
            this.style.pixelTop -= dif; }
        
        winLeft = window.document.body.scrollLeft;
        leftPos = this.style.pixelLeft;

        if (leftPos < winLeft) {
            if (this.hasParent) {
                parentLeft = this.parentMenu.style.pixelLeft;
                newLeft = ((parentLeft+this.width) - this.childOverlap);
                this.style.pixelLeft = newLeft; }
            else { this.style.pixelLeft = 5; }}}
}

function linkIt() {
    if (this.URL.indexOf("javascript:")!=-1) eval(this.URL)
    else window.location.href = this.URL;
}

function Hide(menuName){
    if (!bIsLoaded || !bAllFinished) return;
    whichEl = eval(menuName);
    whichEl.isOn = false;
    whichEl.hideTop();

    //Added by Marc for forms
    // if (checkForms && NN4 && (menuName != "Menu4" || menuName != "Menu5" || menuName != "Menu6" || menuName != "Menu7")) {showForms();}
}

function hideAll() {
    for(i=1; i<topCount; i++) {
        temp = eval("Menu" + i + ".startChild");
        temp.isOn = false;
        if (temp.hasChildVisible) temp.hideChildren();
        temp.showIt(false);
    }   
}

function hideTree() {
    allTimer = null;
    if (bOverMenu) return;
    if (this.hasChildVisible) this.hideChildren();
    this.hideParents();
   // if (NavName != '') inact(NavName);

    //Added by marc
    //if (checkForms && NN4) showForms();
}

function hideTop() {
    whichTop = this;
    this.hideTimer = setTimeout("if(whichTop.hideSelf)whichTop.hideSelf()",mSecsVis);
}

function hideSelf() {
    this.hideTimer = null;
    if (!this.isOn && !bOverMenu) {
        this.showIt(false);
    }
}

function hideParents() {
    tempMenu = this;
    while (tempMenu.hasParent) {
        tempMenu.showIt(false);
        tempMenu.parentMenu.isOn = false;       
        tempMenu = tempMenu.parentMenu;
    }
    tempMenu.hideTop();
}

function hideChildren(item) {
    tempMenu = this.visibleChild;
    while (tempMenu.hasChildVisible) {
        tempMenu.visibleChild.showIt(false);
        tempMenu.hasChildVisible = false;
        tempMenu = tempMenu.visibleChild;
    }
    if (!this.isOn || !item.hasChildren || this.visibleChild != this.child) {
        this.visibleChild.showIt(false);
        this.hasChildVisible = false; }
}

function cancelSelect(){ return false }
