// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
    if (element.addEventListener)
    element.addEventListener(type, handler, false); else {
        if (! handler.$$guid) handler.$$guid = addEvent.guid++;
        if (! element.events) element.events = {
        };
        var handlers = element.events[type];
        if (! handlers) {
            handlers = element.events[type] = {
            };
            if (element[ 'on' + type]) handlers[0] = element[ 'on' + type];
            element[ 'on' + type] = handleEvent;
        }
        
        handlers[handler.$$guid] = handler;
    }
}
addEvent.guid = 1;

function removeEvent(element, type, handler) {
    if (element.removeEventListener)
    element.removeEventListener(type, handler, false); else if (element.events && element.events[type] && handler.$$guid)
    delete element.events[type][handler.$$guid];
}

function handleEvent(event) {
    event = event || fixEvent(window.event);
    var returnValue = true;
    var handlers = this .events[event.type];
    
    for (var i in handlers) {
        if (! Object.prototype[i]) {
            this .$$handler = handlers[i];
            if (this .$$handler(event) === false) returnValue = false;
        }
    }
    
    if (this .$$handler) this .$$handler = null;
    
    return returnValue;
}

function fixEvent(event) {
    event.preventDefault = fixEvent.preventDefault;
    event.stopPropagation = fixEvent.stopPropagation;
    return event;
}
fixEvent.preventDefault = function () {
    this .returnValue = false;
}
fixEvent.stopPropagation = function () {
    this .cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (! window.addEventListener) {
    document.onreadystatechange = function () {
        if (window.onload && window.onload != handleEvent) {
            addEvent(window, 'load', window.onload);
            window.onload = handleEvent;
        }
    }
}

//This is a utility function to get an array of elements with a specific class name
//I copped it from John Resig's "Pro Javascript Techniques." But the book didn't put
//.className in the crucial 'if' and so was returning an empty array. I fixed that.
function hasClass(name, type) {
    var r = [];
    // Locate the class name (allows for multiple class names)
    var re = new RegExp("(^|\\s)" + name + "(\\s|$)");
    
    // Limit search by type, or look through all elements
    var k = document.getElementsByTagName(type || " * ");
    for (var j = 0; j < k.length; j++)
    // If the element has the class, add it for return
    if (re.test(k[j].className)) r.push(k[j]);
    
    // Return the list of matched elements
    return r;
};
//here begins my contribution to this mess
//if javascript is enabled, this will hide the tree and change the image
function treecollapse() {
    var list = hasClass("show", "ul");
    for (var i = 0; i < list.length; i++) {
    list[i].className = 'hide';
    list[i].previousSibling.firstChild.src = '../cssimages/plus.gif';
    };
};

//Attaches the toggle function to the onclick event for the a tags
function attachToggle() {
    var anchors = hasClass("clk", "a");
    for (var i = 0; i < anchors.length; i++)
    addEvent(anchors[i], "click", toggle);
};

//The meat and potatoes function that does the magic
function toggle(e) {
    re = /plus.gif$/
    this .nextSibling.className =(this .nextSibling.className == 'show') ? 'hide' : 'show';
    if (this .firstChild.src.match(re)) {
        this .firstChild.src = '../cssimages/minus.gif';
    } else {
        this .firstChild.src = '../cssimages/plus.gif';
    };
    e.preventDefault();
};

addEvent(window, "load", function () {
    treecollapse();
    attachToggle();
});