var Tongue = new Class({
    Implements: [Options, Events],

    options: {
        length: 0,
        prefer: 'plus',
        property: 'height',
        elements: {
            prefer: null
        }
    },

    initialize: function(element, options) {
        this.setOptions(options);
        this.element = $(element);
        this.element.store('tongue', this);
        this.length = this.options.length;
        this.prefer = this.options.prefer;
        this.fx = new Fx.Tween(this.element, {
            property: this.options.property,
            duration: 400,
            link: 'chain'
        });
    },

    before: function(prefer) {
        prefer = prefer || this.prefer;
        if (this.prefer != prefer) {
            return (function(fn) {
                this.hide().chain((function() {
                    var el = this.options.elements.prefer;
                    el.removeClass(this.prefer).addClass(prefer);
                    fn.run([], this);
                    this.prefer = prefer;
                }).bind(this));
            }).bind(this);
        } else {
            return (function(fn) {
                this.options.elements.prefer.addClass(prefer);
                fn.run([], this);
            }).bind(this);
        }
    },

    expand: function(prefer) {
        this.fireEvent('expand');
        var to = this.length;
        this.before(prefer)(function() {
            this.fx.start(to);
        });
    },

    fold: function(prefer) {
        this.fireEvent('fold');
        var to = 30;
        this.before(prefer)(function() {
            this.fx.start(to);
        });
    },

    hide: function() {
        this.fireEvent('hide');
        return this.fx.start(20);
    }
});

