////////////////////////////////////////////////////////////////////////
//
// Filename:    navi.js
// Purpose:     Implement navigation menu
// Method:      DHTML
// Ref:         http://www.quirksmode.org/js/display.html
// Date:        2005-12-10
//
// Notes:
// This code assumes the following HTML:
//
//     <!-- 1st level menu -->
//     <DIV class=label>
//          <IMG class=label> menu title
//     </DIV>
//     <DIV class=content>
//          <A class=meny>...</A>
//          ...
//          <!-- 2nd level menu starts -->
//          <DIV class=label>
//               <IMG class=label> menu title
//          </DIV>
//          <DIV class=content>
//               <A class=meny>...</A>
//               ...
//          </DIV>
//     </DIV>
//
// The content area should set the curURL variable which will trigger
// setting ID=currentPage on the currently selected link, allowing
// alternate styling of this element.
//
////////////////////////////////////////////////////////////////////////

//
// Current item selected
//
var curURL;

//
// nodeType's
//
var   ELEMENT_NODE = 1;
var ATTRIBUTE_NODE = 2;
var      TEXT_NODE = 3;

window.onload = function ()
//
// Activities once HTML is loaded:
// 1. make <div class=label> clickable
// 2. close all menu's
// 3. open menu sub-hierarchy to make current item visible
// 4. give current item ID=currentPage
//
{
    curURL = top.mainFrame.location.href;

    var x = top.menuFrame.document.getElementsByTagName('div');
    for (var i = 0; i < x.length; i++) {
        if (x[i].className == 'label') {
            x[i].onclick = clickNav;
        }
    }
    closeNav();
    if (curURL) {
        setNav(curURL, 'currentPage');
    }
}

function closeNav()
{
    var x = top.menuFrame.document.getElementsByTagName('div');
    for (var i = 0; i < x.length; i++) {
        if (x[i].className == 'content') {
            x[i].style.display = 'none';
        }
    }
    var x = top.menuFrame.document.getElementsByTagName('img');
    for (var i = 0; i < x.length; i++) {
        if (x[i].className == 'label') {
            x[i].src = '../img/mclosed.gif';
        }
    }
}

function clickNav(e)
//
// Toggle the visibility of a menu
//
{
    if (!e) var e = window.event;

         if (e.target)     var tg = e.target;
    else if (e.srcElement) var tg = e.srcElement;

    while (tg.nodeName != 'DIV') { // Safari
        tg = tg.parentNode;
    }

    //
    // Search for IMG inside the DIV
    // Toggle its source.
    //
    var Sib = tg.firstChild;
    while (Sib.nodeType != ELEMENT_NODE && Sib.tagName != 'IMG') {
        Sib = Sib.nextSibling;
    }
    var newSrc = (Sib.src.search('mopened') >= 0)
               ? '../img/mclosed.gif'
               : '../img/mopened.gif';
    Sib.src = newSrc;

    //
    // Search for sibling DIV's (the menu items related to this header)
    // Toggle their visibility.
    //
    var nextSib = tg.nextSibling;
    while (nextSib.nodeType != ELEMENT_NODE) {
        nextSib = nextSib.nextSibling;
    }
    var nextSibStatus = (nextSib.style.display == 'none') ? 'block' : 'none';
    nextSib.style.display = nextSibStatus;
}

function cleanURL(page)
//
// Ignore 2nd and additional arguments from an URL while comparing them
// (the 1st argument contains the section).
//
{
    var test;

    test = page.indexOf('&')+1;
    if (test) {
        page = page.substring(0,test-1);
    }
    test = page.indexOf('#')+1;
    if (test) {
        page = page.substring(0,test-1);
    }

    return page;
}

function setNav(page, newID)
//
// Open all DIV's (and parents) containing the current URL
//
{
    var x = top.menuFrame.document.getElementsByTagName('A');
    var i;

    //
    // Deselect all menu items
    //
    for (i = 0; i < x.length; i++) {
        x[i].id = '';
    }

    //
    // Select current menu item
    //
    page = cleanURL(page);
    for (i = 0; i < x.length; i++) {
        if (cleanURL(x[i].href) == page) {
            // alert('set ' + newID + ' on ' + page);
            x[i].id = newID;
            break;
        }
    }

    if (i < x.length && newID == 'currentPage') {
        var parDiv = x[i];
        while (parDiv.parentNode.tagName == 'DIV') {
            parDiv = parDiv.parentNode;
            parDiv.style.display = 'block';

            //
            // Search for IMG inside the preceding DIV
            //
            var Sib = parDiv.previousSibling;
            while (Sib && Sib.nodeType != ELEMENT_NODE) {
                Sib = Sib.previousSibling;
            }
            if (Sib && Sib.className == 'label') {
                //
                // Search for IMG inside the DIV
                //
                Sib = Sib.firstChild;
                while (Sib.nodeType != ELEMENT_NODE) {
                    Sib = Sib.nextSibling;
                }
                Sib.src = '../img/mopened.gif';
            }
        }
    }
}

function cleanNav()
{
    closeNav();
    setNav(curURL, 'currentPage');
}
