var Accordion = function(id)
{
    this.menu = $(id);
    this.togglers = null;
    this.contents = null;

    this.init = function()
    {
        // Check if menu was found
        if ( this.menu == null ) return false;
        // Collect togglers and contents
        this.togglers = this.menu.select('h2');
        this.contents = this.menu.select('div');
        // Add click handlers
        this.appendHandlers();
        // Hide all contents
        this.contents.each(function(item) {
            Effect.BlindUp(item, {
                duration:0
            });
        });
        // Open the active one (if presented)
        this.togglers.each(function(item) {
            if (item.hasClassName('active')) {
                item.fire('click');
            }
        }.bind(this));

        return true;
    };

    this.getContents = function(toggler)
    {
        return toggler.next('div');
    };

    this.toggle = function(event)
    {
        Event.stop(event);
        // Check if this element has no contents, than it's a simple link. Do nothing.
        var toggler = $(Event.element(event));
        var contents = this.getContents(toggler);
        /*
        if (contents == undefined) return false;
        */
        // Stop propagation
        // Hide all containers
        /*
        this.contents.each(function(item) {
            Effect.BlindUp(item);
        });
        */
        // Show needed content
        if (contents != undefined) {
            if (!toggler.hasClassName('active')) {
                Effect.BlindDown(contents);
                // Remove "active" class from togglers
                /*
                this.togglers.each(function(item) {
                    item.removeClassName('active');
                    item.up().up().removeClassName('active');
                });
                */
                // Add "active" class to clicked toggler
                toggler.addClassName('active');
                toggler.up().up().addClassName('active');
            } else {
                Effect.BlindUp(contents);
                toggler.removeClassName('active');
                toggler.up().up().removeClassName('active');
            }
        }
    };

    this.appendHandlers = function()
    {
        this.togglers.each(function(item) {
            item.observe('click', function(event) {
                this.toggle(event);
            }.bind(this));
        }.bind(this));
    };
}