

include( "styles.js" );

init = function() {

    // Calculates simple linear  animation representing uniform change
    // of the value from the start  value to the target one within the
    // given number  of frames.
    //
    // Arguments:
    //  - startValue is a value from where an animation starts
    //  - targetValue is a value where an animation should finish
    //  - frameNum is a proposed number of frames
    //  - styleArgs object keeps additional style related arguments:
    //     - preserveTangent (true by default) will make a function to
    //       ignore the framesNum and  calculate them according to the
    //       previous animaiton tangent
    //     - preserveSlower (false by default) will make a function to
    //       preserve  tangent even in case when  calculated number of
    //       frames is more than proposed
    //  - prop is an object which tracks the animation
    // 
    anime.styles.line = function( startValue,
                                  targetValue,
                                  framesNum,
                                  styleArgs,
                                  prop ) {
        // FIRST, calculate actual number of frames
        var preserveTangent = true;
        var preserveSlower = false;
        if ( typeof ( styleArgs ) != "undefined" &&
             styleArgs != null ) {
            if ( typeof ( styleArgs.preserveTangent ) != "undefined" ) {
                preserveTangent = styleArgs.preserveTangent;
            }

            if ( typeof ( styleArgs.preserveSlower ) != "undefined" ) {
                preserveSlower = styleArgs.preserveSlower;
            }
        }

        var actualFramesNum = framesNum;

        if ( preserveTangent &&
             ( prop._state == prop._states.animating &&
               // last frame is treated as animation is finished
               prop._frameNum < prop._frames.length - 1 ) ) {
            var oldMidSpeed =
                ( prop._frames[ prop._frameNum ] -
                  prop._frames[ 0 ] ) / prop._frameNum;
            var newFramesNum =
                Math.abs(
                    Math.round(
                        ( targetValue - startValue ) / oldMidSpeed
                    )
                );
            if ( ( newFramesNum < actualFramesNum ) || preserveSlower ) {
                actualFramesNum = newFramesNum;
            }
        }

        // now, actualFramesNum keeps the number of frames we should use

        // SECOND, caclulate the animation frames
        var result = new Array( actualFramesNum + 1 );

        // value difference between two frames
        var diff =
            ( targetValue - startValue ) / actualFramesNum;

        result[ 0 ] = startValue;
        result[ actualFramesNum ] = targetValue;

        for ( var i = 1; i < actualFramesNum; i++ ) {
            result[ i ] = result[ i - 1 ] + diff;
        }

        return result;
        
    }


}
