/* Main navigation id */
var navId = "nav";

/* CSS class names, change if needed */
var nav      = 'pde_nav';
var hidden   = 'pde_hide';
var visible  = 'pde_show';
var parent   = 'pde_parent';
var active   = 'pde_active';
var current  = 'current';
    
    
    function setup() {

        // Loop counter
        var i;

        if(!document.getElementById && !document.createTextNode){return;}

        /* Get the nav node */
        var root = document.getElementById(navId);

        // If there is no root node, exit */
        if (root == null) {
            return;
        }
    
    
        setClass(root,nav)
    
        /* Now get our top level list elements (ul's) because we want to collapse those as well */
        var ulElements = root.getElementsByTagName('ul');

        for(i=0; i<ulElements.length; i++) {
            // 
            if(isCurrent(ulElements[i])) {
                setClass(ulElements[i].parentNode.firstChild, active);
                alert('setting ' + ulElements[i].parentNode.firstChild.nodeValue + ' [' + active + ']');
            } else {
                setClass(ulElements[i], hidden);

                setClass(first_child(ulElements[i].parentNode), parent);
                first_child(ulElements[i].parentNode).onclick=function() {
                    swapClass(this, parent, active);
                    addAltText();
                    swapClass(this.parentNode.getElementsByTagName('ul')[0], hidden, visible);
                    return false;
                }
            }
        }
    
    }
    // Is the object currently selected (by class)
    function isCurrent(obj){
        if(containsClass(obj.parentNode, current)) {
            return true;
        }

        for(var i=0;i<obj.getElementsByTagName('li').length;i++) {
            if(containsClass(obj.getElementsByTagName('li')[i], current)) {
                return true;
            }
        }

        // Otherwise not current
        return false;
    }


    // Swap two classes on an object
    function swapClass(obj, class1, class2) {

        var objClass = obj.className;

        // If class1 does not already exist, swap with class2
        if(containsClass(obj, class1)) {
            obj.className = objClass.replace(class1, class2);
        } else {
            obj.className = objClass.replace(class2, class1);
        }

    }

    // Set a class on an object
    function setClass(obj, name) {
        // Check to make sure the class doesn't already exist
        if(!containsClass(obj,name)) {

            // See if we need to add a spacer (classes separated by space)
            if(!obj.className == '') {
                obj.className += ' ';
            }
            obj.className += name;
        }
    }


    // Returns boolean - whether the name passed is contained in the obj
    function containsClass(obj,name) {
        return new RegExp('\\b'+name+'\\b').test(obj.className);
    }


    function addAltText() {
        //find the navigation ul
        var navigation = document.getElementById(navId);

        //find all the a tags within the navigation ul
        var expand = navigation.getElementsByTagName('a');
        

        // loop through all the a tags
        for (var i = 0; i < expand.length; i++) {
       // alert(expand[i]);

            // find the child of the links
            expandImg = expand[i].firstChild;

            // alert(expand[i].firstChild.nodeValue);
            //var selection = expand[i].firstChild.nodeValue;     
            
           // Overall Aim - find the link phrase (nodeValue) to insert as alt text for the image.
           // find the first child of the li. 
           // find its next sibling which should be an <a> tag. (the <a> we want)
           // find the nodeValue of the  <a> - this will be the value of selection.
           // if the next sibling of the first child <a> is not an <a> tag but another node such as h2, p, div
           // find the firstChild of this node which should be the <a>
           // find the node value of this <a>.
           // if the first child of the next sibling (p, h2, div) of the first child (<a>) is not an <a> tag
           // ABORT!!!!
            
         if(expand[i].className == parent) {
                       if (first_child(expand[i].nextSibling).hasChildNodes() == true) {
                           var selection = (first_child(first_child(expand[i].nextSibling)).nodeValue); 
                           expandImg.setAttribute('alt', 'Collapse' + " " + selection);  
                       } else if (first_child(expand[i].nextSibling).hasChildNodes() == false) {
                           var selection = (first_child(expand[i].nextSibling).nodeValue);
                           expandImg.setAttribute('alt', 'Expand' + " " + selection);  
                       }  else {
                       expandImg.setAttribute('alt', 'Expand');
                       } 

                  } else if (expand[i].className == active) {
                       if (first_child(expand[i].nextSibling).hasChildNodes() == false) {
                           var selection = (first_child(expand[i].nextSibling).nodeValue); 
                           expandImg.setAttribute('alt', 'Collapse' + " " + selection);
                       } else if (first_child(expand[i].nextSibling).hasChildNodes() == true) {
                           var selection = (first_child(first_child(expand[i].nextSibling)).nodeValue);
                           expandImg.setAttribute('alt', 'Expand' + " " + selection); 
                       } else {
                           expandImg.setAttribute('alt', 'Collapse');
                       }
                   }
               }
    }

    
   
    function expand() {
        //find the list we want to work with
        var nav = document.getElementById(navId);

        //find all the uls 
        var lists = nav.getElementsByTagName("ul");

        for (i=0; i<lists.length; i++){
            var currentUl = lists[i];

            // find the parents of the uls 
            var parentContainer = currentUl.parentNode;

            //create a new link element 
            var newLink = document.createElement('a');

            // set its href to #
            newLink.setAttribute('href', '#');

            // insert the link into the document for lists to expand
            parentContainer.insertBefore(newLink, first_child(parentContainer));

            //create a new image element
            var newImg = document.createElement('img');

            // set its src to transparent.gif
            newImg.setAttribute('src', 'images/transparent.gif');

            // insert the image into the new link we just created
            newLink.insertBefore(newImg, newLink.firstChild);    
        } 
    }

 
// Add function to onload handler
addEvent('onload', window, expand);
addEvent('onload', window, setup);
addEvent('onload', window, addAltText);
