var Stretcher = new Class({
    getOptions : function() {
        return {
            'duration' : 750,
            'transition' : Fx.Transitions.backOut,
            'openDelay' : -1,
            'closeDelay' : -1
        };
    },

    initialize : function(src,options) {
        this.setOptions(this.getOptions(),options);
        this.src = $(src);
        this.src.setStyles({
            visibility: 'hidden',
            display: "block"
        });
        this.initHeight = this.src.getSize().size.y;
        this.expanded = false; this.inprocess = false;
        this.effect = new Fx.Style(this.src,"height",{'duration':this.options.duration,'transition':this.options.transition});
        this.effect.addEvent("onComplete",this._onCompleteFX.bind(this));
        this.closeSrc = $(src+"-close");
        this.counterSrc = $(src+"-counter");
        if ($chk(this.close)) {
            this.closeSrc.addEvent("click",this.close.bindWithEvent(this));
        }
        this.hide();
        this.src.setStyle('visibility', 'visible');
        if (this.options.openDelay > -1) {
            this.expand.delay(this.options.openDelay,this);
        }
        if (this.options.closeDelay > -1) {
            this.addEvent("onExpandComplete",this._startCountDown.bind(this));
            if (this.counterSrc) {
                this.count = Math.round(this.options.closeDelay / 1000);
                this.counterSrc.setHTML(this.count + " sec");
            }
        }
    },

    expand : function() {
        if (this.inprocess) this._stop();
        this.src.setStyle("display","block");
        this.expanded = true;
        this.fireEvent("onExpand",10);
        this.effect.start(0,this.initHeight);
    },

    collapse : function() {
        if (this.inprocess) this._stop();
        this.expanded = false;
        this.fireEvent("onCollapse",10);
        this.effect.start(0);
    },

    hide : function() {
        if (this.inprocess) this._stop();
        this.expanded = false;
        this.fireEvent("onHide",10);
        this.effect.hide();
        this.src.setStyle("display","none");
    },

    close : function(event) {
        this.collapse();
    },

    _onCompleteFX : function() {
        this.inprocess = false;
        if(this.expanded) {
            this.fireEvent("onExpandComplete",10);
        } else {
            this.fireEvent("onCollapseComplete",10);
            this.src.setStyle("display","none");
        }
    },

    _stop : function() {
        this.inprocess = false;
        this.effect.stop();
    },

    _startCountDown : function() {
        if (this.options.closeDelay > -1) {
            if (this.counterSrc) {
                this.counterRef = this._countDown.periodical(1000,this);
            }
            this.collapse.delay(this.options.closeDelay,this);
        }
    },
    _countDown : function() {
        this.count--;
        if (this.count <= 0) {
            $clear(this.counterRef);
        }
        this.counterSrc.setHTML(this.count + " sec");
    }

});

Stretcher.implement(new Events);
Stretcher.implement(new Options);