// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//     You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//     See the License for the specific language governing permissions and
// limitations under the License.


(function(shared, scope, testing) {

  scope.AnimationTimeline = function() {
    this._animations = [];
    this.currentTime = undefined;
  };

  scope.AnimationTimeline.prototype = {
    // FIXME: This needs to return the wrapped players in Web Animations Next
    // TODO: Does this need to be sorted?
    // TODO: Do we need to consider needsRetick?
    getAnimationPlayers: function() {
      this._discardPlayers();
      return this._animations.slice();
    },
    _discardPlayers: function() {
      this._animations = this._animations.filter(function(player) {
        return player.playState != 'finished' && player.playState != 'idle';
      });
    },
    play: function(source) {
      var player = new scope.Player(source);
      this._animations.push(player);
      scope.restartWebAnimationsNextTick();
      // Use player._player.play() here, NOT player.play().
      //
      // Timeline.play calls new scope.Player(source) which (indirectly) calls Timeline.play on
      // source's children, and Player.play is also recursive. We only need to call play on each
      // player in the tree once.
      player._player.play();
      return player;
    },
  };

  var ticking = false;

  scope.restartWebAnimationsNextTick = function() {
    if (!ticking) {
      ticking = true;
      requestAnimationFrame(webAnimationsNextTick);
    }
  };

  function webAnimationsNextTick(t) {
    var timeline = window.document.timeline;
    timeline.currentTime = t;
    timeline._discardPlayers();
    if (timeline._animations.length == 0)
      ticking = false;
    else
      requestAnimationFrame(webAnimationsNextTick);
  }

  var timeline = new scope.AnimationTimeline();
  scope.timeline = timeline;

  try {
    Object.defineProperty(window.document, 'timeline', {
      configurable: true,
      get: function() { return timeline; }
    });
  } catch (e) { }
  try {
    window.document.timeline = timeline;
  } catch (e) { }

})(webAnimationsShared, webAnimationsNext, webAnimationsTesting);

@@ -840,22 +840,25 @@
 wrapped 
-player
+animation
 s in Web
@@ -981,22 +981,25 @@
 nimation
-Player
+Animation
 s: funct
@@ -1017,38 +1017,41 @@
    this._discard
-Player
+Animation
 s();%0A      retur
@@ -1097,22 +1097,25 @@
 _discard
-Player
+Animation
 s: funct
@@ -1180,22 +1180,25 @@
 unction(
-player
+animation
 ) %7B%0A    
@@ -1208,22 +1208,25 @@
  return 
-player
+animation
 .playSta
@@ -1245,22 +1245,25 @@
 hed' && 
-player
+animation
 .playSta
@@ -1332,22 +1332,25 @@
     var 
-player
+animation
  = new s
@@ -1346,38 +1346,41 @@
 ion = new scope.
-Player
+Animation
 (source);%0A      
@@ -1401,22 +1401,25 @@
 ns.push(
-player
+animation
 );%0A     
@@ -1470,30 +1470,36 @@
  // Use 
-player._player
+animation._animation
 .play() 
@@ -1508,22 +1508,25 @@
 re, NOT 
-player
+animation
 .play().
@@ -1574,22 +1574,25 @@
 w scope.
-Player
+Animation
 (source)
@@ -1666,22 +1666,25 @@
 en, and 
-Player
+Animation
 .play is
@@ -1743,22 +1743,25 @@
      // 
-player
+animation
  in the 
@@ -1781,22 +1781,28 @@
     
-player._player
+animation._animation
 .pla
@@ -1823,14 +1823,17 @@
 urn 
-player
+animation
 ;%0A  
@@ -2163,14 +2163,17 @@
 card
-Player
+Animation
 s();

Rename for timeline.js