/*
 * menuExpandable.js - implements an expandable menu based on a HTML list
 * Originally based on code by Dave Lindquist (http://www.gazingus.org)
 */

if (!document.getElementById)
    document.getElementById = function() { return null; }

// For BBO site the content is /html/body/@id

var _bodytag = document.getElementsByTagName("body")[0];
var _context = _bodytag.attributes.getNamedItem("id").value;

// Ensure that the global variable and cookie are available.
var toc = new Cookie( document, 'toc' + _context );
if ( !toc.load() ) {
    toc.created = 'yes'; // defaults.
    toc.store();
}


function initializeMenu(menuId, actuatorId) {
    
    var menu = document.getElementById(menuId);         // a  <ul> element
    var actuator = document.getElementById(actuatorId); // an <img> element

    if (menu == null || actuator == null) return;

    // initialise this menu state to cookie value, if set.

    if (toc[menuId] == null) {
        actuator.setAttribute("src", "/render_image/plus");
        menu.style.display = "none";
        toc[menuId] = "none";
        toc.store();
    } else {
        var disp = toc[menuId];
        menu.style.display = disp;
        if (disp == "none") {
            actuator.setAttribute("src", "/render_image/plus");
        } else {  
            actuator.setAttribute("src", "/render_image/minus");
        }
    }
    
    //
    // set up the onclick handler for this menu
    //
    actuator.onclick = function() {
        
        var display = menu.style.display;
        
        // change the image (of the actuator)
        // and the display style of the menu
        
        if (display == "block") {
            this.setAttribute("src", "/render_image/plus");
            menu.style.display = "none";
        } else {  
            this.setAttribute("src", "/render_image/minus");
            menu.style.display = "block";
        }
            
        // make sure we store the (new) value in cookie
        toc[menuId] = menu.style.display;
        toc.store();

        return false;
    }
}

function initMenus() {
         
     // find all acutators in the page and initialize them
     
     var imgs = document.getElementsByTagName('img');
     for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].className.indexOf('actuator') != -1) {
            
             // we shouldn't be using sequence numbers, since they
             // can change - the number of topic menus displayed changes
             // all the time!  
             //
             // Instead we need to use the topic id itself.  That will ensure
             // that when user opens the "Literature" topic, the site remembers
             // the actual topic rather than where "Literature" was in the list.
             //
             var imgid = imgs[i].id;       // e.g. actuator13
             var topicid = imgid.substring(8); 
             initializeMenu("menu" + topicid, "actuator" + topicid);
        }   
     }
}

addLoadEvent(initMenus);




