<!--
/* Javascript to display sublist of a given list item.
 *
 * How to use
 *
 * Give the navigation list an ID (say, 'id="nav"').
 *
 * Include this in the <body> tag:
 *  onload="initXList('nav')"
 *
 * You must also include this line somewhere in your stylesheet:
 *  ul#nav ul {	display: none; }
 *
 * Other than this, lists can be styled however you want!
 *
 * JB Gray
 */

var navID;
var propagating = false;
var cookieArray=new Object();


// Used to assign xList onclick event
function newXList(li){
    return (function() {
        xList(li);
    });
}

// Initialises XList; takes the list ID as an argument
function initXList(thisNavID) {
    navID = thisNavID;
    attachXList(document.getElementById(navID));
}

// Attaches the xList function to descendents
function attachXList(li) {
    if (li.nodeName == 'LI') {
        var this_onclick = newXList(li);
        li.onclick = this_onclick;
        li.onselectstart = new Function('return false');  // Prevent accidental selection in IE
    }
    for (var i = 0; i < li.childNodes.length; i++) {
        var thisChild = li.childNodes[i]
        if (thisChild.nodeName == 'UL' || thisChild.nodeName == 'LI') {
            attachXList(thisChild);
        }
    }
}

// The xList onclick event: does all the hiding and showing
function xList(li) {
    if (!propagating) {
        var displayChild = isChildHidden(li);
        hideChildren(li);
        hideParentSublists(li);
        if (displayChild){
            showChild(li);
        }

        if((li.childNodes.length==1 || li.childNodes.length==2) && li.childNodes[0].nodeName=='A'){
                    var parent=li.parentNode.parentNode.id;
                    if(parent==""){
                        cookieArray['activenode']=navID;
                        writeCookie(navID);
                    }
                    else{
                        cookieArray['activenode']=li.parentNode.parentNode.id;
                        writeCookie(li.parentNode.parentNode.id);
                    }
            }
    }

    propagating = (li.parentNode.id != navID);

    showNonHiddenMenus();


}

// Shows list child elements of a list item
function showChild(li) {
    for (var i = 0; i < li.childNodes.length; i++){
        if (li.childNodes[i].nodeName == "UL"){
            li.childNodes[i].style.display = 'block';
        }
    }
}

function showNonHiddenMenus(){
    for (var i = 0; i < neverHiddenChildren.length; i++ ){
        var this_id=neverHiddenChildren[i];
        var this_node=document.getElementById(this_id);
        for(var ii = 0; ii < this_node.childNodes.length; ii++){
        	if(this_node.childNodes[ii].nodeName=="UL"){
            	this_node.childNodes[ii].style.display='block';
        	}
        }
    }
}


function showSelectedMenu(){
    readCookie();
    if(menuLocation != 'NONE'){
        var this_id=menuLocation;
	var this_node=document.getElementById(this_id);
            for(var i = 0; i < this_node.childNodes.length; i++){
                    if(this_node.childNodes[i].nodeName==='UL'){
                        this_node.childNodes[i].style.display='block';
                    }
            }
            if(this_node.parentNode.nodeName==='UL' && this_node.parentNode.id!='nav'){
                this_node.parentNode.style.display='block';
            }
            if(this_node.parentNode.parentNode.nodeName==='UL' && this_node.parentNode.parentNode.id!='nav'){
                this_node.parentNode.parentNode.style.display='block';
            }
        writeCookie(this_id);
    }
    else{
            var this_id=cookieArray['activenode'];
            var this_node=document.getElementById(this_id);
            for(var i = 0; i < this_node.childNodes.length; i++){
                    if(this_node.childNodes[i].nodeName==='UL'){
                        this_node.childNodes[i].style.display='block';
                    }
            }
            if(this_node.parentNode.nodeName==='UL' && this_node.parentNode.id!='nav'){
                this_node.parentNode.style.display='block';
            }
            if(this_node.parentNode.parentNode.nodeName==='UL' && this_node.parentNode.parentNode.id!='nav'){
                this_node.parentNode.parentNode.style.display='block';
            }
            writeCookie(this_id);
    }
}


// Detects if the current list child is hidden
function isChildHidden(li) {
    for (var i = 0; i < li.childNodes.length; i++)
        if (li.childNodes[i].nodeName == 'UL')
    return (li.childNodes[i].style.display != 'block');
}

// Recursively hides all sublists
function hideChildren(li) {
    for (var i = 0; i < li.childNodes.length; i++) {
        if (li.childNodes[i].nodeName == 'UL') {
            hideChildren(li.childNodes[i]);
            li.childNodes[i].style.display = 'none';
        } else if (li.childNodes[i].nodeName == 'LI')
            hideChildren(li.childNodes[i]);
    }
}

// Hides displayed lists above this list item
// Only needs to look one level above current list item
function hideParentSublists(li) {
    var listParent = li.parentNode;
    for (var i = 0; i < listParent.childNodes.length; i++) {
        var thisNode = listParent.childNodes[i];
        if (thisNode.nodeName == 'LI' && thisNode != li) {
            hideChildren(thisNode);
        }
    }
}

// stuff for cookie setting etc.

function writeCookie(id){
    document.cookie="activenode="+escape(id);
}

function readCookie(){
    var cookieTempArray=document.cookie.split('; ');
    for(var i=0; i < cookieTempArray.length; i++){
        var tempArray=new Array();
        tempArray=cookieTempArray[i].split('=');
        var name=tempArray[0];
        var value=tempArray[1];
        cookieArray[name]=unescape(value);
    }
    if(!cookieArray['activenode']){
        cookieArray['activenode']=navID;
        writeCookie(navID);
    }
}





// -->
