/*
 * $Id: animation.js 56115 2008-12-04 22:18:31Z kbaxter $
 * $URL: https://build/subversion/DestinationSearch/ds-core-web/tags/ds-core-web-2.0.114/src/main/webapp/js/animation.js $
 */
/**
 * @class LMI.Animation
 */
(function() {
    LMI.Animation = function() {
        this.init();
    };
    LMI.Animation.prototype = {
        /**
         * @method init
         */
        init: function() {
            this.thread = null;
            this.currentFrame = 0;
            this.totalFrames = 0;
            this.fps = 24;
            this.duration = 1;
            this.delay = 1;

            this.initEvents( 'tween', 'end' );
        },
        /**
         * set the duration of the animation
         * @method setDuration
         */
        setDuration: function( d ) {
            this.duration = d;
        },
        /**
         * start the animation
         * @method start
         */
        start: function() {
            if( this.thread === null ) {
                this.totalFrames = Math.ceil( this.fps * this.duration );
                this.currentFrame = 0;
                this.skip = false;
                this.droppedFrames = 0;
                var o = this;
                this.thread = setInterval( function() { o.run(); }, o.delay );
                this.start = new Date().getTime();
            }
        },
        /**
         * stop the animation
         * @method stop
         */
        stop: function() {
            if( this.thread !== null ) {
                clearInterval( this.thread );
                this.thread = null;
                var o = this.getEventObject();
                this.triggerEvent( 'end', o, this );
            }
        },
        /**
         * skip to the end
         * @method skipToEnd
         */
        skipToEnd: function() {
            this.skip = true;
        },
        /**
         * drop frames to keep up
         * @method catchup
         * @private
         */
        catchUp: function() {
            var time = new Date().getTime() - this.start,
                adjust = 0,
                d = this.duration * 1000,
                t = this.totalFrames,
                f = this.currentFrame;
            var expected = t * ( time / d );
            if( time > d || this.skip ) {
                adjust = t - f;
            } else if( expected > f ) {
                adjust = Math.ceil( expected - f );
            }
            if( adjust > 0 ) {
                adjust = this.currentFrame + adjust < t ? adjust : t - f;
                this.droppedFrames += adjust;
                this.currentFrame += adjust;
            }

        },
        /**
         * @method run
         * @private
         */
        run: function() {
            if( this.currentFrame < this.totalFrames ) {
                this.catchUp();
                this.doFrame();
                this.currentFrame++;
            } else {
                this.doFrame();
                this.stop();
            }
        },
        /**
         * @method doFrame
         * @private
         */
        doFrame: function() {
            this.triggerEvent( 'tween', {currentFrame: this.currentFrame, totalFrames: this.totalFrames}, this );
        },
        /**
         * @method getEventObject
         * @private
         */
        getEventObject: function() {
            return {
                dropped: this.droppedFrames,
                endedEarly: this.skip
            };
        }
    };
    LMI.Lang.importFunctions( LMI.Animation, LMI.Event );
})();

/**
 * @class LMI.Animation.Motion
 */
(function() {
    LMI.Animation.Motion = function( element, start, end ) {
        this.init( element, start, end );
    };
    LMI.Lang.extend( LMI.Animation.Motion, LMI.Animation );

    var L = LMI.Animation.Motion, proto = L.prototype, superclass = L.superclass;
    proto.init = function( element, start, end ) {
        superclass.init.call( this );
        this.element = element;
        this.startPos = start;
        this.endPos = end;
        this.easingMethod = LMI.Animation.Easing.easeOutStrong;
    };
    proto.setEasingMethod = function( method ) {
        this.easingMethod = method;
    };
    proto.doFrame = function() {
        var t = this.easingMethod( this.currentFrame, 0, 100, this.totalFrames ) / 100;
        var b = LMI.Animation.Bezier.getPosition( [this.startPos, this.endPos], t );
        this.setProperties( b );
        superclass.doFrame.call( this );
    };
    proto.setProperties = function( p ) {
        this.element.style.left = Math.floor( p.x ) + 'px';
        this.element.style.top = Math.floor( p.y ) + 'px';
    };
})();

/**
 * @class LMI.Animation.Size
 */
(function() {
    LMI.Animation.Size = function( element, start, end ) {
        this.init( element, start, end );
    };
    LMI.Lang.extend( LMI.Animation.Size, LMI.Animation.Motion );

    var L = LMI.Animation.Size, proto = L.prototype;
    proto.setProperties = function( p ) {
	if( p.x >= 0 ) {
	    this.element.style.width = Math.floor( p.x ) + 'px';
	}
	if( p.y >= 0 ) {
            this.element.style.height = Math.floor( p.y ) + 'px';
	}
    };
})();

/**
 * @class LMI.Animation.Fade
 * @param start = starting opacity ( 0 .. 100 )
 * @param end = ending opacity ( 0 .. 100 )
 */
(function() {
    LMI.Animation.Fade = function( element, start, end ) {
        this.init( element, {x: start, y: 0}, {x: end, y: 0} );
    };
    LMI.Lang.extend( LMI.Animation.Fade, LMI.Animation.Motion );

    var L = LMI.Animation.Fade, proto = L.prototype, superclass = L.superclass;
    proto.setProperties = function( p ) {
        LMI.StyleSheet.setOpacity( this.element, p.x );
    };
})();


/**
 * calculate Bezier splines
 * @class LMI.Animation.Bezier
 */
LMI.Animation.Bezier = (function() {
    return {
        /**
         * @method getPosition
         * @param coords array of coords
         * @param t time index
         */
        getPosition: function( coords, t ) {
            var i, j, n = coords.length, r = [];

            // don't modify the original array
            for( i = 0; i < n; ++i ) {
                r.push( {x: coords[i].x, y: coords[i].y } );
            }

            for (j = 1; j < n; ++j) {
                for (i = 0; i < n - j; ++i) {
                    r[i].x = (1 - t) * r[i].x + t * r[i + 1].x;
                    r[i].y = (1 - t) * r[i].y + t * r[i + 1].y;
                }
            }
            return r[0];
        }
    };
})();


/*
TERMS OF USE - EASING EQUATIONS

Open source under the BSD License.

Copyright © 2001 Robert Penner
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
 * @class LMI.Animation.Easing
 */
LMI.Animation.Easing = (function() {
    return {
        /**
         * @method easeOut
         */
        easeOut: function( t, b, c, d ) {
            return -c *(t/=d)*(t-2) + b;
        },
        /**
         * @method easeOutStrong
         * @param t time
         * @param b starting value
         * @param c delta between start and end values
         * @param d total duration of animation
         * @return The computed value for the current animation frame
         */
        easeOutStrong: function( t, b, c, d ) {
            return -c * ((t=t/d-1)*t*t*t - 1) + b;
        },
        /**
         * @method easeBothStrong
         */
        easeBothStrong: function( t, b, c, d ) {
            if( (t/=d/2) < 1 ) { return c/2*t*t*t*t + b; }
            return -c/2 * ((t-=2)*t*t*t - 2) + b;
        },
        /**
         * @method bounceOut
         */
        bounceOut: function( t, b, c, d ) {
            if( (t/=d) < (1/2.75) ) {
                return c*(7.5625*t*t) + b;
            } else if (t < (2/2.75)) {
                return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
            } else if (t < (2.5/2.75)) {
                return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
            } else {
                return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
            }
        },
        /**
         * @method elasticOut
         * @param t time
         * @param b starting value
         * @param c delta between start and end values
         * @param d total duration of animation
         * @param a elasticity
         * @param p period (optional)
         * @return The computed value for the current animation frame
         */
        elasticOut: function (t, b, c, d, a, p) {
            if (t===0) { return b; }
            if ((t/=d)==1) { return b+c; }
            if (!p) { p=d*0.5; }
            if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
            else { s = p/(2*Math.PI) * Math.asin (c/a); }
            return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
        }
    };
})();
