src/js/libs/popcorn.js
author hamidouk
Mon, 19 Dec 2011 15:25:22 +0100
branchpopcorn-port
changeset 481 a46cfeee6d77
parent 429 e84a7100cc3f
permissions -rw-r--r--
using jquery ui draggable changes the state of an element from absolute to relative positioning, which breaks the way our seek button expands itself, so we need to force absolute positioning, quite uglily, using jquery.
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
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    13
    while ( methods.length ) {
110
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
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    61
  refresh = function( obj ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    62
    var currentTime = obj.media.currentTime,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    63
      animation = obj.options.frameAnimation,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    64
      disabled = obj.data.disabled,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    65
      tracks = obj.data.trackEvents,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    66
      animating = tracks.animating,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    67
      start = tracks.startIndex,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    68
      registryByName = Popcorn.registryByName,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    69
      animIndex = 0,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    70
      byStart, natives, type;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    71
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    72
    start = Math.min( start + 1, tracks.byStart.length - 2 );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    73
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    74
    while ( start > 0 && tracks.byStart[ start ] ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    75
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    76
      byStart = tracks.byStart[ start ];
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    77
      natives = byStart._natives;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    78
      type = natives && natives.type;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    79
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    80
      if ( !natives ||
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    81
          ( !!registryByName[ type ] || !!obj[ type ] ) ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    82
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    83
        if ( ( byStart.start <= currentTime && byStart.end > currentTime ) &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    84
                disabled.indexOf( type ) === -1 ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    85
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    86
          if ( !byStart._running ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    87
            byStart._running = true;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    88
            natives.start.call( obj, null, byStart );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    89
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    90
            // if the 'frameAnimation' option is used,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    91
            // push the current byStart object into the `animating` cue
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    92
            if ( animation &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    93
                ( byStart && byStart._running && byStart.natives.frame ) ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    94
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    95
              natives.frame.call( obj, null, byStart, currentTime );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    96
            }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    97
          }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    98
        } else if ( byStart._running === true ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
    99
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   100
          byStart._running = false;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   101
          natives.end.call( obj, null, byStart );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   102
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   103
          if ( animation && byStart._natives.frame ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   104
            animIndex = animating.indexOf( byStart );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   105
            if ( animIndex >= 0 ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   106
              animating.splice( animIndex, 1 );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   107
            }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   108
          }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   109
        }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   110
      }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   111
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   112
      start--;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   113
    }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   114
  },
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   115
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   116
  //  Declare constructor
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   117
  //  Returns an instance object.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   118
  Popcorn = function( entity, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   119
    //  Return new Popcorn object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   120
    return new Popcorn.p.init( entity, options || 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
  //  Popcorn API version, automatically inserted via build system.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   124
  Popcorn.version = "@VERSION";
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
  //  Boolean flag allowing a client to determine if Popcorn can be supported
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   127
  Popcorn.isSupported = true;
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
  //  Instance caching
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   130
  Popcorn.instances = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   131
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   132
  //  Declare a shortcut (Popcorn.p) to and a definition of
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   133
  //  the new prototype for our Popcorn constructor
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   134
  Popcorn.p = Popcorn.prototype = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   135
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   136
    init: function( entity, options ) {
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
      var matches;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   139
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   140
      //  Supports Popcorn(function () { /../ })
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   141
      //  Originally proposed by Daniel Brooks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   142
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   143
      if ( typeof entity === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   144
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   145
        //  If document ready has already fired
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   146
        if ( document.readyState === "interactive" || document.readyState === "complete" ) {
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
          entity( document, Popcorn );
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
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   151
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   152
        //  Add `entity` fn to ready stack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   153
        readyStack.push( entity );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   154
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   155
        //  This process should happen once per page load
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   156
        if ( !readyBound ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   157
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   158
          //  set readyBound flag
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   159
          readyBound = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   160
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   161
          var DOMContentLoaded  = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   162
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   163
            readyFired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   164
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   165
            //  Remove global DOM ready listener
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   166
            document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   167
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   168
            //  Execute all ready function in the stack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   169
            for ( var i = 0, readyStackLength = readyStack.length; i < readyStackLength; i++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   170
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   171
              readyStack[ i ].call( document, Popcorn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   172
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
            //  GC readyStack
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   175
            readyStack = null;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   178
          //  Register global DOM ready listener
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   179
          document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   182
        return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   183
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   184
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   185
      //  Check if entity is a valid string id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   186
      matches = rIdExp.exec( entity );
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
      //  Get media element by id or object reference
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   189
      this.media = matches && matches.length && matches[ 2 ] ?
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   190
                     document.getElementById( matches[ 2 ] ) :
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   191
                     entity;
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
      //  Create an audio or video element property reference
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   194
      this[ ( this.media.nodeName && this.media.nodeName.toLowerCase() ) || "video" ] = this.media;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   195
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   196
      //  Register new instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   197
      Popcorn.instances.push( this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   198
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   199
      this.options = options || {};
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
      this.isDestroyed = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   202
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   203
      this.data = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   204
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   205
        // Executed by either timeupdate event or in rAF loop
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   206
        timeUpdate: Popcorn.nop,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   207
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   208
        // Allows disabling a plugin per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   209
        disabled: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   210
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   211
        // Stores DOM event queues by type
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   212
        events: {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   213
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   214
        // Stores Special event hooks data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   215
        hooks: {},
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
        // Store track event history data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   218
        history: [],
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
        // Stores ad-hoc state related data]
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   221
        state: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   222
          volume: this.media.volume
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   225
        // Store track event object references by trackId
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   226
        trackRefs: {},
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
        // Playback track event queues
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   229
        trackEvents: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   230
          byStart: [{
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
            start: -1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   233
            end: -1
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   234
          }],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   235
          byEnd: [{
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   236
            start: -1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   237
            end: -1
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
          animating: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   240
          startIndex: 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   241
          endIndex: 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   242
          previousUpdateTime: -1
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   246
      //  Wrap true ready check
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   247
      var isReady = function( that ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   248
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   249
        var duration, videoDurationPlus;
110
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 ( that.media.readyState >= 2 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   252
          //  Adding padding to the front and end of the arrays
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   253
          //  this is so we do not fall off either end
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
          duration = that.media.duration;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   256
          //  Check for no duration info (NaN)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   257
          videoDurationPlus = duration != duration ? Number.MAX_VALUE : duration + 1;
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
          Popcorn.addTrackEvent( that, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   260
            start: videoDurationPlus,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   261
            end: videoDurationPlus
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 ( that.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   265
            //  if Popcorn is created with frameAnimation option set to true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   266
            //  requestAnimFrame is used instead of "timeupdate" media event.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   267
            //  This is for greater frame time accuracy, theoretically up to
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   268
            //  60 frames per second as opposed to ~4 ( ~every 15-250ms)
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   269
            that.data.timeUpdate = function () {
110
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
              Popcorn.timeUpdate( that, {} );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   272
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   273
              that.trigger( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   274
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   275
              !that.isDestroyed && requestAnimFrame( that.data.timeUpdate );
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   276
            };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   277
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   278
            !that.isDestroyed && requestAnimFrame( that.data.timeUpdate );
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   279
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   280
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   281
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   282
            that.data.timeUpdate = function( event ) {
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   283
              Popcorn.timeUpdate( that, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   284
            };
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
            if ( !that.isDestroyed ) {
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   287
              that.media.addEventListener( "timeupdate", that.data.timeUpdate, false );
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   288
            }
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
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   291
          global.setTimeout(function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   292
            isReady( that );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   293
          }, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   294
        }
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   297
      isReady( this );
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
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   300
    }
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   303
  //  Extend constructor prototype to instance prototype
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   304
  //  Allows chaining methods to instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   305
  Popcorn.p.init.prototype = Popcorn.p;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   306
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   307
  Popcorn.forEach = function( obj, fn, context ) {
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
    if ( !obj || !fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   310
      return {};
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   313
    context = context || this;
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
    var key, len;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   316
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   317
    // Use native whenever possible
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   318
    if ( forEach && obj.forEach === forEach ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   319
      return obj.forEach( fn, context );
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
    if ( toString.call( obj ) === "[object NodeList]" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   323
      for ( key = 0, len = obj.length; key < len; key++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   324
        fn.call( context, obj[ key ], key, obj );
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
      return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   327
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   328
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   329
    for ( key in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   330
      if ( hasOwn.call( obj, key ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   331
        fn.call( context, obj[ key ], key, obj );
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
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   334
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   335
  };
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
  Popcorn.extend = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   338
    var dest = obj, src = slice.call( arguments, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   339
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   340
    Popcorn.forEach( src, function( copy ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   341
      for ( var prop in copy ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   342
        dest[ prop ] = copy[ prop ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   343
      }
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   346
    return dest;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   350
  // A Few reusable utils, memoized onto Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   351
  Popcorn.extend( Popcorn, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   352
    noConflict: function( deep ) {
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
      if ( deep ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   355
        global.Popcorn = _Popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   356
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   357
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   358
      return Popcorn;
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
    error: function( msg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   361
      throw new Error( msg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   362
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   363
    guid: function( prefix ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   364
      Popcorn.guid.counter++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   365
      return  ( prefix ? prefix : "" ) + ( +new Date() + Popcorn.guid.counter );
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
    sizeOf: function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   368
      var size = 0;
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
      for ( var prop in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   371
        size++;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   374
      return size;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   375
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   376
    isArray: Array.isArray || function( array ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   377
      return toString.call( array ) === "[object Array]";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   378
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   379
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   380
    nop: function() {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   381
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   382
    position: function( elem ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   383
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   384
      var clientRect = elem.getBoundingClientRect(),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   385
          bounds = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   386
          doc = elem.ownerDocument,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   387
          docElem = document.documentElement,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   388
          body = document.body,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   389
          clientTop, clientLeft, scrollTop, scrollLeft, top, left;
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
      //  Determine correct clientTop/Left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   392
      clientTop = docElem.clientTop || body.clientTop || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   393
      clientLeft = docElem.clientLeft || body.clientLeft || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   394
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   395
      //  Determine correct scrollTop/Left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   396
      scrollTop = ( global.pageYOffset && docElem.scrollTop || body.scrollTop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   397
      scrollLeft = ( global.pageXOffset && docElem.scrollLeft || body.scrollLeft );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   398
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   399
      //  Temp top/left
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   400
      top = Math.ceil( clientRect.top + scrollTop - clientTop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   401
      left = Math.ceil( clientRect.left + scrollLeft - clientLeft );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   402
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   403
      for ( var p in clientRect ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   404
        bounds[ p ] = Math.round( clientRect[ p ] );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   407
      return Popcorn.extend({}, bounds, { top: top, left: left });
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   410
    disable: function( instance, plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   411
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   412
      var disabled = instance.data.disabled;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   413
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   414
      if ( disabled.indexOf( plugin ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   415
        disabled.push( plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   416
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   417
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   418
      refresh( instance );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   419
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   420
      return instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   421
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   422
    enable: function( instance, plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   423
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   424
      var disabled = instance.data.disabled,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   425
          index = disabled.indexOf( plugin );
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 ( index > -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   428
        disabled.splice( index, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   429
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   430
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   431
      refresh( instance );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   432
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   433
      return instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   434
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   435
    destroy: function( instance ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   436
      var events = instance.data.events,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   437
          singleEvent, item, fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   438
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   439
      //  Iterate through all events and remove them
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   440
      for ( item in events ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   441
        singleEvent = events[ item ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   442
        for ( fn in singleEvent ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   443
          delete singleEvent[ fn ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   444
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   445
        events[ item ] = null;
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
      if ( !instance.isDestroyed ) {
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   449
        instance.data.timeUpdate && instance.media.removeEventListener( "timeupdate", instance.data.timeUpdate, false );
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   450
        instance.isDestroyed = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   451
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   452
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   453
  });
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
  //  Memoized GUID Counter
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   456
  Popcorn.guid.counter = 1;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   457
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   458
  //  Factory to implement getters, setters and controllers
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   459
  //  as Popcorn instance methods. The IIFE will create and return
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   460
  //  an object with defined methods
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   461
  Popcorn.extend(Popcorn.p, (function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   462
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   463
      var methods = "load play pause currentTime playbackRate volume duration preload playbackRate " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   464
                    "autoplay loop controls muted buffered readyState seeking paused played seekable ended",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   465
          ret = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   466
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   467
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   468
      //  Build methods, store in object that is returned and passed to extend
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   469
      Popcorn.forEach( methods.split( /\s+/g ), function( name ) {
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
        ret[ name ] = function( arg ) {
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
          if ( typeof this.media[ name ] === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   474
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   475
            // Support for shorthanded play(n)/pause(n) jump to currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   476
            // 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
   477
            // allowed shorthandable methods, then set the currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   478
            // Supports time as seconds or SMPTE
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   479
            if ( arg != null && /play|pause/.test( name ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   480
              this.media.currentTime = Popcorn.util.toSeconds( arg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   481
            }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   482
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   483
            this.media[ name ]();
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
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   486
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   487
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   488
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   489
          if ( arg != null ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   490
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   491
            this.media[ name ] = arg;
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
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   494
          }
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.media[ name ];
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   500
      return ret;
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
    })()
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
  Popcorn.forEach( "enable disable".split(" "), function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   506
    Popcorn.p[ method ] = function( plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   507
      return Popcorn[ method ]( this, plugin );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   511
  Popcorn.extend(Popcorn.p, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   512
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   513
    //  Rounded currentTime
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   514
    roundTime: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   515
      return -~this.media.currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   516
    },
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
    //  Attach an event to a single point in time
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   519
    exec: function( time, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   520
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   521
      //  Creating a one second track event with an empty end
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   522
      Popcorn.addTrackEvent( this, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   523
        start: time,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   524
        end: time + 1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   525
        _running: false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   526
        _natives: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   527
          start: fn || Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   528
          end: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   529
          type: "exec"
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
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   532
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   533
      return this;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   536
    // Mute the calling media, optionally toggle
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   537
    mute: function( toggle ) {
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
      var event = toggle == null || toggle === true ? "muted" : "unmuted";
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
      // If `toggle` is explicitly `false`,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   542
      // unmute the media and restore the volume level
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   543
      if ( event === "unmuted" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   544
        this.media.muted = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   545
        this.media.volume = this.data.state.volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   546
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   547
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   548
      // If `toggle` is either null or undefined,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   549
      // save the current volume and mute the media element
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   550
      if ( event === "muted" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   551
        this.data.state.volume = this.media.volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   552
        this.media.muted = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   553
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   554
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   555
      // Trigger either muted|unmuted event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   556
      this.trigger( event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   557
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   558
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   559
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   560
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   561
    // Convenience method, unmute the calling media
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   562
    unmute: function( toggle ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   563
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   564
      return this.mute( toggle == null ? false : !toggle );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   567
    // Get the client bounding box of an instance element
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   568
    position: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   569
      return Popcorn.position( this.media );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   570
    },
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
    // Toggle a plugin's playback behaviour (on or off) per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   573
    toggle: function( plugin ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   574
      return Popcorn[ this.data.disabled.indexOf( plugin ) > -1 ? "enable" : "disable" ]( this, plugin );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   575
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   576
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   577
    // Set default values for plugin options objects per instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   578
    defaults: function( plugin, defaults ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   579
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   580
      // If an array of default configurations is provided,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   581
      // iterate and apply each to this instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   582
      if ( Popcorn.isArray( plugin ) ) {
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.forEach( plugin, function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   585
          for ( var name in obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   586
            this.defaults( name, obj[ name ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   587
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   588
        }, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   589
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   590
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   591
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   592
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   593
      if ( !this.options.defaults ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   594
        this.options.defaults = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   595
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   596
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   597
      if ( !this.options.defaults[ plugin ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   598
        this.options.defaults[ plugin ] = {};
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   601
      Popcorn.extend( this.options.defaults[ plugin ], defaults );
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
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   604
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   605
  });
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
  Popcorn.Events  = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   608
    UIEvents: "blur focus focusin focusout load resize scroll unload",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   609
    MouseEvents: "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave click dblclick",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   610
    Events: "loadstart progress suspend emptied stalled play pause " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   611
            "loadedmetadata loadeddata waiting playing canplay canplaythrough " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   612
            "seeking seeked timeupdate ended ratechange durationchange volumechange"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   613
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   614
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   615
  Popcorn.Events.Natives = Popcorn.Events.UIEvents + " " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   616
                           Popcorn.Events.MouseEvents + " " +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   617
                           Popcorn.Events.Events;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   618
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   619
  internal.events.apiTypes = [ "UIEvents", "MouseEvents", "Events" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   620
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   621
  // Privately compile events table at load time
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   622
  (function( events, data ) {
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
    var apis = internal.events.apiTypes,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   625
    eventsList = events.Natives.split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   626
    idx = 0, len = eventsList.length, prop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   627
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   628
    for( ; idx < len; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   629
      data.hash[ eventsList[idx] ] = true;
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
    apis.forEach(function( val, idx ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   633
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   634
      data.apis[ val ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   635
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   636
      var apiEvents = events[ val ].split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   637
      len = apiEvents.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   638
      k = 0;
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
      for ( ; k < len; k++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   641
        data.apis[ val ][ apiEvents[ k ] ] = true;
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
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   644
  })( Popcorn.Events, internal.events );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   645
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   646
  Popcorn.events = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   647
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   648
    isNative: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   649
      return !!internal.events.hash[ type ];
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
    getInterface: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   652
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   653
      if ( !Popcorn.events.isNative( type ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   654
        return false;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   657
      var eventApi = internal.events,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   658
        apis = eventApi.apiTypes,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   659
        apihash = eventApi.apis,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   660
        idx = 0, len = apis.length, api, tmp;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   661
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   662
      for ( ; idx < len; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   663
        tmp = apis[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   664
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   665
        if ( apihash[ tmp ][ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   666
          api = tmp;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   667
          break;
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
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   670
      return api;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   671
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   672
    //  Compile all native events to single array
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   673
    all: Popcorn.Events.Natives.split( /\s+/g ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   674
    //  Defines all Event handling static functions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   675
    fn: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   676
      trigger: function( type, data ) {
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
        var eventInterface, evt;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   679
        //  setup checks for custom event system
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   680
        if ( this.data.events[ type ] && Popcorn.sizeOf( this.data.events[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   681
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   682
          eventInterface  = Popcorn.events.getInterface( type );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   683
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   684
          if ( eventInterface ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   685
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   686
            evt = document.createEvent( eventInterface );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   687
            evt.initEvent( type, true, true, global, 1 );
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
            this.media.dispatchEvent( evt );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   690
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   691
            return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   692
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   693
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   694
          //  Custom events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   695
          Popcorn.forEach( this.data.events[ type ], function( obj, key ) {
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
            obj.call( this, data );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   698
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   699
          }, this );
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
        return this;
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
      listen: function( type, fn ) {
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
        var self = this,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   708
            hasEvents = true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   709
            eventHook = Popcorn.events.hooks[ type ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   710
            origType = type,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   711
            tmp;
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
        if ( !this.data.events[ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   714
          this.data.events[ type ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   715
          hasEvents = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   716
        }
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
        // Check and setup event hooks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   719
        if ( eventHook ) {
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
          // Execute hook add method if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   722
          if ( eventHook.add ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   723
            eventHook.add.call( this, {}, fn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   724
          }
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
          // Reassign event type to our piggyback event type if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   727
          if ( eventHook.bind ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   728
            type = eventHook.bind;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   731
          // Reassign handler if defined
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   732
          if ( eventHook.handler ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   733
            tmp = fn;
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
            fn = function wrapper( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   736
              eventHook.handler.call( self, event, tmp );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   740
          // assume the piggy back event is registered
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   741
          hasEvents = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   742
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   743
          // Setup event registry entry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   744
          if ( !this.data.events[ type ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   745
            this.data.events[ type ] = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   746
            // Toggle if the previous assumption was untrue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   747
            hasEvents = false;
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
        //  Register event and handler
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   752
        this.data.events[ type ][ fn.name || ( fn.toString() + Popcorn.guid() ) ] = fn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   753
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   754
        // only attach one event of any type
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   755
        if ( !hasEvents && Popcorn.events.all.indexOf( type ) > -1 ) {
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
          this.media.addEventListener( type, function( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   758
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   759
            Popcorn.forEach( self.data.events[ type ], function( obj, key ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   760
              if ( typeof obj === "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   761
                obj.call( self, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   762
              }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   763
            });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   764
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   765
          }, false);
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   766
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   767
        return this;
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
      unlisten: function( type, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   770
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   771
        if ( this.data.events[ type ] && this.data.events[ type ][ fn ] ) {
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
          delete this.data.events[ type ][ fn ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   774
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   775
          return this;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   778
        this.data.events[ type ] = null;
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
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   781
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   782
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   783
    hooks: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   784
      canplayall: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   785
        bind: "canplaythrough",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   786
        add: function( event, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   787
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   788
          var state = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   789
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   790
          if ( this.media.readyState ) {
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
            callback.call( this, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   793
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   794
            state = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   795
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   796
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   797
          this.data.hooks.canplayall = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   798
            fired: state
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
        // declare special handling instructions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   802
        handler: function canplayall( event, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   803
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   804
          if ( !this.data.hooks.canplayall.fired ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   805
            // trigger original user callback once
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   806
            callback.call( this, event );
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
            this.data.hooks.canplayall.fired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   809
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   810
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   811
      }
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
  };
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
  //  Extend Popcorn.events.fns (listen, unlisten, trigger) to all Popcorn instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   816
  Popcorn.forEach( [ "trigger", "listen", "unlisten" ], function( key ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   817
    Popcorn.p[ key ] = Popcorn.events.fn[ key ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   818
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   819
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   820
  // Internal Only - Adds track events to the instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   821
  Popcorn.addTrackEvent = function( obj, track ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   822
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   823
    // Determine if this track has default options set for it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   824
    // If so, apply them to the track object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   825
    if ( track && track._natives && track._natives.type &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   826
        ( obj.options.defaults && obj.options.defaults[ track._natives.type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   827
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   828
      track = Popcorn.extend( {}, obj.options.defaults[ track._natives.type ], track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   829
    }
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
    if ( track._natives ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   832
      //  Supports user defined track event id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   833
      track._id = !track.id ? Popcorn.guid( track._natives.type ) : track.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
      //  Push track event ids into the history
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   836
      obj.data.history.push( track._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   837
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   838
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   839
    track.start = Popcorn.util.toSeconds( track.start, obj.options.framerate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   840
    track.end   = Popcorn.util.toSeconds( track.end, obj.options.framerate );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   841
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   842
    //  Store this definition in an array sorted by times
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   843
    var byStart = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   844
        byEnd = obj.data.trackEvents.byEnd,
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   845
        startIndex, endIndex,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   846
        currentTime;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   847
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   848
    for ( startIndex = byStart.length - 1; startIndex >= 0; startIndex-- ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   849
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   850
      if ( track.start >= byStart[ startIndex ].start ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   851
        byStart.splice( startIndex + 1, 0, track );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   852
        break;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   853
      }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   854
    }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   855
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   856
    for ( endIndex = byEnd.length - 1; endIndex >= 0; endIndex-- ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   857
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   858
      if ( track.end > byEnd[ endIndex ].end ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   859
        byEnd.splice( endIndex + 1, 0, track );
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   860
        break;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   861
      }
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
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   864
    // Display track event immediately if it's enabled and current
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   865
    if ( track._natives &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   866
        ( !!Popcorn.registryByName[ track._natives.type ] || !!obj[ track._natives.type ] ) ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   867
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   868
      currentTime = obj.media.currentTime;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   869
      if ( track.end > currentTime &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   870
        track.start <= currentTime &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   871
        obj.data.disabled.indexOf( track._natives.type ) === -1 ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   872
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   873
        track._running = true;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   874
        track._natives.start.call( obj, null, track );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   875
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   876
        if ( obj.options.frameAnimation &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   877
          track._natives.frame ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   878
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   879
          obj.data.trackEvents.animating.push( track );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   880
          track._natives.frame.call( obj, null, track, currentTime );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   881
        }
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   882
      }
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
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   885
    // update startIndex and endIndex
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   886
    if ( startIndex <= obj.data.trackEvents.startIndex &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   887
      track.start <= obj.data.trackEvents.previousUpdateTime ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   888
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   889
      obj.data.trackEvents.startIndex++;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   890
    }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   891
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   892
    if ( endIndex <= obj.data.trackEvents.endIndex &&
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   893
      track.end < obj.data.trackEvents.previousUpdateTime ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   894
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   895
      obj.data.trackEvents.endIndex++;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   896
    }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   897
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
   898
    this.timeUpdate( obj, null, true );
110
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
    // Store references to user added trackevents in ref table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   901
    if ( track._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   902
      Popcorn.addTrackEvent.ref( obj, track );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   903
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   904
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   905
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   906
  // 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
   907
  Popcorn.addTrackEvent.ref = function( obj, track ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   908
    obj.data.trackRefs[ track._id ] = track;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   909
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   910
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   911
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   912
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   913
  Popcorn.removeTrackEvent  = function( obj, trackId ) {
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
    var historyLen = obj.data.history.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   916
        indexWasAt = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   917
        byStart = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   918
        byEnd = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   919
        animating = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   920
        history = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   921
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   922
    Popcorn.forEach( obj.data.trackEvents.byStart, function( o, i, context ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   923
      // Preserve the original start/end trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   924
      if ( !o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   925
        byStart.push( obj.data.trackEvents.byStart[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   926
        byEnd.push( obj.data.trackEvents.byEnd[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   927
      }
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
      // Filter for user track events (vs system track events)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   930
      if ( o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   931
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   932
        // Filter for the trackevent to remove
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   933
        if ( o._id !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   934
          byStart.push( obj.data.trackEvents.byStart[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   935
          byEnd.push( obj.data.trackEvents.byEnd[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   936
        }
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
        //  Capture the position of the track being removed.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   939
        if ( o._id === trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   940
          indexWasAt = i;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   941
          o._natives._teardown && o._natives._teardown.call( obj, o );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   942
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   943
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   944
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   945
    });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   946
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   947
    if ( obj.data.trackEvents.animating.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   948
      Popcorn.forEach( obj.data.trackEvents.animating, function( o, i, context ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   949
        // Preserve the original start/end trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   950
        if ( !o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   951
          animating.push( obj.data.trackEvents.animating[i] );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   954
        // Filter for user track events (vs system track events)
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   955
        if ( o._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   956
          // Filter for the trackevent to remove
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   957
          if ( o._id !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   958
            animating.push( obj.data.trackEvents.animating[i] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   959
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   960
        }
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
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   963
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   964
    //  Update
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   965
    if ( indexWasAt <= obj.data.trackEvents.startIndex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   966
      obj.data.trackEvents.startIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   967
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   968
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   969
    if ( indexWasAt <= obj.data.trackEvents.endIndex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   970
      obj.data.trackEvents.endIndex--;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   973
    obj.data.trackEvents.byStart = byStart;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   974
    obj.data.trackEvents.byEnd = byEnd;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   975
    obj.data.trackEvents.animating = animating;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   976
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   977
    for ( var i = 0; i < historyLen; i++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   978
      if ( obj.data.history[ i ] !== trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   979
        history.push( obj.data.history[ i ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   980
      }
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   983
    // Update ordered history array
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   984
    obj.data.history = history;
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
    // Update track event references
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   987
    Popcorn.removeTrackEvent.ref( obj, trackId );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   988
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   989
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   990
  // 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
   991
  Popcorn.removeTrackEvent.ref = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   992
    delete obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   993
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   994
    return obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   995
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   996
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   997
  // Return an array of track events bound to this instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   998
  Popcorn.getTrackEvents = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   999
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1000
    var trackevents = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1001
      refs = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1002
      length = refs.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1003
      idx = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1004
      ref;
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
    for ( ; idx < length; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1007
      ref = refs[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1008
      // Return only user attributed track event references
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1009
      if ( ref._id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1010
        trackevents.push( ref );
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
    return trackevents;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1015
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1016
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1017
  // Internal Only - Returns an instance object's trackRefs hash table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1018
  Popcorn.getTrackEvents.ref = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1019
    return obj.data.trackRefs;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1022
  // Return a single track event bound to this instance object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1023
  Popcorn.getTrackEvent = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1024
    return obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1025
  };
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
  // Internal Only - Returns an instance object's track reference by track id
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1028
  Popcorn.getTrackEvent.ref = function( obj, trackId ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1029
    return obj.data.trackRefs[ trackId ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1030
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1031
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1032
  Popcorn.getLastTrackEventId = function( obj ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1033
    return obj.data.history[ obj.data.history.length - 1 ];
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1036
  Popcorn.timeUpdate = function( obj, event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1037
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1038
    var currentTime = obj.media.currentTime,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1039
        previousTime = obj.data.trackEvents.previousUpdateTime,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1040
        tracks = obj.data.trackEvents,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1041
        animating = tracks.animating,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1042
        end = tracks.endIndex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1043
        start = tracks.startIndex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1044
        animIndex = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1045
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1046
        registryByName = Popcorn.registryByName,
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
        byEnd, byStart, byAnimate, natives, type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1049
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1050
    //  Playbar advancing
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1051
    if ( previousTime <= currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1052
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1053
      while ( tracks.byEnd[ end ] && tracks.byEnd[ end ].end <= currentTime ) {
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
        byEnd = tracks.byEnd[ end ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1056
        natives = byEnd._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1057
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1058
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1059
        //  If plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1060
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1061
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1062
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1063
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1064
          if ( byEnd._running === true ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1065
            byEnd._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1066
            natives.end.call( obj, event, byEnd );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1067
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1068
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1069
          end++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1070
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1071
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1072
          Popcorn.removeTrackEvent( obj, byEnd._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1073
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1074
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1075
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1076
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1077
      while ( tracks.byStart[ start ] && tracks.byStart[ start ].start <= currentTime ) {
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
        byStart = tracks.byStart[ start ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1080
        natives = byStart._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1081
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1082
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1083
        //  If plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1084
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1085
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1086
              !!obj[ type ] ) ) {
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
          if ( byStart.end > currentTime &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1089
                byStart._running === false &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1090
                  obj.data.disabled.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1091
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1092
            byStart._running = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1093
            natives.start.call( obj, event, byStart );
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
            // If the `frameAnimation` option is used,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1096
            // push the current byStart object into the `animating` cue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1097
            if ( obj.options.frameAnimation &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1098
                ( byStart && byStart._running && byStart._natives.frame ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1099
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1100
              animating.push( byStart );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1101
            }
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
          start++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1104
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1105
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1106
          Popcorn.removeTrackEvent( obj, byStart._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1107
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1108
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1109
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1111
      // If the `frameAnimation` option is used, iterate the animating track
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1112
      // and execute the `frame` callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1113
      if ( obj.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1114
        while ( animIndex < animating.length ) {
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
          byAnimate = animating[ animIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1117
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1118
          if ( !byAnimate._running ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1119
            animating.splice( animIndex, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1120
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1121
            byAnimate._natives.frame.call( obj, event, byAnimate, currentTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1122
            animIndex++;
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
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1125
      }
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
    // Playbar receding
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1128
    } else if ( previousTime > currentTime ) {
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
      while ( tracks.byStart[ start ] && tracks.byStart[ start ].start > currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1131
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1132
        byStart = tracks.byStart[ start ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1133
        natives = byStart._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1134
        type = natives && natives.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1135
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1136
        // if plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1137
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1138
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1139
              !!obj[ type ] ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1140
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1141
          if ( byStart._running === true ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1142
            byStart._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1143
            natives.end.call( obj, event, byStart );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1144
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1145
          start--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1146
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1147
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1148
          Popcorn.removeTrackEvent( obj, byStart._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1149
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1150
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1151
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1152
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1153
      while ( tracks.byEnd[ end ] && tracks.byEnd[ end ].end > currentTime ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1154
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1155
        byEnd = tracks.byEnd[ end ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1156
        natives = byEnd._natives;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1157
        type = natives && natives.type;
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 plugin does not exist on this instance, remove it
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1160
        if ( !natives ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1161
            ( !!registryByName[ type ] ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1162
              !!obj[ type ] ) ) {
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
          if ( byEnd.start <= currentTime &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1165
                byEnd._running === false  &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1166
                  obj.data.disabled.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1167
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1168
            byEnd._running = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1169
            natives.start.call( obj, event, byEnd );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1170
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1171
            // If the `frameAnimation` option is used,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1172
            // push the current byEnd object into the `animating` cue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1173
            if ( obj.options.frameAnimation &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1174
                  ( byEnd && byEnd._running && byEnd._natives.frame ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1175
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1176
              animating.push( byEnd );
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
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1179
          end--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1180
        } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1181
          // remove track event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1182
          Popcorn.removeTrackEvent( obj, byEnd._id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1183
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1184
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1185
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1186
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1187
      // If the `frameAnimation` option is used, iterate the animating track
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1188
      // and execute the `frame` callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1189
      if ( obj.options.frameAnimation ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1190
        while ( animIndex < animating.length ) {
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
          byAnimate = animating[ animIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1193
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1194
          if ( !byAnimate._running ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1195
            animating.splice( animIndex, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1196
          } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1197
            byAnimate._natives.frame.call( obj, event, byAnimate, currentTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1198
            animIndex++;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1199
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1200
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1201
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1202
    // time bar is not moving ( video is paused )
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1205
    tracks.endIndex = end;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1206
    tracks.startIndex = start;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1207
    tracks.previousUpdateTime = currentTime;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1210
  //  Map and Extend TrackEvent functions to all Popcorn instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1211
  Popcorn.extend( Popcorn.p, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1212
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1213
    getTrackEvents: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1214
      return Popcorn.getTrackEvents.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1215
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1216
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1217
    getTrackEvent: function( id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1218
      return Popcorn.getTrackEvent.call( null, this, id );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1221
    getLastTrackEventId: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1222
      return Popcorn.getLastTrackEventId.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1223
    },
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
    removeTrackEvent: function( id ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1226
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1227
      Popcorn.removeTrackEvent.call( null, this, id );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1228
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1229
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1230
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1231
    removePlugin: function( name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1232
      Popcorn.removePlugin.call( null, this, name );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1233
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1234
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1235
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1236
    timeUpdate: function( event ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1237
      Popcorn.timeUpdate.call( null, this, event );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1238
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1239
    },
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
    destroy: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1242
      Popcorn.destroy.call( null, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1243
      return this;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1247
  //  Plugin manifests
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1248
  Popcorn.manifest = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1249
  //  Plugins are registered
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1250
  Popcorn.registry = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1251
  Popcorn.registryByName = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1252
  //  An interface for extending Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1253
  //  with plugin functionality
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1254
  Popcorn.plugin = function( name, definition, manifest ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1255
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1256
    if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1257
      Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1258
      return;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1261
    //  Provides some sugar, but ultimately extends
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1262
    //  the definition into Popcorn.p
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1263
    var reserved = [ "start", "end" ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1264
        plugin = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1265
        setup,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1266
        isfn = typeof definition === "function",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1267
        methods = [ "_setup", "_teardown", "start", "end", "frame" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1268
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1269
    // combines calls of two function calls into one
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1270
    var combineFn = function( first, second ) {
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
      first = first || Popcorn.nop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1273
      second = second || Popcorn.nop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1274
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1275
      return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1276
        first.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1277
        second.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1278
      };
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1281
    //  If `manifest` arg is undefined, check for manifest within the `definition` object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1282
    //  If no `definition.manifest`, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1283
    Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1284
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1285
    // apply safe, and empty default functions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1286
    methods.forEach(function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1287
      definition[ method ] = safeTry( definition[ method ] || Popcorn.nop, name );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1290
    var pluginFn = function( setup, options ) {
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
      if ( !options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1293
        return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1294
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1295
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1296
      //  Storing the plugin natives
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1297
      var natives = options._natives = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1298
          compose = "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1299
          defaults, originalOpts, manifestOpts, mergedSetupOpts;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1300
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1301
      Popcorn.extend( natives, setup );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1302
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1303
      options._natives.type = name;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1304
      options._running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1305
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1306
      natives.start = natives.start || natives[ "in" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1307
      natives.end = natives.end || natives[ "out" ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1308
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1309
      // extend teardown to always call end if running
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1310
      natives._teardown = combineFn(function() {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1311
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1312
        var args = slice.call( arguments );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1313
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1314
        // end function signature is not the same as teardown,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1315
        // put null on the front of arguments for the event parameter
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1316
        args.unshift( null );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1317
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1318
        // only call end if event is running
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1319
        args[ 1 ]._running && natives.end.apply( this, args );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1320
      }, natives._teardown );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1321
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1322
      // Check for previously set default options
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1323
      defaults = this.options.defaults && this.options.defaults[ options._natives && options._natives.type ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1324
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1325
      // default to an empty string if no effect exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1326
      // split string into an array of effects
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1327
      options.compose = options.compose && options.compose.split( " " ) || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1328
      options.effect = options.effect && options.effect.split( " " ) || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1329
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1330
      // join the two arrays together
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1331
      options.compose = options.compose.concat( options.effect );
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
      options.compose.forEach(function( composeOption ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1334
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1335
        // if the requested compose is garbage, throw it away
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1336
        compose = Popcorn.compositions[ composeOption ] || {};
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
        // extends previous functions with compose function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1339
        methods.forEach(function( method ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1340
          natives[ method ] = combineFn( natives[ method ], compose[ method ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1341
        });
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1344
      //  Ensure a manifest object, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1345
      options._natives.manifest = manifest;
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
      //  Checks for expected properties
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1348
      if ( !( "start" in options ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1349
        options.start = options[ "in" ] || 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1350
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1351
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1352
      if ( !( "end" in options ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1353
        options.end = options[ "out" ] || this.duration() || Number.MAX_VALUE;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1354
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1355
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1356
      // Merge with defaults if they exist, make sure per call is prioritized
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1357
      mergedSetupOpts = defaults ? Popcorn.extend( {}, defaults, options ) :
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1358
                          options;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1359
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1360
      // Resolves 239, 241, 242
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1361
      if ( !mergedSetupOpts.target ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1362
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1363
        //  Sometimes the manifest may be missing entirely
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1364
        //  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
  1365
        manifestOpts = "options" in manifest && manifest.options;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1366
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1367
        mergedSetupOpts.target = manifestOpts && "target" in manifestOpts && manifestOpts.target;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1368
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1369
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1370
      // Trigger _setup method if exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1371
      options._natives._setup && options._natives._setup.call( this, mergedSetupOpts );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1372
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1373
      // Create new track event for this instance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1374
      Popcorn.addTrackEvent( this, Popcorn.extend( mergedSetupOpts, options ) );
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
      //  Future support for plugin event definitions
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1377
      //  for all of the native events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1378
      Popcorn.forEach( setup, function( callback, type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1379
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1380
        if ( type !== "type" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1381
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1382
          if ( reserved.indexOf( type ) === -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1383
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1384
            this.listen( type, callback );
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
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1387
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1388
      }, this );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1389
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1390
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1391
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1392
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1393
    //  Assign new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1394
    plugin[ name ] = function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1395
      return pluginFn.call( this, isfn ? definition.call( this, options ) : definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1396
                                  options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1397
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1398
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1399
    //  Extend Popcorn.p with new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1400
    Popcorn.extend( Popcorn.p, plugin );
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
    //  Push into the registry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1403
    var entry = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1404
      fn: plugin[ name ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1405
      definition: definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1406
      base: definition,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1407
      parents: [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1408
      name: name
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
    Popcorn.registry.push(
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1411
       Popcorn.extend( plugin, entry, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1412
        type: name
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
    Popcorn.registryByName[ name ] = entry;
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
    return plugin;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1420
  // Storage for plugin function errors
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1421
  Popcorn.plugin.errors = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1422
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1423
  // Returns wrapped plugin function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1424
  function safeTry( fn, pluginName ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1425
    return function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1426
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1427
      //  When Popcorn.plugin.debug is true, do not suppress errors
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1428
      if ( Popcorn.plugin.debug ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1429
        return fn.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1430
      }
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
      try {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1433
        return fn.apply( this, arguments );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1434
      } catch ( ex ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1435
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1436
        // Push plugin function errors into logging queue
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1437
        Popcorn.plugin.errors.push({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1438
          plugin: pluginName,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1439
          thrown: ex,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1440
          source: fn.toString()
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1441
        });
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
        // Trigger an error that the instance can listen for
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1444
        // and react to
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1445
        this.trigger( "error", Popcorn.plugin.errors );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1446
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1447
    };
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
  // Debug-mode flag for plugin development
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1451
  Popcorn.plugin.debug = false;
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
  //  removePlugin( type ) removes all tracks of that from all instances of popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1454
  //  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
  1455
  Popcorn.removePlugin = function( obj, name ) {
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
    //  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
  1458
    if ( !name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1459
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1460
      //  Fix the order
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1461
      name = obj;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1462
      obj = Popcorn.p;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1463
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1464
      if ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1465
        Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1466
        return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1467
      }
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
      var registryLen = Popcorn.registry.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1470
          registryIdx;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1471
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1472
      // remove plugin reference from registry
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1473
      for ( registryIdx = 0; registryIdx < registryLen; registryIdx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1474
        if ( Popcorn.registry[ registryIdx ].name === name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1475
          Popcorn.registry.splice( registryIdx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1476
          delete Popcorn.registryByName[ name ];
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1477
          delete Popcorn.manifest[ name ];
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1478
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1479
          // delete the plugin
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1480
          delete obj[ name ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1481
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1482
          // plugin found and removed, stop checking, we are done
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1483
          return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1484
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1485
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1486
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
    var byStart = obj.data.trackEvents.byStart,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1490
        byEnd = obj.data.trackEvents.byEnd,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1491
        animating = obj.data.trackEvents.animating,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1492
        idx, sl;
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
    // remove all trackEvents
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1495
    for ( idx = 0, sl = byStart.length; idx < sl; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1496
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1497
      if ( ( byStart[ idx ] && byStart[ idx ]._natives && byStart[ idx ]._natives.type === name ) &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1498
                ( byEnd[ idx ] && byEnd[ idx ]._natives && byEnd[ idx ]._natives.type === name ) ) {
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
        byStart[ idx ]._natives._teardown && byStart[ idx ]._natives._teardown.call( obj, byStart[ idx ] );
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
        byStart.splice( idx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1503
        byEnd.splice( idx, 1 );
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
        // update for loop if something removed, but keep checking
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1506
        idx--; sl--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1507
        if ( obj.data.trackEvents.startIndex <= idx ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1508
          obj.data.trackEvents.startIndex--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1509
          obj.data.trackEvents.endIndex--;
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
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1512
    }
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
    //remove all animating events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1515
    for ( idx = 0, sl = animating.length; idx < sl; idx++ ) {
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
      if ( animating[ idx ] && animating[ idx ]._natives && animating[ idx ]._natives.type === name ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1518
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1519
        animating.splice( idx, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1520
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1521
        // update for loop if something removed, but keep checking
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1522
        idx--; sl--;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1523
      }
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
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1528
  Popcorn.compositions = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1529
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1530
  //  Plugin inheritance
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1531
  Popcorn.compose = function( name, definition, manifest ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1532
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1533
    //  If `manifest` arg is undefined, check for manifest within the `definition` object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1534
    //  If no `definition.manifest`, an empty object is a sufficient fallback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1535
    Popcorn.manifest[ name ] = manifest = manifest || definition.manifest || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1536
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1537
    // register the effect by name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1538
    Popcorn.compositions[ name ] = definition;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1539
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1540
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1541
  Popcorn.plugin.effect = Popcorn.effect = Popcorn.compose;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1542
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1543
  // stores parsers keyed on filetype
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1544
  Popcorn.parsers = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1545
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1546
  // An interface for extending Popcorn
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1547
  // with parser functionality
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1548
  Popcorn.parser = function( name, type, definition ) {
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 ( Popcorn.protect.natives.indexOf( name.toLowerCase() ) >= 0 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1551
      Popcorn.error( "'" + name + "' is a protected function name" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1552
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1553
    }
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
    // fixes parameters for overloaded function call
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1556
    if ( typeof type === "function" && !definition ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1557
      definition = type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1558
      type = "";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1559
    }
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
    if ( typeof definition !== "function" || typeof type !== "string" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1562
      return;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1565
    // Provides some sugar, but ultimately extends
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1566
    // the definition into Popcorn.p
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1567
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1568
    var natives = Popcorn.events.all,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1569
        parseFn,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1570
        parser = {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1571
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1572
    parseFn = function( filename, callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1573
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1574
      if ( !filename ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1575
        return this;
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 that = this;
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
      Popcorn.xhr({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1581
        url: filename,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1582
        dataType: type,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1583
        success: function( data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1584
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1585
          var tracksObject = definition( data ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1586
              tracksData,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1587
              tracksDataLen,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1588
              tracksDef,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1589
              idx = 0;
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
          tracksData = tracksObject.data || [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1592
          tracksDataLen = tracksData.length;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1593
          tracksDef = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1594
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1595
          //  If no tracks to process, return immediately
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1596
          if ( !tracksDataLen ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1597
            return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1598
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1599
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1600
          //  Create tracks out of parsed object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1601
          for ( ; idx < tracksDataLen; idx++ ) {
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
            tracksDef = tracksData[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1604
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1605
            for ( var key in tracksDef ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1606
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1607
              if ( hasOwn.call( tracksDef, key ) && !!that[ key ] ) {
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
                that[ key ]( tracksDef[ key ] );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1610
              }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1611
            }
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
          if ( callback ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1614
            callback();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1615
          }
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
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1618
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1619
      return this;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1620
    };
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
    // Assign new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1623
    parser[ name ] = parseFn;
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
    // Extend Popcorn.p with new named definition
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1626
    Popcorn.extend( Popcorn.p, parser );
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
    // keys the function name by filetype extension
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1629
    //Popcorn.parsers[ name ] = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1630
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1631
    return parser;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1632
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1633
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1634
  Popcorn.player = function( name, player ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1635
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1636
    player = player || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1637
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1638
    var playerFn = function( target, src, options ) {
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
      options = options || {};
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1641
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1642
      // List of events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1643
      var date = new Date() / 1000,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1644
          baselineTime = date,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1645
          currentTime = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1646
          volume = 1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1647
          muted = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1648
          events = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1649
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1650
          // The container div of the resource
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1651
          container = document.getElementById( rIdExp.exec( target ) && rIdExp.exec( target )[ 2 ] ) ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1652
                        document.getElementById( target ) ||
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1653
                          target,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1654
          basePlayer = {},
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1655
          timeout,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1656
          popcorn;
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
      // copies a div into the media object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1659
      for( var val in container ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1660
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1661
        if ( typeof container[ val ] === "object" ) {
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
          basePlayer[ val ] = container[ val ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1664
        } else if ( typeof container[ val ] === "function" ) {
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
          basePlayer[ val ] = (function( value ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1667
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1668
            // this is a stupid ugly kludgy hack in honour of Safari
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1669
            // in Safari a NodeList is a function, not an object
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1670
            if ( "length" in container[ value ] && !container[ value ].call ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1671
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1672
              return container[ value ];
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1673
            } else {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1674
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1675
              return function() {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1676
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1677
                return container[ value ].apply( container, arguments );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1678
              };
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1679
            }
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1680
          }( val ));
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1681
        } else {
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
          Popcorn.player.defineProperty( basePlayer, val, {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1684
            get: (function( value ) {
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
              return function() {
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
                return container[ value ];
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 )),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1691
            set: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1692
            configurable: true
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
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1695
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1696
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1697
      var timeupdate = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1698
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1699
        date = new Date() / 1000;
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 ( !basePlayer.paused ) {
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
          basePlayer.currentTime = basePlayer.currentTime + ( date - baselineTime );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1704
          basePlayer.dispatchEvent( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1705
          timeout = setTimeout( timeupdate, 10 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1706
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1707
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1708
        baselineTime = date;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1709
      };
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
      basePlayer.play = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1712
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1713
        this.paused = false;
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 ( basePlayer.readyState >= 4 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1716
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1717
          baselineTime = new Date() / 1000;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1718
          basePlayer.dispatchEvent( "play" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1719
          timeupdate();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1720
        }
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.pause = 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
        this.paused = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1726
        basePlayer.dispatchEvent( "pause" );
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.player.defineProperty( basePlayer, "currentTime", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1730
        get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1731
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1732
          return currentTime;
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
        set: function( val ) {
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
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1737
          currentTime = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1738
          basePlayer.dispatchEvent( "timeupdate" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1739
          return currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1740
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1741
        configurable: true
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1744
      Popcorn.player.defineProperty( basePlayer, "volume", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1745
        get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1746
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1747
          return volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1748
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1749
        set: function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1750
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1751
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1752
          volume = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1753
          basePlayer.dispatchEvent( "volumechange" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1754
          return volume;
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
        configurable: true
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1759
      Popcorn.player.defineProperty( basePlayer, "muted", {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1760
        get: function() {
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
          return muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1763
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1764
        set: function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1765
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1766
          // make sure val is a number
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1767
          muted = +val;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1768
          basePlayer.dispatchEvent( "volumechange" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1769
          return muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1770
        },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1771
        configurable: true
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1774
      // Adds an event listener to the object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1775
      basePlayer.addEventListener = function( evtName, fn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1776
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1777
        if ( !events[ evtName ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1778
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1779
          events[ evtName ] = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1780
        }
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
        events[ evtName ].push( fn );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1783
        return fn;
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1786
      // Can take event object or simple string
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1787
      basePlayer.dispatchEvent = function( oEvent ) {
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
        var evt,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1790
            self = this,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1791
            eventInterface,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1792
            eventName = oEvent.type;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1793
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1794
        // A string was passed, create event object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1795
        if ( !eventName ) {
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
          eventName = oEvent;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1798
          eventInterface  = Popcorn.events.getInterface( eventName );
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
          if ( eventInterface ) {
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
            evt = document.createEvent( eventInterface );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1803
            evt.initEvent( eventName, true, true, window, 1 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1804
          }
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1807
        Popcorn.forEach( events[ eventName ], function( val ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1808
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1809
          val.call( self, evt, self );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1813
      // Attempt to get src from playerFn parameter
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1814
      basePlayer.src = src || "";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1815
      basePlayer.readyState = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1816
      basePlayer.duration = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1817
      basePlayer.paused = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1818
      basePlayer.ended = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1819
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1820
      if ( player._setup ) {
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
        player._setup.call( basePlayer, options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1823
      } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1824
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1825
        // there is no setup, which means there is nothing to load
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1826
        basePlayer.readyState = 4;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1827
        basePlayer.dispatchEvent( "load" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1828
        basePlayer.dispatchEvent( "loadeddata" );
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
      // when a custom player is loaded, load basePlayer state into custom player
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1832
      basePlayer.addEventListener( "load", function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1833
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1834
        // 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
  1835
        basePlayer.currentTime = currentTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1836
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1837
        // same as above with volume and muted
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1838
        basePlayer.volume = volume;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1839
        basePlayer.muted = muted;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1840
      });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1841
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1842
      basePlayer.addEventListener( "loadeddata", function() {
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 play was called before player ready, start playing video
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1845
        !basePlayer.paused && basePlayer.play();
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
      popcorn = new Popcorn.p.init( basePlayer, options );
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
      return popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1851
    };
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
    Popcorn[ name ] = Popcorn[ name ] || playerFn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1854
  };
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
  Popcorn.player.defineProperty = Object.defineProperty || function( object, description, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1857
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1858
    object.__defineGetter__( description, options.get || Popcorn.nop );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1859
    object.__defineSetter__( description, options.set || Popcorn.nop );
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
  //  Cache references to reused RegExps
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1863
  var rparams = /\?/,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1864
  //  XHR Setup object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1865
  setup = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1866
    url: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1867
    data: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1868
    dataType: "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1869
    success: Popcorn.nop,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1870
    type: "GET",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1871
    async: true,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1872
    xhr: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1873
      return new global.XMLHttpRequest();
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
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1876
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1877
  Popcorn.xhr = function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1878
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1879
    options.dataType = options.dataType && options.dataType.toLowerCase() || null;
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
    if ( options.dataType &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1882
         ( options.dataType === "jsonp" || options.dataType === "script" ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1883
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1884
      Popcorn.xhr.getJSONP(
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1885
        options.url,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1886
        options.success,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1887
        options.dataType === "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
      return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1890
    }
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
    var settings = Popcorn.extend( {}, setup, options );
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
    //  Create new XMLHttpRequest object
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1895
    settings.ajax  = settings.xhr();
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
    if ( settings.ajax ) {
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
      if ( settings.type === "GET" && settings.data ) {
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
        //  append query string
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1902
        settings.url += ( rparams.test( settings.url ) ? "&" : "?" ) + settings.data;
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
        //  Garbage collect and reset settings.data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1905
        settings.data = null;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1906
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1907
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1908
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1909
      settings.ajax.open( settings.type, settings.url, settings.async );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1910
      settings.ajax.send( settings.data || null );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1911
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1912
      return Popcorn.xhr.httpData( settings );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1913
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1914
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1915
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1916
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1917
  Popcorn.xhr.httpData = function( settings ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1918
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1919
    var data, json = null,
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1920
        parser, xml = null;
110
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
    settings.ajax.onreadystatechange = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1923
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1924
      if ( settings.ajax.readyState === 4 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1925
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1926
        try {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1927
          json = JSON.parse( settings.ajax.responseText );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1928
        } catch( e ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1929
          //suppress
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1930
        }
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
        data = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1933
          xml: settings.ajax.responseXML,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1934
          text: settings.ajax.responseText,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1935
          json: json
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1936
        };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1937
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1938
        // Normalize: data.xml is non-null in IE9 regardless of if response is valid xml
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1939
        if ( !data.xml || !data.xml.documentElement ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1940
          data.xml = null;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1941
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1942
          try {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1943
            parser = new DOMParser();
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1944
            xml = parser.parseFromString( settings.ajax.responseText, "text/xml" );
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1945
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1946
            if ( !xml.getElementsByTagName( "parsererror" ).length ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1947
              data.xml = xml;
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1948
            }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1949
          } catch ( e ) {
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1950
            // data.xml remains null
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1951
          }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1952
        }
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  1953
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1954
        //  If a dataType was specified, return that type of data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1955
        if ( settings.dataType ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1956
          data = data[ settings.dataType ];
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
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
        settings.success.call( settings.ajax, data );
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
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1964
    return data;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1965
  };
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
  Popcorn.xhr.getJSONP = function( url, success, isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1968
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1969
    var head = document.head || document.getElementsByTagName( "head" )[ 0 ] || document.documentElement,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1970
      script = document.createElement( "script" ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1971
      paramStr = url.split( "?" )[ 1 ],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1972
      isFired = false,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1973
      params = [],
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1974
      callback, parts, callparam;
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
    if ( paramStr && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1977
      params = paramStr.split( "&" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1978
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1979
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1980
    if ( params.length ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1981
      parts = params[ params.length - 1 ].split( "=" );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1984
    callback = params.length ? ( parts[ 1 ] ? parts[ 1 ] : parts[ 0 ]  ) : "jsonp";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1985
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1986
    if ( !paramStr && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1987
      url += "?callback=" + callback;
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
    if ( callback && !isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1991
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1992
      //  If a callback name already exists
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1993
      if ( !!window[ callback ] ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1994
        //  Create a new unique callback name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1995
        callback = Popcorn.guid( callback );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1998
      //  Define the JSONP success callback globally
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  1999
      window[ callback ] = function( data ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2000
        // Fire success callbacks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2001
        success && success( data );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2002
        isFired = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2003
      };
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
      //  Replace callback param and callback name
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2006
      url = url.replace( parts.join( "=" ), parts[ 0 ] + "=" + callback );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2007
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2008
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2009
    script.onload = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2010
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2011
      //  Handling remote script loading callbacks
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2012
      if ( isScript ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2013
        //  getScript
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2014
        success && success();
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2017
      //  Executing for JSONP requests
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2018
      if ( isFired ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2019
        //  Garbage collect the callback
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2020
        delete window[ callback ];
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
      //  Garbage collect the script resource
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2023
      head.removeChild( script );
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
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2026
    script.src = url;
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
    head.insertBefore( script, head.firstChild );
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
    return;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2031
  };
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
  Popcorn.getJSONP = Popcorn.xhr.getJSONP;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2034
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2035
  Popcorn.getScript = Popcorn.xhr.getScript = function( url, success ) {
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
    return Popcorn.xhr.getJSONP( url, success, true );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2038
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2039
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2040
  Popcorn.util = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2041
    // Simple function to parse a timestamp into seconds
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2042
    // Acceptable formats are:
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2043
    // HH:MM:SS.MMM
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2044
    // HH:MM:SS;FF
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2045
    // Hours and minutes are optional. They default to 0
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2046
    toSeconds: function( timeStr, framerate ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2047
      // Hours and minutes are optional
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2048
      // Seconds must be specified
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2049
      // Seconds can be followed by milliseconds OR by the frame information
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2050
      var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2051
          errorMessage = "Invalid time format",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2052
          digitPairs, lastIndex, lastPair, firstPair,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2053
          frameInfo, frameTime;
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
      if ( typeof timeStr === "number" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2056
        return timeStr;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2057
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2058
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2059
      if ( typeof timeStr === "string" &&
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2060
            !validTimeFormat.test( timeStr ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2061
        Popcorn.error( errorMessage );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2062
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2063
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2064
      digitPairs = timeStr.split( ":" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2065
      lastIndex = digitPairs.length - 1;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2066
      lastPair = digitPairs[ lastIndex ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2067
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2068
      // Fix last element:
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2069
      if ( lastPair.indexOf( ";" ) > -1 ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2070
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2071
        frameInfo = lastPair.split( ";" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2072
        frameTime = 0;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2073
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2074
        if ( framerate && ( typeof framerate === "number" ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2075
          frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2076
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2077
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2078
        digitPairs[ lastIndex ] = parseInt( frameInfo[ 0 ], 10 ) + frameTime;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2079
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2080
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2081
      firstPair = digitPairs[ 0 ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2082
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2083
      return {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2084
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2085
        1: parseFloat( firstPair, 10 ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2086
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2087
        2: ( parseInt( firstPair, 10 ) * 60 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2088
              parseFloat( digitPairs[ 1 ], 10 ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2089
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2090
        3: ( parseInt( firstPair, 10 ) * 3600 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2091
            ( parseInt( digitPairs[ 1 ], 10 ) * 60 ) +
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2092
              parseFloat( digitPairs[ 2 ], 10 )
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2093
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2094
      }[ digitPairs.length || 1 ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2095
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2096
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2097
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2098
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2099
  // Initialize locale data
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2100
  // 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
  2101
  function initLocale( arg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2102
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2103
    var locale = typeof arg === "string" ? arg : [ arg.language, arg.region ].join( "-" ),
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2104
        parts = locale.split( "-" );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2105
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2106
    // Setup locale data table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2107
    return {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2108
      iso6391: locale,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2109
      language: parts[ 0 ] || "",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2110
      region: parts[ 1 ] || ""
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2111
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2112
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2113
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2114
  // Declare locale data table
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2115
  var localeData = initLocale( global.navigator.userLanguage || global.navigator.language );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2116
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2117
  Popcorn.locale = {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2118
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2119
    // Popcorn.locale.get()
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2120
    // returns reference to privately
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2121
    // defined localeData
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2122
    get: function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2123
      return localeData;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2124
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2125
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2126
    // Popcorn.locale.set( string|object );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2127
    set: function( arg ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2128
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2129
      localeData = initLocale( arg );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2130
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2131
      Popcorn.locale.broadcast();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2132
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2133
      return localeData;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2134
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2135
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2136
    // Popcorn.locale.broadcast( type )
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2137
    // Sends events to all popcorn media instances that are
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2138
    // listening for locale events
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2139
    broadcast: function( type ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2140
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2141
      var instances = Popcorn.instances,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2142
          length = instances.length,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2143
          idx = 0,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2144
          instance;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2145
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2146
      type = type || "locale:changed";
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2147
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2148
      // Iterate all current instances
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2149
      for ( ; idx < length; idx++ ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2150
        instance = instances[ idx ];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2151
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2152
        // For those instances with locale event listeners,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2153
        // trigger a locale change event
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2154
        if ( type in instance.data.events  ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2155
          instance.trigger( type );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2156
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2157
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2158
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2159
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2160
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2161
  // alias for exec function
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2162
  Popcorn.p.cue = Popcorn.p.exec;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2163
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2164
  function getItems() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2165
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2166
    var item,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2167
        list = [];
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2168
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2169
    if ( Object.keys ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2170
      list = Object.keys( Popcorn.p );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2171
    } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2172
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2173
      for ( item in Popcorn.p ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2174
        if ( hasOwn.call( Popcorn.p, item ) ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2175
          list.push( item );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2176
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2177
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2178
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2179
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2180
    return list.join( "," ).toLowerCase().split( ",");
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2181
  }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2182
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2183
  //  Protected API methods
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2184
  Popcorn.protect = {
429
e84a7100cc3f bumped popcorn version.
hamidouk
parents: 110
diff changeset
  2185
    natives: getItems()
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2186
  };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2187
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2188
  //  Exposes Popcorn to global context
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2189
  global.Popcorn = Popcorn;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2190
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
  2191
})(window, window.document);