﻿Type.registerNamespace("Infragistics.Web.UI");

/******************************************ANIMATION BASE******************************************/
$IG.AnimationBase = function(elem)
{
    this._element = elem;
    this._duration = 35; 
    this._tickInterval = 21;
    this._curveDepth = 2;
    if($util.IsFireFox)
        this._tickInterval = 31;
};  

$IG.AnimationBase.prototype = 
{
    /*********PUBLIC METHODS ******/
    play:function()
    {
        this.onBegin();
        this._time = 1
        if(this._animating == true)
            this.stop();
        this._animating = true;
        
        this._init();
        if(this._animating)
            this.__tick(true);
    }, 
    
    stop:function()
    {
        this._animating = false;
        clearInterval(this._timerId);
        this._timerId = undefined;        
    },
    /*********END PUBLIC METHODS ******/
    
    /*********EVENTS*******************/
    onBegin:function(){},
    onNext:function(){},
    onEnd:function(){},
    
    /*********END EVENTS***************/
    
    /*********PUBLIC PROPERTIES *******/
    get_duration:function(){return this._duration;},
    set_duration:function(ms)
    {
        this._duration = parseInt(ms/this._tickInterval);
        if(this._duration < 1 || ms <= 0)
            this._duration = 1; 
    },
    get_isAnimating:function(){return this._animating;},
    /*********END PUBLIC PROPERTIES *******/
    
    /*********PRIVATE METHODS *********/
    __tick:function(firstTime)
    {   
        this.onNext(); 
        this._next();
        if(this._animating)
        {
            this._time++;    
            if($util.IsFireFox && this._animating)
            {
                clearInterval(this._timerId);
                this._timerId = setInterval(Function.createDelegate(this, this.__tick), this._tickInterval);
            }
            
            if(firstTime && !$util.IsFireFox)
                this._timerId = setInterval(Function.createDelegate(this, this.__tick), this._tickInterval);       
        }
        else
            this.onEnd();
        
    },
    /*********END PRIVATE METHODS *********/
    
    /********Protected Virtual Methods ****/
    _init:function()
    {
        /* TO BE OVERRIDDEN ON DERIVED CLASS */
    },
    
    _next:function()
    {
        /* TO BE OVERRIDDEN ON DERIVED CLASS */
    },
    
    _calc:function(type, t, s, e, d)
    {
        /* t = current tick
           s = start position
           e = end position
           d = duration of animtation */
        var cd = this._curveDepth;        
        if(type == $IG.AnimationEquationType.Linear)
            return ((e - s)/ d) * t;  
        else if(type == $IG.AnimationEquationType.EaseIn)
            return (Math.pow(t,cd) * e)/ Math.pow(d,cd) + s; 
        else if(type == $IG.AnimationEquationType.EaseOut)
            return ((-Math.pow(t,cd) *e)/Math.pow(d,cd)) + ((2 * t *e)/d) + s;
        else if(type == $IG.AnimationEquationType.EaseInOut)
        {
            if((t/(d/2)) < 1) // is halfway through?
                return (Math.pow(t,cd)*(e/2))/Math.pow((d/2),cd) + s; // Ease In
            else
                return ((-Math.pow((t-(d/2)),cd) * (e/2))/Math.pow((d/2), cd)) +( (2 * (t-(d/2)) *(e/2))/(d/2)) + s + (e/2); // Ease out;
        }
    }
    /********END Protected Virtual Methods ****/
    
};
$IG.AnimationBase.registerClass("Infragistics.Web.UI.AnimationBase");
/******************************************END ANIMATION BASE**************************************/

/******************************************Bounce Animation**************************************/
$IG.OpacityAnimation = function(elem)
{
    $IG.OpacityAnimation.initializeBase(this, [elem]);
    
};

$IG.OpacityAnimation.prototype = 
{
    play:function( startOpacity, endOpacity, removeOpacityAtEnd)
    {
        this._startOpacity = startOpacity; 
        this._endOpacity = endOpacity;     
        this.removeOpacityAtEnd = removeOpacityAtEnd;    
        $IG.OpacityAnimation.callBaseMethod(this, "play");     
    },
    
    _init:function()
    {
        this._opacity = this._startOpacity; 
        $util.setOpacity(this._element, this._opacity);            
    },
    
    _next:function()
    {
        if(this._startOpacity < this._endOpacity)
        {
            this._opacity += ((this._endOpacity - this._startOpacity)/this._duration);
            $util.setOpacity(this._element, this._opacity);
            
            if(this._opacity >= this._endOpacity)
                this.stop();
        }
        else
        {
            this._opacity -= ((this._startOpacity - this._endOpacity)/this._duration);
            $util.setOpacity(this._element, this._opacity);
            
            if(this._opacity <= this._endOpacity)
                this.stop();
        }
    },

    stop:function()
    {
        if (this.removeOpacityAtEnd) 
        {
            if(this._element.style.removeAttribute)
                this._element.style.removeAttribute("filter")
            else
                this._element.style.removeProperty("opacity")
        }
        $IG.OpacityAnimation.callBaseMethod(this, "stop");
    }

};

$IG.OpacityAnimation.registerClass("Infragistics.Web.UI.OpacityAnimation", $IG.AnimationBase);
/******************************************END Opacity Animation**********************************/


/******************************************AnimationEquationType ENUM******************************/
$IG.AnimationEquationType = function (){}
$IG.AnimationEquationType.prototype = 
{
    Linear: 0,

    EaseIn:1,
    EaseOut:2,
    EaseInOut:3

};
$IG.AnimationEquationType.registerEnum("Infragistics.Web.UI.AnimationEquationType");
/******************************************END AnimationEquationType ENUM**************************/
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();