src/js/pop.js
branchno-popcorn
changeset 390 9b4e5f606d72
parent 388 30277c1e3d46
child 391 0a68395f7e12
--- a/src/js/pop.js	Fri Dec 02 12:01:43 2011 +0100
+++ b/src/js/pop.js	Fri Dec 02 12:02:05 2011 +0100
@@ -2,6 +2,8 @@
    popcorn is a bit unstable at the time */
 
 Popcorn = {};
+Popcorn.media = {};
+
 Popcorn.listen = function(msg, callback) {
   IriSP.jQuery(Popcorn).bind(msg, callback);
 };
@@ -24,13 +26,14 @@
 };
 
 Popcorn.jwplayer = function(container, options) {
-  Popcorn._container = container;
+  Popcorn._container = container.slice(1); //eschew the '#'
   options.events = {
-      onReady: Popcorn.__initApi
+      onReady: Popcorn.__initApi,
+      onTime: Popcorn.__timehandler 
     };
     
   jwplayer(Popcorn._container).setup(options);
-
+  return Popcorn;
 };
 
 Popcorn.currentTime = function(time) {
@@ -38,23 +41,23 @@
       return jwplayer(Popcorn._container).getPosition();            
   } else {
      var currentTime = +time;
-     media.dispatchEvent( "seeked" );
-     media.dispatchEvent( "timeupdate" );
+     Popcorn.trigger("seeked");
+     Popcorn.trigger("timeupdate");
      jwplayer( Popcorn._container ).seek( currentTime );
      return jwplayer(Popcorn._container).getPosition();            
   }
 };
 
 Popcorn.play = function() {
-      Popcorn.paused = false;
+      Popcorn.media.paused = false;
       Popcorn.trigger("play");
       Popcorn.trigger("playing");
       jwplayer( Popcorn._container ).play();
 };
     
 Popcorn.pause = function() {
-      if ( !media.paused ) {
-        media.paused = true;
+      if ( !Popcorn.media.paused ) {
+        Popcorn.media.paused = true;
         Popcorn.trigger( "pause" );
         jwplayer( Popcorn._container ).pause();
       }
@@ -62,9 +65,10 @@
 
 Popcorn.muted = function(val) {
   if (typeof(val) !== "undefined") {
-    if ( jwplayer( Popcorn._container ).getMute() !== val ) {
-      if ( val ) {
-        jwplayer( Popcorn._container ).setMute(true);
+
+    if (jwplayer(Popcorn._container).getMute() !== val) {
+      if (val) {
+        jwplayer(Popcorn._container).setMute(true);
       } else {
         jwplayer( Popcorn._container ).setMute(false);
       }
@@ -76,6 +80,30 @@
   } else {
     return jwplayer( Popcorn._container ).getMute();
   }
-});
+};
+
+Popcorn.__codes = [];
+Popcorn.code = function(options) {
+  Popcorn.__codes.push(options);
+  return Popcorn;
+};
+
+/* called everytime the player updates itself 
+   (onTime event)
+ */
 
+Popcorn.__timehandler = function() {
+  var currentTime = jwplayer(Popcorn._container).getPosition();
+  var i = 0;
+  for(i = 0; i < Popcorn.__codes.length; i++) {
+    var c = Popcorn.__codes[i];
+    if (currentTime <= c.start && currentTime + 0.1 >= c.start) {
+      c.onStart();
+    }
+    
+    if (currentTime <= c.end && currentTime + 0.1 >= c.end) {
+      c.onEnd();
+    }
 
+  }
+};