﻿Type.registerNamespace("Infragistics.Web.UI");


$IG.AnimationBase = function(elem)
{
    ///<summary>
    /// A base class that provides the ability to create a custom animation.
    ///</summary>
    this._element = elem;
    this._duration = 35; 
    this._tickInterval = 21;
    this._curveDepth = 2;
    if($util.IsFireFox)
        this._tickInterval = 31;
};

$IG.AnimationBase.prototype =
{
	
	play: function()
	{
		///<summary>
		/// Starts the Animation
		///</summary>            
		this.onBegin();
		this._time = 1
		if (this._animating == true)
			this.stop();
		this._animating = true;

		this._init();
		if (this._animating)
			this.__tick(true);
	},

	stop: function()
	{
		///<summary>
		/// Ends the Animation.
		///</summary>    
		this._animating = false;
		clearInterval(this._timerId);
		this._timerId = undefined;
	},
	

	
	onBegin: function()
	{
		///<summary>
		/// Should be overriden on the base class.
		/// Notifies the Animation that it is about to Begin
		///</summary>    
	},
	onNext: function()
	{
		///<summary>
		/// Should be overriden on the base class.
		/// Fired for each tick of the Animation
		///</summary>    
	},
	onEnd: function()
	{
		///<summary>
		/// Should be overriden on the base class.
		/// Notifies the Animation that the animation has ended.
		///</summary>    
	},

	

	
	get_duration: function()
	{
		///<summary>
		/// Returns the duration of the animation in ticks.
		/// Meaning that the onNext method will be fired for each tick.
		/// The default Value is 35.
		///</summary>    
		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()
	{
		///<summary>
		/// Returns true if the animation is currently occurring.
		///</summary> 
		return this._animating;
	},
	

	
	__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();

	},
	

	
	_init: function()
	{
		
	},

	_next: function()
	{
		
	},

	_calc: function(type, t, s, e, d)
	{
		
		var cd = this._curveDepth;
		if (type == $IG.AnimationEquationType.Linear)
			return ((e - s) / d) * t + s;
		else if (type == $IG.AnimationEquationType.EaseIn)
			return (Math.pow(t, cd) * (e - s)) / Math.pow(d, cd) + s;
		else if (type == $IG.AnimationEquationType.EaseOut)
			return ((-Math.pow(t, cd) * (e - s)) / Math.pow(d, cd)) + ((2 * t * (e - s)) / d) + s;
		else if (type == $IG.AnimationEquationType.EaseInOut)
		{
			if (t < (d / 2)) // is halfway through?
				return (Math.pow(t, cd) * ((e - s) / 2)) / Math.pow((d / 2), cd) + s; // Ease In
			else
				return ((-Math.pow((t - (d / 2)), cd) * ((e - s) / 2)) / Math.pow((d / 2), cd)) + ((2 * (t - (d / 2)) * ((e - s) / 2)) / (d / 2)) + (s + e) / 2; // Ease out;
		}
		else if (type == $IG.AnimationEquationType.Bounce)
		{
			var ts = (t /= d) * t;
			var tc = ts * t;
			return (e - s) * (44.25 * tc * ts + -138.25 * ts * ts + 156.5 * tc + -76.5 * ts + 15 * t) + s;
		}
	},

	dispose: function()
	{
		this.stop();
		this._element = null;
	}

	

};
$IG.AnimationBase.registerClass("Infragistics.Web.UI.AnimationBase");



$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 != null && typeof(this._element) != undefined)
            {
                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);




$IG.AnimationEquationType = function ()
{
    ///<summary>
    /// The type of calculation that an animation will use to determine it's next position..
    ///</summary> 
}
$IG.AnimationEquationType.prototype = 
{
    Linear: 0,
    EaseIn:1,
    EaseOut:2,
    EaseInOut:3,
    Bounce: 4

};
$IG.AnimationEquationType.registerEnum("Infragistics.Web.UI.AnimationEquationType");

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();