Site.Widgets.CollapsePanel = Class.create
(
    Site.Base,
    {
        initialize: function($super, button, panel, options)
        {
            $super();
            
            this.panel = panel;
            this.button = button;
            
            this.setOptions
            (
                options,
                {
                    openClassName: "open",
                    closedClassName: "closed",
                    durationClose: 0, /* set to 0 to turn off animation completely */
                    durationOpen: 0
                }
            );
            
            this.addBlocks("panelAfterSlideUp", "panelAfterSlideDown");
            this.addObservers("buttonOnClick");
            
            addEvent(this.button, "click", this.observers.buttonOnClick);
            
            this.opened = this.panel.visible();
        },
        
        buttonOnClick: function(event)
        {
            var element = Event.element(event);
            
            if (element.blur)
                element.blur();

            this.toggle();
            
            Event.stop(event);
        },
        
        toggle: function()
        {
            if (this.opened)
                this.close();
            else
                this.open();
        },
        
        open: function()
        {
            this.button.removeClassName(this.options.closedClassName);
            this.button.addClassName(this.options.openClassName);

            if (this.options.beforeOpen)
                this.options.beforeOpen(this.panel);

            this.opened = true;
            
            if (this.options.durationOpen > 0)
            {
                new Effect.SlideDown(this.panel, {duration: this.options.durationOpen, afterFinish: this.blocks.panelAfterSlideDown });
            }
            else
            {
                this.panel.show();
                this.afterOpen();
            }
        },
        
        close: function()
        {
            this.button.removeClassName(this.options.openClassName);
            this.button.addClassName(this.options.closedClassName);
            
            this.opened = false;

            if (this.options.beforeClose)
                this.options.beforeClose(this.panel);
                
            if (this.options.durationClose > 0)
            {
                new Effect.SlideUp(this.panel, {duration: this.options.durationClose, afterFinish: this.blocks.panelAfterSlideUp } );
            }
            else
            {
                this.panel.hide();
                this.afterClose();
            }
        },
        
        afterOpen: function()
        {
            if (this.options.afterOpen)
                this.options.afterOpen(this.panel);
        },
        
        afterClose: function()
        {
            if (this.options.afterClose)    
                this.options.afterClose(this.panel);
        },
        
        panelAfterSlideUp: function()
        {
            this.afterClose();
        },
        
        panelAfterSlideDown: function()
        {
            this.afterOpen();
        }
    },
    {
        
    }
);
