var YOOaccordion = new Class({
    initialize: function (togglers, elements, options) {
        this.setOptions({
            open: 'first',
            allowMultipleOpen: false,
            fade: true,
            fadeDuration: 600,
            transition: Fx.Transitions.linear,
            duration: 400
        },
        options);
        this.togglers = togglers;
        this.elements = elements;
        this.elementFx = [];
        this.elementVisible = [];
        this.togglers.each(function (tog, i) {
            tog.addEvent('click', function () {
                this.toggleSection(i)
            }.bind(this))
        },
        this);
        this.elements.each(function (el, i) {
            this.elementFx[i] = new Fx.Slide(el, this.options);
            if (! (this.options.allowMultipleOpen && this.options.open == 'all')) this.hide(i)
        },
        this);
        if (this.options.open == 'first')(function () {
            this.slideIn(0)
        }).delay(1, this)
    },
    toggleSection: function (iToToggle) {
        if (!this.options.allowMultipleOpen) {
            this.elements.each(function (el, i) {
                if (this.elementVisible[i] && i != iToToggle) this.slideOut(i)
            },
            this)
        }
        this.toggle(iToToggle)
    },
    toggle: function (i) {
        this.elementFx[i].toggle().chain(function () {
            this.elementVisible[i] = (this.elementVisible[i] + 1) % 2
        }.bind(this));
        if (this.options.fade) this.fade(i)
    },
    slideIn: function (i) {
        this.elementFx[i].slideIn().chain(function () {
            this.elementVisible[i] = 1
        }.bind(this));
        if (this.options.fade) this.fade(i)
    },
    slideOut: function (i) {
        this.elementFx[i].slideOut().chain(function () {
            this.elementVisible[i] = 0
        }.bind(this));
        if (this.options.fade) this.fade(i)
    },
    show: function (i) {
        this.elementVisible[i] = 1;
        this.elementFx[i].show()
    },
    hide: function (i) {
        this.elementVisible[i] = 0;
        this.elementFx[i].hide()
    },
    fade: function (i) {
        var fx = new Fx.Styles(this.elements[i], {
            'duration': this.options.fadeDuration,
            'wait': false
        });
        fx.start({
            'opacity': [this.elementVisible
                [i], (this.elementVisible[i] + 1) % 2]
        })
    }
});
YOOaccordion.implement(new Options);
