src/js/pop.js
author hamidouk
Fri, 02 Dec 2011 15:42:06 +0100
branchno-popcorn
changeset 394 89a79a2c6014
parent 391 0a68395f7e12
child 417 bae7c50704d7
permissions -rw-r--r--
WIP - working on the code function.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
387
d1fe53ad8d72 initial commit
hamidouk
parents:
diff changeset
     1
/* wrapper that simulates popcorn.js because
d1fe53ad8d72 initial commit
hamidouk
parents:
diff changeset
     2
   popcorn is a bit unstable at the time */
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
     3
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
     4
Popcorn = {};
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
     5
Popcorn.media = { "paused": true};
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
     6
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
     7
Popcorn.listen = function(msg, callback) {
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
     8
  IriSP.jQuery(Popcorn).bind(msg, function(event, rest) { callback(rest); });
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
     9
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    10
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    11
Popcorn.trigger = function(msg, params) {
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    12
  IriSP.jQuery(Popcorn).trigger(msg, params);
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    13
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    14
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    15
Popcorn.guid = function(prefix) {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    16
  var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    17
      var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    18
      return v.toString(16);
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    19
   });
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    20
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    21
  return prefix + str;
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    22
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    23
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    24
Popcorn.__initApi = function() {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    25
  Popcorn.trigger("timeupdate");
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    26
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    27
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    28
Popcorn.jwplayer = function(container, options) {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    29
  Popcorn._container = container.slice(1); //eschew the '#'
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    30
  options.events = {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    31
      onReady: Popcorn.__initApi,
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    32
      onTime: Popcorn.__timeHandler,
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    33
      onSeek: function(event) { 
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    34
                              var i = 0;
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    35
                              console.log("old: %d, new: %d", event.position, event.offset);
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    36
                              for(i = 0; i < Popcorn.__codes.length; i++) {
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    37
                                 var c = Popcorn.__codes[i];
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    38
                                
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    39
                                 if (event.position >= c.start && event.position < c.end) {
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    40
                                    c.onEnd();
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    41
                                 }
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    42
                                
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    43
                                 if (typeof(event.offset) === "undefined")
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    44
                                   event.offset = 0;
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    45
                                 if (event.offset >= c.start && event.offset < c.end) {
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    46
                                   c.onStart();
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    47
                                 }
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    48
                                 
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    49
                               }
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    50
                         
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    51
                           Popcorn.trigger("timeupdate");
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    52
              }
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    53
  }
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    54
    
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    55
  jwplayer(Popcorn._container).setup(options);
391
0a68395f7e12 added duration. automatically trigger timeupdate.
hamidouk
parents: 390
diff changeset
    56
  Popcorn.media.duration = options.duration;
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    57
  return Popcorn;
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    58
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    59
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    60
Popcorn.currentTime = function(time) {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    61
  if (typeof(time) === "undefined") {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    62
      return jwplayer(Popcorn._container).getPosition();            
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    63
  } else {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    64
     var currentTime = +time;
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    65
     jwplayer( Popcorn._container ).seek( currentTime );
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    66
     return jwplayer(Popcorn._container).getPosition();            
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    67
  }
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    68
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    69
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    70
Popcorn.play = function() {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    71
      Popcorn.media.paused = false;
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    72
//      Popcorn.trigger("play");
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    73
//      Popcorn.trigger("playing");
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    74
      jwplayer( Popcorn._container ).play();
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    75
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    76
    
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    77
Popcorn.pause = function() {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    78
      if ( !Popcorn.media.paused ) {
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    79
        Popcorn.media.paused = true;
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    80
        Popcorn.trigger( "pause" );
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    81
        jwplayer( Popcorn._container ).pause();
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    82
      }
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    83
};
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    84
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    85
Popcorn.muted = function(val) {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    86
  if (typeof(val) !== "undefined") {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    87
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    88
    if (jwplayer(Popcorn._container).getMute() !== val) {
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    89
      if (val) {
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
    90
        jwplayer(Popcorn._container).setMute(true);
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    91
      } else {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    92
        jwplayer( Popcorn._container ).setMute(false);
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    93
      }
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    94
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
    95
      Popcorn.trigger( "volumechange" );
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    96
    }
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    97
    
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    98
    return jwplayer( Popcorn._container ).getMute();
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
    99
  } else {
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
   100
    return jwplayer( Popcorn._container ).getMute();
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
   101
  }
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   102
};
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   103
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   104
Popcorn.mute = Popcorn.muted;
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   105
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   106
Popcorn.__codes = [];
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   107
Popcorn.code = function(options) {
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   108
  Popcorn.__codes.push(options);
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   109
  return Popcorn;
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   110
};
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   111
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   112
Popcorn.__runCode = function() {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   113
  var currentTime = jwplayer(Popcorn._container).getPosition();
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   114
  var i = 0;
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   115
  for(i = 0; i < Popcorn.__codes.length; i++) {
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   116
    var c = Popcorn.__codes[i];
391
0a68395f7e12 added duration. automatically trigger timeupdate.
hamidouk
parents: 390
diff changeset
   117
    if (currentTime == c.start) {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   118
      c.onStart();
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   119
    }
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   120
    
391
0a68395f7e12 added duration. automatically trigger timeupdate.
hamidouk
parents: 390
diff changeset
   121
    if (currentTime == c.end) {
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   122
      c.onEnd();
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   123
    }
388
30277c1e3d46 replaced most of popcorn's functions.
hamidouk
parents: 387
diff changeset
   124
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   125
  }
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   126
};
391
0a68395f7e12 added duration. automatically trigger timeupdate.
hamidouk
parents: 390
diff changeset
   127
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   128
/* called everytime the player updates itself 
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   129
   (onTime event)
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   130
 */
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   131
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   132
Popcorn.__timeHandler = function() {
391
0a68395f7e12 added duration. automatically trigger timeupdate.
hamidouk
parents: 390
diff changeset
   133
  Popcorn.trigger("timeupdate");
390
9b4e5f606d72 first draft, a little rough around the edges.
hamidouk
parents: 388
diff changeset
   134
};
394
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   135
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   136
Popcorn.roundTime = function() {
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   137
  var currentTime = Popcorn.currentTime();
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   138
  return Math.round(currentTime);
89a79a2c6014 WIP - working on the code function.
hamidouk
parents: 391
diff changeset
   139
};