

dojo.lang.declare(
    'ElementExpander',
    null,
    function (/*String*/ className, /*Boolean*/showOnlyOneAtATime) {
        this.className = className;
        this.showOnlyOneAtATime = showOnlyOneAtATime;
        this.setup();
        
    },
    {
        
        showOnlyOneAtATime  : false,
        showingElements     : new Array(),
        elements            : new Array(),
        lockedElements      : new Array(),
        
        setup : function()
        {
            elements = dojo.html.getElementsByClass(this.className);
            for (i = 0; i < elements.length; i++) {
                if (elements[i].id) {
                    this.elements[elements[i].id] = elements[i];
                }
            }
        },
        
        click : function(element)
        {
            if (this._isShowing(element)) {
                this.hide(element);
            } else {
                this.show(element);
            }
        },
        
        show : function(element)
        {
            animation = null;
            if (this.showOnlyOneAtATime && this.showingElements.length > 0) {
                animation = this.hideShowing(false);
            }
            
            if (this._isLocked(element)) {
                return;
            }

            this._lockElement(element);
            
            animation = (!animation) ? dojo.lfx.html.wipeIn(element, 200) : dojo.lfx.combine(animation, dojo.lfx.html.wipeIn(element, 200));
            animation.connect("onEnd", dojo.lang.hitch(this, function() { this._unlockElement(element); this._setShowing(element); }));
            animation.play();
            
        },
        
        hideShowing : function(playAnimation)
        {
            if (playAnimation == null) {
                playAnimation = true;
            }
            
            animation = null;
            for (i = 0; i < this.showingElements.length; i++) {
                
                element_name = this.showingElements[i];
                element      = document.getElementById(element_name);

                if (this._isShowing(element) && (!this._isLocked(element)) ) {
                    this._lockElement(element);
                    animation = (!animation) ? dojo.lfx.html.wipeOut(element, 200) : dojo.lfx.combine(animation, dojo.lfx.html.wipeOut(element, 200));
                    animation.connect("onEnd", dojo.lang.hitch(this, function() { this._unlockElement(element); this._unsetShowing(element); }));
                }
            }
            
            if (animation && playAnimation) {
                animation.play();
            } else {
                return animation;
            }
        },
        
        hide : function(element)
        {
            
            if (!this._isLocked(element) && this._isShowing(element)) {
                this._lockElement(element);
                animation = dojo.lfx.html.wipeOut(element, 200);
                animation.connect("onEnd", dojo.lang.hitch(this, function() { this._unlockElement(element); this._unsetShowing(element); }));
                animation.play();
            }
        },
        
        _setShowing : function(element)
        {
            this.showingElements.push(element.id);
        },
        
        _unsetShowing : function(element)
        {
            for (i = 0; i < this.showingElements.length; i++) {
                if (this.showingElements[i] == element.id) {
                    this.showingElements.splice(i, 1);
                }
            }
        },
        
        _isShowing : function(element)
        {
            for (i = 0; i < this.showingElements.length; i++) {
                if (this.showingElements[i] == element.id) {
                    return true;
                }
            }
            
            return false;
        },
        
        _lockElement  : function(element)
        {
            this.lockedElements.push(element.id);
        },
        
        _unlockElement : function(element)
        {
            for (i = 0; i < this.lockedElements.length; i++) {
                if (this.lockedElements[i] == element.id) {
                    this.lockedElements.splice(i, 1);
                }
            }
        },
        
        _isLocked : function(element)
        {

            for (i = 0; i < this.lockedElements.length; i++) {
                if (this.lockedElements[i] == element.id) {
                    return true;
                }
            }
            
            return false;
        }
        
        
    }
);
