SortableList = Class.create();
SortableList.prototype = {
    initialize: function(id, config) {
        Object.extend(this, new Widget(id, config));
        
        this.setupWidget = function(from_ajax) {
            // alert('sortable list');
            if (from_ajax === true) oWidget.publish("/sortablelist/refresh", this);
            this.main = $(this.id);
            var next = this.main.childNodes[0];
            while (next) {
                var n = next;
                next = next.nextSibling;
                if (!n.tagName || n.tagName.toLowerCase() != "div") {
                    this.main.removeChild(n);
                    continue;
                }

                // find div with 'closebox' attribute
                var m = document.getElementsByClassName("closebox", n);
                if (m && m.length == 1) n.closebox = m[0];

                var m = document.getElementsByClassName("closeimage", n);
                if (m && m.length == 1) n.closeimage = m[0];

            }
            
            if (from_ajax !== true) {
                for (var i=0; i<this.config.closed_items.length; i++) {
                    this.toggleEl(this.config.closed_items[i]);
                }
            }

            this.main.style.visibility = "";
        };

        Event.observe(window, "load", this.setupWidget.bind(this));
    },

    getIndex: function(el) {
        while (el.parentNode) {
            var pos = $A(this.main.childNodes).indexOf(el);
            if (pos >= 0) return pos;
            el = el.parentNode;
        }
        throw new Error("SortableList controls outside of sortable list...");
    },
    
    up: function(e) {
        var pos = this.getIndex(e);
        if (pos == 0) return;

        var el = this.main.childNodes[pos];
        el.parentNode.insertBefore(el, el.previousSibling);
        
        this.ajaxRequest("up", {pos: pos});
    },

    down: function(e) {
        var pos = this.getIndex(e);
        if (pos == this.main.childNodes.length-1) return;
        this.ajaxRequest("down", {pos: pos});

        var el = this.main.childNodes[pos];
        var parent = el.parentNode;
        if (el.nextSibling.nextSibling) {
            parent.insertBefore(el, el.nextSibling.nextSibling);
        } else {
            parent.appendChild(el);
        }
        
    },

    toggleEl: function(pos) {
        var el = this.main.childNodes[pos];
        if (Element.hasClassName(el, "closebg")) {
            Element.removeClassName(el, "closebg");
            if (el.closeimage) el.closeimage.src = "/img/box_close.gif";
        } else {
            Element.addClassName(el, "closebg");
            if (el.closeimage) el.closeimage.src = "/img/box_open.gif";
        }
        
    },
    
    toggle: function(e) {
        var pos = this.getIndex(e);
        
        if (!this.main.childNodes[pos] || !this.main.childNodes[pos].closebox) return;

        this.ajaxRequest("toggle", {pos: pos});

        this.toggleEl(pos);
    }
}
