if (typeof onepica == "undefined" || !onepica) {
    var onepica = {};
}

onepica.Tree = Class.create({
    /**
    * Constructor
    *
    * @param string containerId: the ID of the container element
    */
    initialize: function(containerId) {
        this.container = $(containerId);
        this.spans = $$("#" + containerId + " span");
        for (var i = 0; i < this.spans.length; i++) {
            Event.observe(this.spans[i], "click", this.showHideLeaf);
        }
    },

    /**
    * Shows or hides child elements.
    *
    * @param Event e
    */
    showHideLeaf: function(e) {
        var parent = $(this.parentNode);
        if (parent.down("ul li").hasClassName("open")) {
            this.removeClassName("show");
            var index = 0;
            while (el = parent.down("ul li", index)) {
                if (el.parentNode.parentNode.identify() == parent.identify()) // only direct descendents
                    el.removeClassName("open");
                index++;
            }
        }
        else {
            this.addClassName("show");
            var index = 0;
            while (el = parent.down("ul li", index)) {
                if (el.parentNode.parentNode.identify() == parent.identify()) // only direct descendents
                    el.addClassName("open");
                index++;
            }
        }
    }
});


onepica.Tree2 = Class.create({
    /**
    * Constructor
    *
    * @param string containerId: the ID of the container element
    */
    initialize: function(container) {
        this.container = container;
        this.spans = this.container.select('span');
        for (var i = 0; i < this.spans.length; i++) {
            Event.observe(this.spans[i], "click", this.showHideLeaf);
        }
    },

    /**
    * Shows or hides child elements.
    *
    * @param Event e
    */
    showHideLeaf: function(e) {
        var parent = $(this.parentNode);
        if (parent.down("ul li").hasClassName("open")) {
            this.removeClassName("show");
            var index = 0;
            while (el = parent.down("ul li", index)) {
                if (el.parentNode.parentNode.identify() == parent.identify()) // only direct descendents
                    el.removeClassName("open");
                index++;
            }
        }
        else {
            this.addClassName("show");
            var index = 0;
            while (el = parent.down("ul li", index)) {
                if (el.parentNode.parentNode.identify() == parent.identify()) // only direct descendents
                    el.addClassName("open");
                index++;
            }
        }
    }
});
