var Block = Class.create({
    initialize: function( elements ) {
        this.elements = elements;
        this.visible = false;
    },

    appear: function() {
        if (this.visible) {
            return;
        }

        for(var i = 0; i < this.elements.length; i++) {
            this.elements[i].appear();
        }
        this.visible = true;
    },

    disappear: function() {
        if (!this.visible) {
            return;
        }

        for(var i = 0; i < this.elements.length; i++) {
            this.elements[i].fade();
        }
        this.visible = false;
    },

    hide: function() {
        for(var i = 0; i < this.elements.length; i++) {
            this.elements[i].hide();
        }
        this.visible = false;
    }
});

var Select = Class.create({
    initialize: function( element ) {
        this.groupMembers = false;
        this.element = element;
        this.element.observe('click', this.onClick.bind(this));
        if (this.element.classNames().toArray().last() == 'show') {
            this.block = false;
            return;
        }
        this.block = new Block($$('.' + this.element.classNames().toArray().last()).without(this.element));
        this.block.hide();
    },

    onClick: function() {
        if (this.block) {
            this.block.appear();
        }
        this.groupMembers.each(function( obj ) {
            if (obj.block) {
                obj.block.disappear();
            }
        });
    }
});

function init() {
    var selectors = new Array();
    for (var i = 0; i < $$('.show').length; i++) {
        selectors[i] = new Select($$('.show')[i]);
    }

    for (var i = 0; i < selectors.length; i++) {
        selectors[i].groupMembers = selectors.without(selectors[i]);
    }
}

document.observe("dom:loaded", init);

