<!--

/**
 * @author Ferdy Perdaan
 * @copyright Copyright (c) 2010, WFIT
 * @version 1.0
 */
/** ANIMATION ----------------------------------------------------------------------------------------------------------------------------------- **/
function Animation(){return {
    _init : function(config, handler){
        this.meta    = {inAnimation:false, handler:handler};
        this.config  = config;
        this.timeout = null;
    },

    start : function(){
        if(!this.inAnimation()){
            if(this.meta.handler && this.meta.handler.beforeStart)          this.meta.handler.beforeStart(this);

            this._start();
            this.meta.inAnimation = true;

            if(this.meta.handler && this.meta.handler.afterStart)           this.meta.handler.afterStart(this);

            this.date = new Date();
        }
    },

    stop : function(){
        if(this.inAnimation()){
            if(this.meta.handler && this.meta.handler.beforeStop)           this.meta.handler.beforeStop(this);
          
            this._stop();
            this.meta.inAnimation = false;

            if(this.timeout !== null){
                clearTimeout(this.timeout);
                this.timeout = null;
            }

            if(this.meta.handler && this.meta.handler.afterStop)            this.meta.handler.afterStop(this);
        }
    },

    inAnimation : function(){
        return this.meta.inAnimation;
    }
}}

/** FADE ANIMATION ------------------------------------------------------------------------------------------------------------------------------ **/
FadeAnimation.prototype = new Animation();

function FadeAnimation(element, config, handler){
    this._init(config, handler);
    this.meta.element  = element;
    this.meta.fadeAway = (Element.getStyle(this.meta.element, 'opacity') == 1);
    this.config        = {speed:100, fade:{min:0, max:100, amount:2}}.merge(this.config);
}

FadeAnimation.prototype._start = function(){
    this.timeout = setTimeout(callbackTimeout(this, this._animate), this.config.speed); 
}

FadeAnimation.prototype._animate = function(){
    if(this.inAnimation()){
        Element.setOpacity(this.meta.element, (Element.getStyle(this.meta.element, 'opacity') * 100) - (this.config.fade.amount * (this.meta.fadeAway ? 1 : -1)));
        
        if(Element.getStyle(this.meta.element, 'opacity') <= this.config.fade.min / 100 || Element.getStyle(this.meta.element, 'opacity') >= this.config.fade.max / 100){
            this.meta.fadeAway = !this.meta.fadeAway;
            this.stop();
        }else{
            this.timeout = setTimeout(callbackTimeout(this, this._animate), this.config.speed); 
        }
    }
}

FadeAnimation.prototype._stop = function(){
    if(Element.getStyle(this.meta.element, 'opacity') > this.config.fade.max / 100)
    	Element.setOpacity(this.meta.element, this.config.fade.max);
    else if(Element.getStyle(this.meta.element, 'opacity') < this.config.fade.min / 100)
    	Element.setOpacity(this.meta.element, this.config.fade.min);
}

/** EXPAND ANIMATION ---------------------------------------------------------------------------------------------------------------------------- **/
StretchAnimation.prototype = new Animation();

function StretchAnimation(element, config, handler, size){
    this._init(config, handler);
    
    var dimension    = Element.getDimension(element);
    this.config      = {speed:50, x:{stretch:15, enabled:true}, y:{stretch:2, enabled:true}}.merge(this.config);
    this.element     = element;
    this.meta.size   = (size ? size : Element.getDimension(element));
    this.meta.expand = (dimension.width >= this.meta.size.width && dimension.height >= this.meta.size.height);
}

StretchAnimation.prototype._start = function(){
    this.timeout = setTimeout(callbackTimeout(this, this._animate), this.config.speed); 
}

StretchAnimation.prototype._animate = function(){
    if(this.inAnimation()){
        var dimension = Element.getDimension(this.element);
        
        if(this.config.x.enabled && dimension.width < this.meta.size.width && this.meta.expand || dimension.width > 0 && !this.meta.expand){
            dimension.width += this.config.x.stretch * (this.meta.expand ? 1 : -1);
            if(dimension.width > this.meta.size.width)
                this.element.style.width = this.meta.size.width + 'px';
            else if(dimension.width < 0)
                this.element.style.width = '0px';
            else
                this.element.style.width = dimension.width + 'px';
        }

        if(this.config.y.enabled && dimension.height < this.meta.size.height && this.meta.expand || dimension.height > 0 && !this.meta.expand){
            dimension.height += this.config.x.stretch * (this.meta.expand ? 1 : -1);
            if(dimension.height > this.meta.size.height)
                this.element.style.height = this.meta.size.height + 'px';	
            else if(dimension.height < 0)
                this.element.style.height = '0px';							
            else
                this.element.style.height = dimension.height + 'px';			
        }
            
        if(dimension.width >= this.meta.size.width && dimension.height >= this.meta.size.height || dimension.width <= 0 && dimension.height <= 0){
            this.meta.expand = !this.meta.expand;
            this.stop();
        }else{
            this.timeout = setTimeout(callbackTimeout(this, this._animate), this.config.speed); 
        }
    }
}

StretchAnimation.prototype._stop = function(){

}

/** SOME ANIMATION ------------------------------------------------------------------------------------------------------------------------------ **/
SomeAnimation.prototype = new Animation();

function SomeAnimation(element, config, handler){

}

SomeAnimation.prototype._start = function(){

}

SomeAnimation.prototype._animate = function(){

}

SomeAnimation.prototype._stop = function(){

}

//-->
