var SO = Object.extend({
    UnfoldingContainer: Class.create({
        initialize: function(toggleElement, container, options) {
            this.toggleElement= $(toggleElement);
            this.visible= false;

            this.options= Object.extend({
                activeClass: 'accordion_toggle_active',
                animated: true,
                duration: 0.5,
                transition: Effect.Transitions.sinoidal
            }, options || {});

            this.container= this.options.animated ? $(container).setStyle({height: '0px'}) : $(container).hide();

            if (this.options.animated) {
                this.effectScopeId= this.container.identify();
                this.effectDefaults= {
                    duration: this.options.duration,
                    scaleContent: false,
                    transition: this.options.transition,
                    queue: {
                        position: 'end',
                        scope: this.effectScopeId
                    },
                    scaleX: false,
                    scaleY: true,
                    scaleMode: {
                        originalHeight: this.container.scrollHeight,
                        originalWidth: this.container.scrollWidth
                    }
                };
            }

            this.toggleElement.observe('click', this.toggle.bind(this));

            Event.observe(window, 'unload', this.destroy.bind(this));
        },

        destroy: function() {
            this.toggleElement.stopObserving('click');
            this.toggleElement= this.container= null;
        },

        toggle: function() {
            return this.visible ? this.fold() : this.unfold();
        },

        fold: function() {
            this.toggleElement.removeClassName(this.options.activeClass);
            if (this.options.animated) {
                var effectOptions= Object.extend(this.effectDefaults, {scaleFrom: 100});

                new Effect.Scale(this.container, 0, effectOptions);
            } else {
                this.container.hide();
            }
            this.visible= false;
        },

        unfold: function() {
            this.toggleElement.addClassName(this.options.activeClass);
            if (this.options.animated) {
                var effectOptions= Object.extend(this.effectDefaults, {scaleFrom: 0});

                new Effect.Scale(this.container, 100, effectOptions);
            } else {
                this.container.show();
            }
            this.visible= true;
        }
    })
}, SO || {});
