/*
This requires jQuery, for the document.ready() feature
It could also take advantage of other parts of jQuery, but it doesn't

This script handles any <ul id='collapsed-submenus'>.  It will collapse sub-lists ASAP.  Then
when clicking on a menu item it will expand its sub-list, and collapse all others.

*/

// modified from http://andrewdupont.net/refresh/js/
function _log(type, args) {
  for (var i = 1; i < arguments.length; i++) {
    var arg = arguments[i];
    if (window.console) {
        if (window.console[type]) {
            window.console[type](arg);
        } else {
            window.console.log(arg);
        }
    }
    else if (window.opera) { window.opera.postError(arg); }
    else if (window.Log) {
       self._logger = self._logger || new Log(Log.INFO, Log.popupLogger);
       self._logger[type](arg);
    } else {
        window.status = arg;
    }
  }
}

function _error() { _log("error", arguments); }
function _warn()  { _log("warn",  arguments); }
function _info()  { _log("info",  arguments); }
function _debug() { _log("debug", arguments); }

function collapseAll() {
    if (document.getElementById&&document.getElementById("collapsed-submenus")) {
        navRoot = document.getElementById("collapsed-submenus");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                // http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
                if (! new RegExp('\\b' + 'hidden-submenu' + '\\b').test(node.className)) {
                    node.className+=" hidden-submenu";
                }
            }
        }
    }
}

attachOnClickHandlersToCollapsableMenu = function() {
    if (document.getElementById&&document.getElementById("collapsed-submenus")) {
        collapseAll();
        navRoot = document.getElementById("collapsed-submenus");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                has_children = false;
                for (j=0; j<node.childNodes.length; j++) {
                    nestedNode = node.childNodes[j];
                    if (nestedNode.nodeName=="UL") {
                        has_children = true;
                        break;
                    }
                }
                if (has_children) {
                    node.onclick=function() {
                        // 'this' is the source of the event
                        _debug(this.parentNode.id);
                        _debug(this.className);
                        if (new RegExp('\\b' + 'hidden-submenu' + '\\b').test(this.className)) {
                            // check to see if a main menu item; otherwise: ignore clicks on the subitems within it
                            
                            collapseAll();
                            // http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
                            rep=this.className.match(' hidden-submenu')?' '+"hidden-submenu":"hidden-submenu";
                            this.className=this.className.replace(rep,'');
                            return false;
                        }
                        return true;
                    }
                }
            }
        }
    }
}

// as soon as DOM is loaded, before images et. al. load
$(document).ready(attachOnClickHandlersToCollapsableMenu);
