src/js/libs/popcorn.js
author hamidouk
Tue, 29 Nov 2011 11:09:08 +0100
branchpopcorn-port
changeset 345 8a088f7daa66
parent 110 048125f1a167
child 429 e84a7100cc3f
permissions -rw-r--r--
rollover over the interface buttons now works as expected. Also changed the width of the buttons to the correct size. Resized the width and height of the sprites to be the same as the boxes we display them in.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     1
(function(global, document) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     2
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     3
  // Popcorn.js does not support archaic browsers
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     4
  if ( !document.addEventListener ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     5
    global.Popcorn = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     6
      isSupported: false
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     7
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     8
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     9
    var methods = ( "forEach extend effects error guid sizeOf isArray nop position disable enable destroy " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    10
          "addTrackEvent removeTrackEvent getTrackEvents getTrackEvent getLastTrackEventId " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    11
          "timeUpdate plugin removePlugin compose effect parser xhr getJSONP getScript" ).split(/\s+/);
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    12
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    13
    while( methods.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    14
      global.Popcorn[ methods.shift() ] = function() {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    15
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    16
    return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    17
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    18
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    19
  var
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    20
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    21
  AP = Array.prototype,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    22
  OP = Object.prototype,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    23
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    24
  forEach = AP.forEach,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    25
  slice = AP.slice,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    26
  hasOwn = OP.hasOwnProperty,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    27
  toString = OP.toString,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    28
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    29
  // Copy global Popcorn (may not exist)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    30
  _Popcorn = global.Popcorn,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    31
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    32
  //  ID string matching
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    33
  rIdExp  = /^(#([\w\-\_\.]+))$/,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    34
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    35
  //  Ready fn cache
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    36
  readyStack = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    37
  readyBound = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    38
  readyFired = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    39
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    40
  //  Non-public internal data object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    41
  internal = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    42
    events: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    43
      hash: {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    44
      apis: {}
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    45
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    46
  },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    47
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    48
  //  Non-public `requestAnimFrame`
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    49
  //  http://paulirish.com/2011/requestanimationframe-for-smart-animating/
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    50
  requestAnimFrame = (function(){
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    51
    return global.requestAnimationFrame ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    52
      global.webkitRequestAnimationFrame ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    53
      global.mozRequestAnimationFrame ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    54
      global.oRequestAnimationFrame ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    55
      global.msRequestAnimationFrame ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    56
      function( callback, element ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    57
        global.setTimeout( callback, 16 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    58
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    59
  }()),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    60
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    61
  //  Declare constructor
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    62
  //  Returns an instance object.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    63
  Popcorn = function( entity, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    64
    //  Return new Popcorn object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    65
    return new Popcorn.p.init( entity, options || null );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    66
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    67
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    68
  //  Popcorn API version, automatically inserted via build system.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    69
  Popcorn.version = "@VERSION";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    70
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    71
  //  Boolean flag allowing a client to determine if Popcorn can be supported
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    72
  Popcorn.isSupported = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    73
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    74
  //  Instance caching
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    75
  Popcorn.instances = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    76
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    77
  //  Declare a shortcut (Popcorn.p) to and a definition of
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    78
  //  the new prototype for our Popcorn constructor
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    79
  Popcorn.p = Popcorn.prototype = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    80
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    81
    init: function( entity, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    82
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    83
      var matches;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    84
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    85
      //  Supports Popcorn(function () { /../ })
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    86
      //  Originally proposed by Daniel Brooks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    87
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    88
      if ( typeof entity === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    89
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    90
        //  If document ready has already fired
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    91
        if ( document.readyState === "interactive" || document.readyState === "complete" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    92
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    93
          entity( document, Popcorn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    94
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    95
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    96
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    97
        //  Add `entity` fn to ready stack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    98
        readyStack.push( entity );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    99
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   100
        //  This process should happen once per page load
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   101
        if ( !readyBound ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   102
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   103
          //  set readyBound flag
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   104
          readyBound = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   105
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   106
          var DOMContentLoaded  = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   107
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   108
            readyFired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   109
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   110
            //  Remove global DOM ready listener
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   111
            document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   112
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   113
            //  Execute all ready function in the stack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   114
            for ( var i = 0, readyStackLength = readyStack.length; i < readyStackLength; i++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   115
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   116
              readyStack[ i ].call( document, Popcorn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   117
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   118
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   119
            //  GC readyStack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   120
            readyStack = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   121
          };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   122
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   123
          //  Register global DOM ready listener
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   124
          document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   125
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   126
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   127
        return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   128
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   129
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   130
      //  Check if entity is a valid string id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   131
      matches = rIdExp.exec( entity );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   132
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   133
      //  Get media element by id or object reference
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   134
      this.media = matches && matches.length && matches[ 2 ] ?
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   135
                     document.getElementById( matches[ 2 ] ) :
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   136
                     entity;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   137
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   138
      //  Create an audio or video element property reference
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   139
      this[ ( this.media.nodeName && this.media.nodeName.toLowerCase() ) || "video" ] = this.media;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   140
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   141
      //  Register new instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   142
      Popcorn.instances.push( this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   143
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   144
      this.options = options || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   145
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   146
      this.isDestroyed = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   147
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   148
      this.data = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   149
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   150
        // Allows disabling a plugin per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   151
        disabled: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   152
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   153
        // Stores DOM event queues by type
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   154
        events: {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   155
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   156
        // Stores Special event hooks data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   157
        hooks: {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   158
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   159
        // Store track event history data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   160
        history: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   161
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   162
        // Stores ad-hoc state related data]
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   163
        state: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   164
          volume: this.media.volume
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   165
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   166
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   167
        // Store track event object references by trackId
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   168
        trackRefs: {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   169
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   170
        // Playback track event queues
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   171
        trackEvents: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   172
          byStart: [{
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   173
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   174
            start: -1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   175
            end: -1
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   176
          }],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   177
          byEnd: [{
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   178
            start: -1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   179
            end: -1
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   180
          }],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   181
          animating: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   182
          startIndex: 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   183
          endIndex: 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   184
          previousUpdateTime: -1
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   185
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   186
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   187
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   188
      //  Wrap true ready check
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   189
      var isReady = function( that ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   190
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   191
        var duration, videoDurationPlus, animate;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   192
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   193
        if ( that.media.readyState >= 2 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   194
          //  Adding padding to the front and end of the arrays
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   195
          //  this is so we do not fall off either end
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   196
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   197
          duration = that.media.duration;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   198
          //  Check for no duration info (NaN)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   199
          videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   200
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   201
          Popcorn.addTrackEvent( that, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   202
            start: videoDurationPlus,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   203
            end: videoDurationPlus
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   204
          });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   205
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   206
          if ( that.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   207
            //  if Popcorn is created with frameAnimation option set to true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   208
            //  requestAnimFrame is used instead of "timeupdate" media event.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   209
            //  This is for greater frame time accuracy, theoretically up to
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   210
            //  60 frames per second as opposed to ~4 ( ~every 15-250ms)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   211
            animate = function () {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   212
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   213
              Popcorn.timeUpdate( that, {} );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   214
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   215
              that.trigger( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   216
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   217
              requestAnimFrame( animate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   218
            };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   219
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   220
            requestAnimFrame( animate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   221
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   222
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   223
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   224
            that.data.timeUpdateFunction = function( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   225
              Popcorn.timeUpdate( that, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   226
            };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   227
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   228
            if ( !that.isDestroyed ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   229
              that.media.addEventListener( "timeupdate", that.data.timeUpdateFunction, false );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   230
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   231
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   232
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   233
          global.setTimeout(function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   234
            isReady( that );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   235
          }, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   236
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   237
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   238
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   239
      isReady( this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   240
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   241
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   242
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   243
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   244
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   245
  //  Extend constructor prototype to instance prototype
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   246
  //  Allows chaining methods to instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   247
  Popcorn.p.init.prototype = Popcorn.p;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   248
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   249
  Popcorn.forEach = function( obj, fn, context ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   250
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   251
    if ( !obj || !fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   252
      return {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   253
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   254
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   255
    context = context || this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   256
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   257
    var key, len;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   258
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   259
    // Use native whenever possible
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   260
    if ( forEach && obj.forEach === forEach ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   261
      return obj.forEach( fn, context );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   262
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   263
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   264
    if ( toString.call( obj ) === "[object NodeList]" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   265
      for ( key = 0, len = obj.length; key < len; key++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   266
        fn.call( context, obj[ key ], key, obj );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   267
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   268
      return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   269
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   270
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   271
    for ( key in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   272
      if ( hasOwn.call( obj, key ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   273
        fn.call( context, obj[ key ], key, obj );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   274
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   275
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   276
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   277
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   278
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   279
  Popcorn.extend = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   280
    var dest = obj, src = slice.call( arguments, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   281
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   282
    Popcorn.forEach( src, function( copy ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   283
      for ( var prop in copy ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   284
        dest[ prop ] = copy[ prop ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   285
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   286
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   287
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   288
    return dest;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   289
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   290
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   291
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   292
  // A Few reusable utils, memoized onto Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   293
  Popcorn.extend( Popcorn, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   294
    noConflict: function( deep ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   295
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   296
      if ( deep ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   297
        global.Popcorn = _Popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   298
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   299
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   300
      return Popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   301
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   302
    error: function( msg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   303
      throw new Error( msg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   304
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   305
    guid: function( prefix ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   306
      Popcorn.guid.counter++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   307
      return  ( prefix ? prefix : "" ) + ( +new Date() + Popcorn.guid.counter );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   308
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   309
    sizeOf: function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   310
      var size = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   311
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   312
      for ( var prop in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   313
        size++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   314
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   315
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   316
      return size;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   317
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   318
    isArray: Array.isArray || function( array ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   319
      return toString.call( array ) === "[object Array]";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   320
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   321
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   322
    nop: function() {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   323
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   324
    position: function( elem ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   325
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   326
      var clientRect = elem.getBoundingClientRect(),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   327
          bounds = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   328
          doc = elem.ownerDocument,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   329
          docElem = document.documentElement,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   330
          body = document.body,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   331
          clientTop, clientLeft, scrollTop, scrollLeft, top, left;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   332
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   333
      //  Determine correct clientTop/Left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   334
      clientTop = docElem.clientTop || body.clientTop || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   335
      clientLeft = docElem.clientLeft || body.clientLeft || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   336
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   337
      //  Determine correct scrollTop/Left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   338
      scrollTop = ( global.pageYOffset && docElem.scrollTop || body.scrollTop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   339
      scrollLeft = ( global.pageXOffset && docElem.scrollLeft || body.scrollLeft );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   340
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   341
      //  Temp top/left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   342
      top = Math.ceil( clientRect.top + scrollTop - clientTop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   343
      left = Math.ceil( clientRect.left + scrollLeft - clientLeft );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   344
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   345
      for ( var p in clientRect ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   346
        bounds[ p ] = Math.round( clientRect[ p ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   347
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   348
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   349
      return Popcorn.extend({}, bounds, { top: top, left: left });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   350
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   351
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   352
    disable: function( instance, plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   353
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   354
      var disabled = instance.data.disabled;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   355
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   356
      if ( disabled.indexOf( plugin ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   357
        disabled.push( plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   358
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   359
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   360
      return instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   361
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   362
    enable: function( instance, plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   363
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   364
      var disabled = instance.data.disabled,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   365
          index = disabled.indexOf( plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   366
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   367
      if ( index > -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   368
        disabled.splice( index, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   369
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   370
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   371
      return instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   372
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   373
    destroy: function( instance ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   374
      var events = instance.data.events,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   375
          singleEvent, item, fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   376
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   377
      //  Iterate through all events and remove them
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   378
      for ( item in events ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   379
        singleEvent = events[ item ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   380
        for ( fn in singleEvent ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   381
          delete singleEvent[ fn ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   382
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   383
        events[ item ] = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   384
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   385
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   386
      if ( !instance.isDestroyed ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   387
        instance.media.removeEventListener( "timeupdate", instance.data.timeUpdateFunction, false );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   388
        instance.isDestroyed = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   389
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   390
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   391
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   392
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   393
  //  Memoized GUID Counter
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   394
  Popcorn.guid.counter = 1;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   395
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   396
  //  Factory to implement getters, setters and controllers
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   397
  //  as Popcorn instance methods. The IIFE will create and return
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   398
  //  an object with defined methods
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   399
  Popcorn.extend(Popcorn.p, (function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   400
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   401
      var methods = "load play pause currentTime playbackRate volume duration preload playbackRate " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   402
                    "autoplay loop controls muted buffered readyState seeking paused played seekable ended",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   403
          ret = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   404
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   405
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   406
      //  Build methods, store in object that is returned and passed to extend
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   407
      Popcorn.forEach( methods.split( /\s+/g ), function( name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   408
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   409
        ret[ name ] = function( arg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   410
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   411
          if ( typeof this.media[ name ] === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   412
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   413
            // Support for shorthanded play(n)/pause(n) jump to currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   414
            // If arg is not null or undefined and called by one of the
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   415
            // allowed shorthandable methods, then set the currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   416
            // Supports time as seconds or SMPTE
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   417
            if ( arg != null && /play|pause/.test( name ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   418
              this.media.currentTime = Popcorn.util.toSeconds( arg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   419
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   420
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   421
            this.media[ name ]();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   422
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   423
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   424
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   425
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   426
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   427
          if ( arg != null ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   428
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   429
            this.media[ name ] = arg;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   430
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   431
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   432
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   433
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   434
          return this.media[ name ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   435
        };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   436
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   437
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   438
      return ret;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   439
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   440
    })()
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   441
  );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   442
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   443
  Popcorn.forEach( "enable disable".split(" "), function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   444
    Popcorn.p[ method ] = function( plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   445
      return Popcorn[ method ]( this, plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   446
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   447
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   448
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   449
  Popcorn.extend(Popcorn.p, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   450
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   451
    //  Rounded currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   452
    roundTime: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   453
      return -~this.media.currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   454
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   455
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   456
    //  Attach an event to a single point in time
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   457
    exec: function( time, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   458
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   459
      //  Creating a one second track event with an empty end
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   460
      Popcorn.addTrackEvent( this, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   461
        start: time,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   462
        end: time + 1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   463
        _running: false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   464
        _natives: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   465
          start: fn || Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   466
          end: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   467
          type: "exec"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   468
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   469
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   470
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   471
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   472
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   473
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   474
    // Mute the calling media, optionally toggle
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   475
    mute: function( toggle ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   476
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   477
      var event = toggle == null || toggle === true ? "muted" : "unmuted";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   478
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   479
      // If `toggle` is explicitly `false`,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   480
      // unmute the media and restore the volume level
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   481
      if ( event === "unmuted" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   482
        this.media.muted = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   483
        this.media.volume = this.data.state.volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   484
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   485
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   486
      // If `toggle` is either null or undefined,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   487
      // save the current volume and mute the media element
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   488
      if ( event === "muted" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   489
        this.data.state.volume = this.media.volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   490
        this.media.muted = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   491
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   492
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   493
      // Trigger either muted|unmuted event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   494
      this.trigger( event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   495
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   496
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   497
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   498
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   499
    // Convenience method, unmute the calling media
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   500
    unmute: function( toggle ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   501
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   502
      return this.mute( toggle == null ? false : !toggle );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   503
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   504
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   505
    // Get the client bounding box of an instance element
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   506
    position: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   507
      return Popcorn.position( this.media );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   508
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   509
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   510
    // Toggle a plugin's playback behaviour (on or off) per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   511
    toggle: function( plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   512
      return Popcorn[ this.data.disabled.indexOf( plugin ) > -1 ? "enable" : "disable" ]( this, plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   513
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   514
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   515
    // Set default values for plugin options objects per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   516
    defaults: function( plugin, defaults ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   517
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   518
      // If an array of default configurations is provided,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   519
      // iterate and apply each to this instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   520
      if ( Popcorn.isArray( plugin ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   521
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   522
        Popcorn.forEach( plugin, function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   523
          for ( var name in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   524
            this.defaults( name, obj[ name ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   525
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   526
        }, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   527
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   528
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   529
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   530
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   531
      if ( !this.options.defaults ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   532
        this.options.defaults = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   533
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   534
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   535
      if ( !this.options.defaults[ plugin ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   536
        this.options.defaults[ plugin ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   537
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   538
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   539
      Popcorn.extend( this.options.defaults[ plugin ], defaults );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   540
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   541
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   542
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   543
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   544
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   545
  Popcorn.Events  = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   546
    UIEvents: "blur focus focusin focusout load resize scroll unload",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   547
    MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   548
    Events: "loadstart progress suspend emptied stalled play pause " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   549
            "loadedmetadata loadeddata waiting playing canplay canplaythrough " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   550
            "seeking seeked timeupdate ended ratechange durationchange volumechange"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   551
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   552
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   553
  Popcorn.Events.Natives = Popcorn.Events.UIEvents + " " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   554
                           Popcorn.Events.MouseEvents + " " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   555
                           Popcorn.Events.Events;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   556
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   557
  internal.events.apiTypes = [ "UIEvents", "MouseEvents", "Events" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   558
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   559
  // Privately compile events table at load time
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   560
  (function( events, data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   561
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   562
    var apis = internal.events.apiTypes,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   563
    eventsList = events.Natives.split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   564
    idx = 0, len = eventsList.length, prop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   565
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   566
    for( ; idx < len; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   567
      data.hash[ eventsList[idx] ] = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   568
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   569
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   570
    apis.forEach(function( val, idx ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   571
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   572
      data.apis[ val ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   573
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   574
      var apiEvents = events[ val ].split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   575
      len = apiEvents.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   576
      k = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   577
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   578
      for ( ; k < len; k++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   579
        data.apis[ val ][ apiEvents[ k ] ] = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   580
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   581
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   582
  })( Popcorn.Events, internal.events );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   583
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   584
  Popcorn.events = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   585
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   586
    isNative: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   587
      return !!internal.events.hash[ type ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   588
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   589
    getInterface: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   590
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   591
      if ( !Popcorn.events.isNative( type ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   592
        return false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   593
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   594
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   595
      var eventApi = internal.events,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   596
        apis = eventApi.apiTypes,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   597
        apihash = eventApi.apis,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   598
        idx = 0, len = apis.length, api, tmp;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   599
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   600
      for ( ; idx < len; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   601
        tmp = apis[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   602
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   603
        if ( apihash[ tmp ][ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   604
          api = tmp;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   605
          break;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   606
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   607
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   608
      return api;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   609
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   610
    //  Compile all native events to single array
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   611
    all: Popcorn.Events.Natives.split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   612
    //  Defines all Event handling static functions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   613
    fn: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   614
      trigger: function( type, data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   615
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   616
        var eventInterface, evt;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   617
        //  setup checks for custom event system
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   618
        if ( this.data.events[ type ] && Popcorn.sizeOf( this.data.events[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   619
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   620
          eventInterface  = Popcorn.events.getInterface( type );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   621
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   622
          if ( eventInterface ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   623
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   624
            evt = document.createEvent( eventInterface );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   625
            evt.initEvent( type, true, true, global, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   626
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   627
            this.media.dispatchEvent( evt );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   628
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   629
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   630
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   631
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   632
          //  Custom events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   633
          Popcorn.forEach( this.data.events[ type ], function( obj, key ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   634
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   635
            obj.call( this, data );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   636
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   637
          }, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   638
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   639
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   640
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   641
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   642
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   643
      listen: function( type, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   644
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   645
        var self = this,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   646
            hasEvents = true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   647
            eventHook = Popcorn.events.hooks[ type ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   648
            origType = type,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   649
            tmp;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   650
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   651
        if ( !this.data.events[ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   652
          this.data.events[ type ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   653
          hasEvents = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   654
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   655
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   656
        // Check and setup event hooks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   657
        if ( eventHook ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   658
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   659
          // Execute hook add method if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   660
          if ( eventHook.add ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   661
            eventHook.add.call( this, {}, fn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   662
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   663
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   664
          // Reassign event type to our piggyback event type if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   665
          if ( eventHook.bind ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   666
            type = eventHook.bind;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   667
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   668
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   669
          // Reassign handler if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   670
          if ( eventHook.handler ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   671
            tmp = fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   672
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   673
            fn = function wrapper( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   674
              eventHook.handler.call( self, event, tmp );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   675
            };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   676
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   677
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   678
          // assume the piggy back event is registered
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   679
          hasEvents = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   680
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   681
          // Setup event registry entry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   682
          if ( !this.data.events[ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   683
            this.data.events[ type ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   684
            // Toggle if the previous assumption was untrue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   685
            hasEvents = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   686
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   687
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   688
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   689
        //  Register event and handler
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   690
        this.data.events[ type ][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   691
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   692
        // only attach one event of any type
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   693
        if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   694
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   695
          this.media.addEventListener( type, function( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   696
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   697
            Popcorn.forEach( self.data.events[ type ], function( obj, key ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   698
              if ( typeof obj === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   699
                obj.call( self, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   700
              }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   701
            });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   702
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   703
          }, false);
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   704
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   705
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   706
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   707
      unlisten: function( type, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   708
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   709
        if ( this.data.events[ type ] && this.data.events[ type ][ fn ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   710
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   711
          delete this.data.events[ type ][ fn ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   712
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   713
          return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   714
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   715
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   716
        this.data.events[ type ] = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   717
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   718
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   719
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   720
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   721
    hooks: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   722
      canplayall: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   723
        bind: "canplaythrough",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   724
        add: function( event, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   725
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   726
          var state = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   727
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   728
          if ( this.media.readyState ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   729
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   730
            callback.call( this, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   731
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   732
            state = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   733
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   734
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   735
          this.data.hooks.canplayall = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   736
            fired: state
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   737
          };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   738
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   739
        // declare special handling instructions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   740
        handler: function canplayall( event, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   741
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   742
          if ( !this.data.hooks.canplayall.fired ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   743
            // trigger original user callback once
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   744
            callback.call( this, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   745
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   746
            this.data.hooks.canplayall.fired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   747
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   748
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   749
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   750
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   751
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   752
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   753
  //  Extend Popcorn.events.fns (listen, unlisten, trigger) to all Popcorn instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   754
  Popcorn.forEach( [ "trigger", "listen", "unlisten" ], function( key ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   755
    Popcorn.p[ key ] = Popcorn.events.fn[ key ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   756
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   757
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   758
  // Internal Only - Adds track events to the instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   759
  Popcorn.addTrackEvent = function( obj, track ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   760
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   761
    // Determine if this track has default options set for it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   762
    // If so, apply them to the track object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   763
    if ( track && track._natives && track._natives.type &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   764
        ( obj.options.defaults && obj.options.defaults[ track._natives.type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   765
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   766
      track = Popcorn.extend( {}, obj.options.defaults[ track._natives.type ], track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   767
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   768
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   769
    if ( track._natives ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   770
      //  Supports user defined track event id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   771
      track._id = !track.id ? Popcorn.guid( track._natives.type ) : track.id;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   772
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   773
      //  Push track event ids into the history
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   774
      obj.data.history.push( track._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   775
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   776
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   777
    track.start = Popcorn.util.toSeconds( track.start, obj.options.framerate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   778
    track.end   = Popcorn.util.toSeconds( track.end, obj.options.framerate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   779
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   780
    //  Store this definition in an array sorted by times
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   781
    var byStart = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   782
        byEnd = obj.data.trackEvents.byEnd,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   783
        idx;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   784
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   785
    for ( idx = byStart.length - 1; idx >= 0; idx-- ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   786
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   787
      if ( track.start >= byStart[ idx ].start ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   788
        byStart.splice( idx + 1, 0, track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   789
        break;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   790
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   791
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   792
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   793
    for ( idx = byEnd.length - 1; idx >= 0; idx-- ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   794
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   795
      if ( track.end > byEnd[ idx ].end ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   796
        byEnd.splice( idx + 1, 0, track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   797
        break;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   798
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   799
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   800
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   801
    this.timeUpdate( obj, null );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   802
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   803
    // Store references to user added trackevents in ref table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   804
    if ( track._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   805
      Popcorn.addTrackEvent.ref( obj, track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   806
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   807
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   808
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   809
  // Internal Only - Adds track event references to the instance object's trackRefs hash table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   810
  Popcorn.addTrackEvent.ref = function( obj, track ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   811
    obj.data.trackRefs[ track._id ] = track;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   812
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   813
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   814
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   815
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   816
  Popcorn.removeTrackEvent  = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   817
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   818
    var historyLen = obj.data.history.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   819
        indexWasAt = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   820
        byStart = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   821
        byEnd = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   822
        animating = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   823
        history = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   824
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   825
    Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   826
      // Preserve the original start/end trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   827
      if ( !o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   828
        byStart.push( obj.data.trackEvents.byStart[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   829
        byEnd.push( obj.data.trackEvents.byEnd[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   830
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   831
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   832
      // Filter for user track events (vs system track events)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   833
      if ( o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   834
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   835
        // Filter for the trackevent to remove
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   836
        if ( o._id !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   837
          byStart.push( obj.data.trackEvents.byStart[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   838
          byEnd.push( obj.data.trackEvents.byEnd[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   839
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   840
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   841
        //  Capture the position of the track being removed.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   842
        if ( o._id === trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   843
          indexWasAt = i;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   844
          o._natives._teardown && o._natives._teardown.call( obj, o );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   845
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   846
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   847
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   848
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   849
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   850
    if ( obj.data.trackEvents.animating.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   851
      Popcorn.forEach( obj.data.trackEvents.animating, function( o, i, context ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   852
        // Preserve the original start/end trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   853
        if ( !o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   854
          animating.push( obj.data.trackEvents.animating[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   855
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   856
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   857
        // Filter for user track events (vs system track events)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   858
        if ( o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   859
          // Filter for the trackevent to remove
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   860
          if ( o._id !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   861
            animating.push( obj.data.trackEvents.animating[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   862
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   863
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   864
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   865
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   866
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   867
    //  Update
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   868
    if ( indexWasAt <= obj.data.trackEvents.startIndex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   869
      obj.data.trackEvents.startIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   870
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   871
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   872
    if ( indexWasAt <= obj.data.trackEvents.endIndex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   873
      obj.data.trackEvents.endIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   874
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   875
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   876
    obj.data.trackEvents.byStart = byStart;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   877
    obj.data.trackEvents.byEnd = byEnd;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   878
    obj.data.trackEvents.animating = animating;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   879
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   880
    for ( var i = 0; i < historyLen; i++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   881
      if ( obj.data.history[ i ] !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   882
        history.push( obj.data.history[ i ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   883
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   884
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   885
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   886
    // Update ordered history array
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   887
    obj.data.history = history;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   888
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   889
    // Update track event references
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   890
    Popcorn.removeTrackEvent.ref( obj, trackId );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   891
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   892
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   893
  // Internal Only - Removes track event references from instance object's trackRefs hash table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   894
  Popcorn.removeTrackEvent.ref = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   895
    delete obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   896
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   897
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   898
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   899
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   900
  // Return an array of track events bound to this instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   901
  Popcorn.getTrackEvents = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   902
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   903
    var trackevents = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   904
      refs = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   905
      length = refs.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   906
      idx = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   907
      ref;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   908
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   909
    for ( ; idx < length; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   910
      ref = refs[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   911
      // Return only user attributed track event references
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   912
      if ( ref._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   913
        trackevents.push( ref );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   914
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   915
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   916
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   917
    return trackevents;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   918
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   919
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   920
  // Internal Only - Returns an instance object's trackRefs hash table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   921
  Popcorn.getTrackEvents.ref = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   922
    return obj.data.trackRefs;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   923
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   924
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   925
  // Return a single track event bound to this instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   926
  Popcorn.getTrackEvent = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   927
    return obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   928
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   929
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   930
  // Internal Only - Returns an instance object's track reference by track id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   931
  Popcorn.getTrackEvent.ref = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   932
    return obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   933
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   934
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   935
  Popcorn.getLastTrackEventId = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   936
    return obj.data.history[ obj.data.history.length - 1 ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   937
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   938
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   939
  Popcorn.timeUpdate = function( obj, event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   940
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   941
    var currentTime = obj.media.currentTime,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   942
        previousTime = obj.data.trackEvents.previousUpdateTime,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   943
        tracks = obj.data.trackEvents,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   944
        animating = tracks.animating,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   945
        end = tracks.endIndex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   946
        start = tracks.startIndex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   947
        animIndex = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   948
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   949
        registryByName = Popcorn.registryByName,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   950
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   951
        byEnd, byStart, byAnimate, natives, type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   952
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   953
    //  Playbar advancing
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   954
    if ( previousTime <= currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   955
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   956
      while ( tracks.byEnd[ end ] && tracks.byEnd[ end ].end <= currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   957
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   958
        byEnd = tracks.byEnd[ end ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   959
        natives = byEnd._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   960
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   961
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   962
        //  If plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   963
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   964
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   965
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   966
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   967
          if ( byEnd._running === true ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   968
            byEnd._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   969
            natives.end.call( obj, event, byEnd );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   970
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   971
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   972
          end++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   973
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   974
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   975
          Popcorn.removeTrackEvent( obj, byEnd._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   976
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   977
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   978
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   979
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   980
      while ( tracks.byStart[ start ] && tracks.byStart[ start ].start <= currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   981
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   982
        byStart = tracks.byStart[ start ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   983
        natives = byStart._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   984
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   985
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   986
        //  If plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   987
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   988
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   989
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   990
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   991
          if ( byStart.end > currentTime &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   992
                byStart._running === false &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   993
                  obj.data.disabled.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   994
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   995
            byStart._running = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   996
            natives.start.call( obj, event, byStart );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   997
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   998
            // If the `frameAnimation` option is used,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   999
            // push the current byStart object into the `animating` cue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1000
            if ( obj.options.frameAnimation &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1001
                ( byStart && byStart._running && byStart._natives.frame ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1002
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1003
              animating.push( byStart );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1004
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1005
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1006
          start++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1007
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1008
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1009
          Popcorn.removeTrackEvent( obj, byStart._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1010
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1011
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1012
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1013
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1014
      // If the `frameAnimation` option is used, iterate the animating track
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1015
      // and execute the `frame` callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1016
      if ( obj.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1017
        while ( animIndex < animating.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1018
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1019
          byAnimate = animating[ animIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1020
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1021
          if ( !byAnimate._running ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1022
            animating.splice( animIndex, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1023
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1024
            byAnimate._natives.frame.call( obj, event, byAnimate, currentTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1025
            animIndex++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1026
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1027
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1028
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1029
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1030
    // Playbar receding
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1031
    } else if ( previousTime > currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1032
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1033
      while ( tracks.byStart[ start ] && tracks.byStart[ start ].start > currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1034
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1035
        byStart = tracks.byStart[ start ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1036
        natives = byStart._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1037
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1038
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1039
        // if plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1040
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1041
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1042
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1043
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1044
          if ( byStart._running === true ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1045
            byStart._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1046
            natives.end.call( obj, event, byStart );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1047
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1048
          start--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1049
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1050
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1051
          Popcorn.removeTrackEvent( obj, byStart._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1052
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1053
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1054
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1055
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1056
      while ( tracks.byEnd[ end ] && tracks.byEnd[ end ].end > currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1057
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1058
        byEnd = tracks.byEnd[ end ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1059
        natives = byEnd._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1060
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1061
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1062
        // if plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1063
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1064
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1065
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1066
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1067
          if ( byEnd.start <= currentTime &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1068
                byEnd._running === false  &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1069
                  obj.data.disabled.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1070
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1071
            byEnd._running = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1072
            natives.start.call( obj, event, byEnd );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1073
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1074
            // If the `frameAnimation` option is used,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1075
            // push the current byEnd object into the `animating` cue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1076
            if ( obj.options.frameAnimation &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1077
                  ( byEnd && byEnd._running && byEnd._natives.frame ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1078
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1079
              animating.push( byEnd );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1080
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1081
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1082
          end--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1083
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1084
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1085
          Popcorn.removeTrackEvent( obj, byEnd._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1086
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1087
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1088
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1089
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1090
      // If the `frameAnimation` option is used, iterate the animating track
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1091
      // and execute the `frame` callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1092
      if ( obj.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1093
        while ( animIndex < animating.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1094
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1095
          byAnimate = animating[ animIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1096
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1097
          if ( !byAnimate._running ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1098
            animating.splice( animIndex, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1099
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1100
            byAnimate._natives.frame.call( obj, event, byAnimate, currentTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1101
            animIndex++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1102
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1103
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1104
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1105
    // time bar is not moving ( video is paused )
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1106
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1107
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1108
    tracks.endIndex = end;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1109
    tracks.startIndex = start;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1110
    tracks.previousUpdateTime = currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1111
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1112
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1113
  //  Map and Extend TrackEvent functions to all Popcorn instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1114
  Popcorn.extend( Popcorn.p, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1115
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1116
    getTrackEvents: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1117
      return Popcorn.getTrackEvents.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1118
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1119
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1120
    getTrackEvent: function( id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1121
      return Popcorn.getTrackEvent.call( null, this, id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1122
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1123
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1124
    getLastTrackEventId: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1125
      return Popcorn.getLastTrackEventId.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1126
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1127
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1128
    removeTrackEvent: function( id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1129
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1130
      Popcorn.removeTrackEvent.call( null, this, id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1131
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1132
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1133
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1134
    removePlugin: function( name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1135
      Popcorn.removePlugin.call( null, this, name );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1136
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1137
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1138
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1139
    timeUpdate: function( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1140
      Popcorn.timeUpdate.call( null, this, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1141
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1142
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1143
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1144
    destroy: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1145
      Popcorn.destroy.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1146
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1147
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1148
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1149
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1150
  //  Plugin manifests
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1151
  Popcorn.manifest = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1152
  //  Plugins are registered
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1153
  Popcorn.registry = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1154
  Popcorn.registryByName = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1155
  //  An interface for extending Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1156
  //  with plugin functionality
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1157
  Popcorn.plugin = function( name, definition, manifest ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1158
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1159
    if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1160
      Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1161
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1162
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1163
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1164
    //  Provides some sugar, but ultimately extends
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1165
    //  the definition into Popcorn.p
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1166
    var reserved = [ "start", "end" ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1167
        plugin = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1168
        setup,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1169
        isfn = typeof definition === "function",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1170
        methods = [ "_setup", "_teardown", "start", "end", "frame" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1171
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1172
    // combines calls of two function calls into one
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1173
    var combineFn = function( first, second ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1174
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1175
      first = first || Popcorn.nop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1176
      second = second || Popcorn.nop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1177
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1178
      return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1179
        first.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1180
        second.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1181
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1182
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1183
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1184
    //  If `manifest` arg is undefined, check for manifest within the `definition` object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1185
    //  If no `definition.manifest`, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1186
    Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1187
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1188
    // apply safe, and empty default functions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1189
    methods.forEach(function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1190
      definition[ method ] = safeTry( definition[ method ] || Popcorn.nop, name );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1191
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1192
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1193
    var pluginFn = function( setup, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1194
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1195
      if ( !options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1196
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1197
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1198
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1199
      //  Storing the plugin natives
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1200
      var natives = options._natives = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1201
          compose = "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1202
          defaults, originalOpts, manifestOpts, mergedSetupOpts;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1203
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1204
      Popcorn.extend( natives, setup );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1205
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1206
      options._natives.type = name;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1207
      options._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1208
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1209
      natives.start = natives.start || natives[ "in" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1210
      natives.end = natives.end || natives[ "out" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1211
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1212
      // Check for previously set default options
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1213
      defaults = this.options.defaults && this.options.defaults[ options._natives && options._natives.type ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1214
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1215
      // default to an empty string if no effect exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1216
      // split string into an array of effects
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1217
      options.compose = options.compose && options.compose.split( " " ) || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1218
      options.effect = options.effect && options.effect.split( " " ) || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1219
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1220
      // join the two arrays together
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1221
      options.compose = options.compose.concat( options.effect );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1222
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1223
      options.compose.forEach(function( composeOption ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1224
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1225
        // if the requested compose is garbage, throw it away
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1226
        compose = Popcorn.compositions[ composeOption ] || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1227
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1228
        // extends previous functions with compose function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1229
        methods.forEach(function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1230
          natives[ method ] = combineFn( natives[ method ], compose[ method ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1231
        });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1232
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1233
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1234
      //  Ensure a manifest object, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1235
      options._natives.manifest = manifest;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1236
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1237
      //  Checks for expected properties
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1238
      if ( !( "start" in options ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1239
        options.start = options[ "in" ] || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1240
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1241
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1242
      if ( !( "end" in options ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1243
        options.end = options[ "out" ] || this.duration() || Number.MAX_VALUE;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1244
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1245
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1246
      // Merge with defaults if they exist, make sure per call is prioritized
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1247
      mergedSetupOpts = defaults ? Popcorn.extend( {}, defaults, options ) :
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1248
                          options;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1249
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1250
      // Resolves 239, 241, 242
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1251
      if ( !mergedSetupOpts.target ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1252
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1253
        //  Sometimes the manifest may be missing entirely
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1254
        //  or it has an options object that doesn't have a `target` property
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1255
        manifestOpts = "options" in manifest && manifest.options;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1256
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1257
        mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1258
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1259
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1260
      // Trigger _setup method if exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1261
      options._natives._setup && options._natives._setup.call( this, mergedSetupOpts );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1262
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1263
      // Create new track event for this instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1264
      Popcorn.addTrackEvent( this, Popcorn.extend( mergedSetupOpts, options ) );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1265
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1266
      //  Future support for plugin event definitions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1267
      //  for all of the native events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1268
      Popcorn.forEach( setup, function( callback, type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1269
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1270
        if ( type !== "type" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1271
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1272
          if ( reserved.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1273
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1274
            this.listen( type, callback );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1275
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1276
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1277
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1278
      }, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1279
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1280
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1281
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1282
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1283
    //  Assign new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1284
    plugin[ name ] = function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1285
      return pluginFn.call( this, isfn ? definition.call( this, options ) : definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1286
                                  options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1287
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1288
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1289
    //  Extend Popcorn.p with new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1290
    Popcorn.extend( Popcorn.p, plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1291
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1292
    //  Push into the registry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1293
    var entry = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1294
      fn: plugin[ name ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1295
      definition: definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1296
      base: definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1297
      parents: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1298
      name: name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1299
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1300
    Popcorn.registry.push(
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1301
       Popcorn.extend( plugin, entry, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1302
        type: name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1303
      })
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1304
    );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1305
    Popcorn.registryByName[ name ] = entry;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1306
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1307
    return plugin;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1308
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1309
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1310
  // Storage for plugin function errors
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1311
  Popcorn.plugin.errors = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1312
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1313
  // Returns wrapped plugin function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1314
  function safeTry( fn, pluginName ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1315
    return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1316
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1317
      //  When Popcorn.plugin.debug is true, do not suppress errors
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1318
      if ( Popcorn.plugin.debug ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1319
        return fn.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1320
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1321
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1322
      try {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1323
        return fn.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1324
      } catch ( ex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1325
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1326
        // Push plugin function errors into logging queue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1327
        Popcorn.plugin.errors.push({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1328
          plugin: pluginName,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1329
          thrown: ex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1330
          source: fn.toString()
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1331
        });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1332
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1333
        // Trigger an error that the instance can listen for
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1334
        // and react to
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1335
        this.trigger( "error", Popcorn.plugin.errors );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1336
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1337
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1338
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1339
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1340
  // Debug-mode flag for plugin development
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1341
  Popcorn.plugin.debug = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1342
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1343
  //  removePlugin( type ) removes all tracks of that from all instances of popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1344
  //  removePlugin( obj, type ) removes all tracks of type from obj, where obj is a single instance of popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1345
  Popcorn.removePlugin = function( obj, name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1346
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1347
    //  Check if we are removing plugin from an instance or from all of Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1348
    if ( !name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1349
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1350
      //  Fix the order
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1351
      name = obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1352
      obj = Popcorn.p;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1353
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1354
      if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1355
        Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1356
        return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1357
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1358
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1359
      var registryLen = Popcorn.registry.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1360
          registryIdx;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1361
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1362
      // remove plugin reference from registry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1363
      for ( registryIdx = 0; registryIdx < registryLen; registryIdx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1364
        if ( Popcorn.registry[ registryIdx ].name === name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1365
          Popcorn.registry.splice( registryIdx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1366
          delete Popcorn.registryByName[ name ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1367
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1368
          // delete the plugin
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1369
          delete obj[ name ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1370
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1371
          // plugin found and removed, stop checking, we are done
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1372
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1373
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1374
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1375
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1376
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1377
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1378
    var byStart = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1379
        byEnd = obj.data.trackEvents.byEnd,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1380
        animating = obj.data.trackEvents.animating,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1381
        idx, sl;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1382
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1383
    // remove all trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1384
    for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1385
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1386
      if ( ( byStart[ idx ] && byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1387
                ( byEnd[ idx ] && byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1388
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1389
        byStart[ idx ]._natives._teardown && byStart[ idx ]._natives._teardown.call( obj, byStart[ idx ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1390
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1391
        byStart.splice( idx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1392
        byEnd.splice( idx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1393
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1394
        // update for loop if something removed, but keep checking
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1395
        idx--; sl--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1396
        if ( obj.data.trackEvents.startIndex <= idx ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1397
          obj.data.trackEvents.startIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1398
          obj.data.trackEvents.endIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1399
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1400
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1401
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1402
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1403
    //remove all animating events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1404
    for ( idx = 0, sl = animating.length; idx < sl; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1405
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1406
      if ( animating[ idx ] && animating[ idx ]._natives && animating[ idx ]._natives.type === name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1407
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1408
        animating.splice( idx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1409
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1410
        // update for loop if something removed, but keep checking
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1411
        idx--; sl--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1412
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1413
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1414
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1415
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1416
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1417
  Popcorn.compositions = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1418
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1419
  //  Plugin inheritance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1420
  Popcorn.compose = function( name, definition, manifest ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1421
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1422
    //  If `manifest` arg is undefined, check for manifest within the `definition` object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1423
    //  If no `definition.manifest`, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1424
    Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1425
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1426
    // register the effect by name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1427
    Popcorn.compositions[ name ] = definition;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1428
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1429
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1430
  Popcorn.plugin.effect = Popcorn.effect = Popcorn.compose;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1431
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1432
  // stores parsers keyed on filetype
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1433
  Popcorn.parsers = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1434
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1435
  // An interface for extending Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1436
  // with parser functionality
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1437
  Popcorn.parser = function( name, type, definition ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1438
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1439
    if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1440
      Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1441
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1442
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1443
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1444
    // fixes parameters for overloaded function call
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1445
    if ( typeof type === "function" && !definition ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1446
      definition = type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1447
      type = "";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1448
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1449
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1450
    if ( typeof definition !== "function" || typeof type !== "string" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1451
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1452
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1453
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1454
    // Provides some sugar, but ultimately extends
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1455
    // the definition into Popcorn.p
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1456
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1457
    var natives = Popcorn.events.all,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1458
        parseFn,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1459
        parser = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1460
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1461
    parseFn = function( filename, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1462
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1463
      if ( !filename ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1464
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1465
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1466
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1467
      var that = this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1468
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1469
      Popcorn.xhr({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1470
        url: filename,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1471
        dataType: type,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1472
        success: function( data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1473
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1474
          var tracksObject = definition( data ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1475
              tracksData,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1476
              tracksDataLen,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1477
              tracksDef,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1478
              idx = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1479
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1480
          tracksData = tracksObject.data || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1481
          tracksDataLen = tracksData.length;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1482
          tracksDef = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1483
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1484
          //  If no tracks to process, return immediately
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1485
          if ( !tracksDataLen ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1486
            return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1487
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1488
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1489
          //  Create tracks out of parsed object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1490
          for ( ; idx < tracksDataLen; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1491
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1492
            tracksDef = tracksData[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1493
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1494
            for ( var key in tracksDef ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1495
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1496
              if ( hasOwn.call( tracksDef, key ) && !!that[ key ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1497
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1498
                that[ key ]( tracksDef[ key ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1499
              }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1500
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1501
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1502
          if ( callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1503
            callback();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1504
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1505
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1506
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1507
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1508
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1509
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1510
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1511
    // Assign new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1512
    parser[ name ] = parseFn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1513
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1514
    // Extend Popcorn.p with new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1515
    Popcorn.extend( Popcorn.p, parser );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1516
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1517
    // keys the function name by filetype extension
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1518
    //Popcorn.parsers[ name ] = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1519
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1520
    return parser;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1521
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1522
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1523
  Popcorn.player = function( name, player ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1524
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1525
    player = player || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1526
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1527
    var playerFn = function( target, src, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1528
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1529
      options = options || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1530
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1531
      // List of events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1532
      var date = new Date() / 1000,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1533
          baselineTime = date,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1534
          currentTime = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1535
          volume = 1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1536
          muted = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1537
          events = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1538
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1539
          // The container div of the resource
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1540
          container = document.getElementById( rIdExp.exec( target ) && rIdExp.exec( target )[ 2 ] ) ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1541
                        document.getElementById( target ) ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1542
                          target,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1543
          basePlayer = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1544
          timeout,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1545
          popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1546
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1547
      // copies a div into the media object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1548
      for( var val in container ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1549
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1550
        if ( typeof container[ val ] === "object" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1551
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1552
          basePlayer[ val ] = container[ val ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1553
        } else if ( typeof container[ val ] === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1554
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1555
          basePlayer[ val ] = (function( value ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1556
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1557
            return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1558
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1559
              return container[ value ].apply( container, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1560
            };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1561
          }( val ));
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1562
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1563
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1564
          Popcorn.player.defineProperty( basePlayer, val, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1565
            get: (function( value ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1566
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1567
              return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1568
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1569
                return container[ value ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1570
              };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1571
            }( val )),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1572
            set: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1573
            configurable: true
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1574
          });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1575
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1576
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1577
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1578
      var timeupdate = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1579
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1580
        date = new Date() / 1000;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1581
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1582
        if ( !basePlayer.paused ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1583
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1584
          basePlayer.currentTime = basePlayer.currentTime + ( date - baselineTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1585
          basePlayer.dispatchEvent( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1586
          timeout = setTimeout( timeupdate, 10 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1587
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1588
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1589
        baselineTime = date;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1590
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1591
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1592
      basePlayer.play = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1593
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1594
        this.paused = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1595
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1596
        if ( basePlayer.readyState >= 4 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1597
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1598
          baselineTime = new Date() / 1000;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1599
          basePlayer.dispatchEvent( "play" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1600
          timeupdate();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1601
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1602
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1603
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1604
      basePlayer.pause = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1605
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1606
        this.paused = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1607
        basePlayer.dispatchEvent( "pause" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1608
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1609
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1610
      Popcorn.player.defineProperty( basePlayer, "currentTime", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1611
        get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1612
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1613
          return currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1614
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1615
        set: function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1616
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1617
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1618
          currentTime = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1619
          basePlayer.dispatchEvent( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1620
          return currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1621
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1622
        configurable: true
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1623
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1624
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1625
      Popcorn.player.defineProperty( basePlayer, "volume", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1626
        get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1627
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1628
          return volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1629
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1630
        set: function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1631
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1632
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1633
          volume = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1634
          basePlayer.dispatchEvent( "volumechange" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1635
          return volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1636
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1637
        configurable: true
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1638
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1639
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1640
      Popcorn.player.defineProperty( basePlayer, "muted", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1641
        get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1642
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1643
          return muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1644
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1645
        set: function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1646
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1647
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1648
          muted = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1649
          basePlayer.dispatchEvent( "volumechange" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1650
          return muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1651
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1652
        configurable: true
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1653
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1654
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1655
      // Adds an event listener to the object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1656
      basePlayer.addEventListener = function( evtName, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1657
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1658
        if ( !events[ evtName ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1659
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1660
          events[ evtName ] = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1661
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1662
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1663
        events[ evtName ].push( fn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1664
        return fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1665
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1666
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1667
      // Can take event object or simple string
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1668
      basePlayer.dispatchEvent = function( oEvent ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1669
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1670
        var evt,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1671
            self = this,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1672
            eventInterface,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1673
            eventName = oEvent.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1674
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1675
        // A string was passed, create event object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1676
        if ( !eventName ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1677
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1678
          eventName = oEvent;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1679
          eventInterface  = Popcorn.events.getInterface( eventName );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1680
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1681
          if ( eventInterface ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1682
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1683
            evt = document.createEvent( eventInterface );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1684
            evt.initEvent( eventName, true, true, window, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1685
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1686
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1687
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1688
        Popcorn.forEach( events[ eventName ], function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1689
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1690
          val.call( self, evt, self );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1691
        });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1692
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1693
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1694
      // Attempt to get src from playerFn parameter
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1695
      basePlayer.src = src || "";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1696
      basePlayer.readyState = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1697
      basePlayer.duration = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1698
      basePlayer.paused = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1699
      basePlayer.ended = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1700
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1701
      if ( player._setup ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1702
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1703
        player._setup.call( basePlayer, options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1704
      } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1705
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1706
        // there is no setup, which means there is nothing to load
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1707
        basePlayer.readyState = 4;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1708
        basePlayer.dispatchEvent( "load" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1709
        basePlayer.dispatchEvent( "loadeddata" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1710
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1711
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1712
      // when a custom player is loaded, load basePlayer state into custom player
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1713
      basePlayer.addEventListener( "load", function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1714
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1715
        // if a player is not ready before currentTime is called, this will set it after it is ready
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1716
        basePlayer.currentTime = currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1717
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1718
        // same as above with volume and muted
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1719
        basePlayer.volume = volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1720
        basePlayer.muted = muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1721
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1722
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1723
      basePlayer.addEventListener( "loadeddata", function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1724
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1725
        // if play was called before player ready, start playing video
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1726
        !basePlayer.paused && basePlayer.play();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1727
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1728
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1729
      popcorn = new Popcorn.p.init( basePlayer, options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1730
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1731
      return popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1732
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1733
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1734
    Popcorn[ name ] = Popcorn[ name ] || playerFn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1735
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1736
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1737
  Popcorn.player.defineProperty = Object.defineProperty || function( object, description, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1738
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1739
    object.__defineGetter__( description, options.get || Popcorn.nop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1740
    object.__defineSetter__( description, options.set || Popcorn.nop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1741
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1742
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1743
  //  Cache references to reused RegExps
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1744
  var rparams = /\?/,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1745
  //  XHR Setup object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1746
  setup = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1747
    url: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1748
    data: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1749
    dataType: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1750
    success: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1751
    type: "GET",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1752
    async: true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1753
    xhr: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1754
      return new global.XMLHttpRequest();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1755
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1756
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1757
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1758
  Popcorn.xhr = function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1759
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1760
    options.dataType = options.dataType && options.dataType.toLowerCase() || null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1761
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1762
    if ( options.dataType &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1763
         ( options.dataType === "jsonp" || options.dataType === "script" ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1764
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1765
      Popcorn.xhr.getJSONP(
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1766
        options.url,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1767
        options.success,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1768
        options.dataType === "script"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1769
      );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1770
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1771
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1772
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1773
    var settings = Popcorn.extend( {}, setup, options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1774
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1775
    //  Create new XMLHttpRequest object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1776
    settings.ajax  = settings.xhr();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1777
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1778
    if ( settings.ajax ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1779
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1780
      if ( settings.type === "GET" && settings.data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1781
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1782
        //  append query string
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1783
        settings.url += ( rparams.test( settings.url ) ? "&" : "?" ) + settings.data;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1784
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1785
        //  Garbage collect and reset settings.data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1786
        settings.data = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1787
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1788
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1789
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1790
      settings.ajax.open( settings.type, settings.url, settings.async );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1791
      settings.ajax.send( settings.data || null );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1792
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1793
      return Popcorn.xhr.httpData( settings );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1794
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1795
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1796
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1797
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1798
  Popcorn.xhr.httpData = function( settings ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1799
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1800
    var data, json = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1801
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1802
    settings.ajax.onreadystatechange = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1803
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1804
      if ( settings.ajax.readyState === 4 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1805
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1806
        try {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1807
          json = JSON.parse( settings.ajax.responseText );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1808
        } catch( e ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1809
          //suppress
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1810
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1811
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1812
        data = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1813
          xml: settings.ajax.responseXML,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1814
          text: settings.ajax.responseText,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1815
          json: json
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1816
        };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1817
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1818
        //  If a dataType was specified, return that type of data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1819
        if ( settings.dataType ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1820
          data = data[ settings.dataType ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1821
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1822
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1823
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1824
        settings.success.call( settings.ajax, data );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1825
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1826
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1827
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1828
    return data;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1829
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1830
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1831
  Popcorn.xhr.getJSONP = function( url, success, isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1832
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1833
    var head = document.head || document.getElementsByTagName( "head" )[ 0 ] || document.documentElement,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1834
      script = document.createElement( "script" ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1835
      paramStr = url.split( "?" )[ 1 ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1836
      isFired = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1837
      params = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1838
      callback, parts, callparam;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1839
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1840
    if ( paramStr && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1841
      params = paramStr.split( "&" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1842
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1843
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1844
    if ( params.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1845
      parts = params[ params.length - 1 ].split( "=" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1846
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1847
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1848
    callback = params.length ? ( parts[ 1 ] ? parts[ 1 ] : parts[ 0 ]  ) : "jsonp";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1849
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1850
    if ( !paramStr && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1851
      url += "?callback=" + callback;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1852
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1853
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1854
    if ( callback && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1855
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1856
      //  If a callback name already exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1857
      if ( !!window[ callback ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1858
        //  Create a new unique callback name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1859
        callback = Popcorn.guid( callback );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1860
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1861
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1862
      //  Define the JSONP success callback globally
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1863
      window[ callback ] = function( data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1864
        // Fire success callbacks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1865
        success && success( data );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1866
        isFired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1867
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1868
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1869
      //  Replace callback param and callback name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1870
      url = url.replace( parts.join( "=" ), parts[ 0 ] + "=" + callback );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1871
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1872
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1873
    script.onload = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1874
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1875
      //  Handling remote script loading callbacks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1876
      if ( isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1877
        //  getScript
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1878
        success && success();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1879
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1880
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1881
      //  Executing for JSONP requests
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1882
      if ( isFired ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1883
        //  Garbage collect the callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1884
        delete window[ callback ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1885
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1886
      //  Garbage collect the script resource
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1887
      head.removeChild( script );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1888
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1889
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1890
    script.src = url;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1891
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1892
    head.insertBefore( script, head.firstChild );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1893
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1894
    return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1895
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1896
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1897
  Popcorn.getJSONP = Popcorn.xhr.getJSONP;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1898
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1899
  Popcorn.getScript = Popcorn.xhr.getScript = function( url, success ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1900
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1901
    return Popcorn.xhr.getJSONP( url, success, true );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1902
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1903
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1904
  Popcorn.util = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1905
    // Simple function to parse a timestamp into seconds
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1906
    // Acceptable formats are:
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1907
    // HH:MM:SS.MMM
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1908
    // HH:MM:SS;FF
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1909
    // Hours and minutes are optional. They default to 0
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1910
    toSeconds: function( timeStr, framerate ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1911
      // Hours and minutes are optional
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1912
      // Seconds must be specified
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1913
      // Seconds can be followed by milliseconds OR by the frame information
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1914
      var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1915
          errorMessage = "Invalid time format",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1916
          digitPairs, lastIndex, lastPair, firstPair,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1917
          frameInfo, frameTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1918
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1919
      if ( typeof timeStr === "number" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1920
        return timeStr;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1921
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1922
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1923
      if ( typeof timeStr === "string" &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1924
            !validTimeFormat.test( timeStr ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1925
        Popcorn.error( errorMessage );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1926
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1927
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1928
      digitPairs = timeStr.split( ":" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1929
      lastIndex = digitPairs.length - 1;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1930
      lastPair = digitPairs[ lastIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1931
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1932
      // Fix last element:
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1933
      if ( lastPair.indexOf( ";" ) > -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1934
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1935
        frameInfo = lastPair.split( ";" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1936
        frameTime = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1937
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1938
        if ( framerate && ( typeof framerate === "number" ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1939
          frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1940
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1941
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1942
        digitPairs[ lastIndex ] = parseInt( frameInfo[ 0 ], 10 ) + frameTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1943
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1944
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1945
      firstPair = digitPairs[ 0 ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1946
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1947
      return {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1948
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1949
        1: parseFloat( firstPair, 10 ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1950
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1951
        2: ( parseInt( firstPair, 10 ) * 60 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1952
              parseFloat( digitPairs[ 1 ], 10 ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1953
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1954
        3: ( parseInt( firstPair, 10 ) * 3600 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1955
            ( parseInt( digitPairs[ 1 ], 10 ) * 60 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1956
              parseFloat( digitPairs[ 2 ], 10 )
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1957
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1958
      }[ digitPairs.length || 1 ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1959
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1960
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1961
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1962
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1963
  // Initialize locale data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1964
  // Based on http://en.wikipedia.org/wiki/Language_localisation#Language_tags_and_codes
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1965
  function initLocale( arg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1966
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1967
    var locale = typeof arg === "string" ? arg : [ arg.language, arg.region ].join( "-" ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1968
        parts = locale.split( "-" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1969
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1970
    // Setup locale data table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1971
    return {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1972
      iso6391: locale,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1973
      language: parts[ 0 ] || "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1974
      region: parts[ 1 ] || ""
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1975
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1976
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1977
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1978
  // Declare locale data table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1979
  var localeData = initLocale( global.navigator.userLanguage || global.navigator.language );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1980
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1981
  Popcorn.locale = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1982
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1983
    // Popcorn.locale.get()
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1984
    // returns reference to privately
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1985
    // defined localeData
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1986
    get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1987
      return localeData;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1988
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1989
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1990
    // Popcorn.locale.set( string|object );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1991
    set: function( arg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1992
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1993
      localeData = initLocale( arg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1994
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1995
      Popcorn.locale.broadcast();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1996
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1997
      return localeData;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1998
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1999
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2000
    // Popcorn.locale.broadcast( type )
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2001
    // Sends events to all popcorn media instances that are
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2002
    // listening for locale events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2003
    broadcast: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2004
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2005
      var instances = Popcorn.instances,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2006
          length = instances.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2007
          idx = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2008
          instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2009
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2010
      type = type || "locale:changed";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2011
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2012
      // Iterate all current instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2013
      for ( ; idx < length; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2014
        instance = instances[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2015
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2016
        // For those instances with locale event listeners,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2017
        // trigger a locale change event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2018
        if ( type in instance.data.events  ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2019
          instance.trigger( type );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2020
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2021
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2022
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2023
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2024
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2025
  // alias for exec function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2026
  Popcorn.p.cue = Popcorn.p.exec;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2027
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2028
  function getItems() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2029
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2030
    var item,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2031
        list = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2032
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2033
    if ( Object.keys ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2034
      list = Object.keys( Popcorn.p );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2035
    } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2036
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2037
      for ( item in Popcorn.p ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2038
        if ( hasOwn.call( Popcorn.p, item ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2039
          list.push( item );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2040
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2041
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2042
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2043
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2044
    return list.join( "," ).toLowerCase().split( ",");
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2045
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2046
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2047
  //  Protected API methods
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2048
  Popcorn.protect = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2049
    natives: getItems() 
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2050
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2051
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2052
  //  Exposes Popcorn to global context
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2053
  global.Popcorn = Popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2054
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2055
})(window, window.document);