Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 3.77 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /** http://docs.sublimevideo.net/playlists **/
  2. /** Prototype version **/
  3.  
  4. var SublimeVideo = SublimeVideo || { playlists: {} };
  5.  
  6. document.observe("dom:loaded", function() {
  7.   // Automatically instantiate all the playlists in the page
  8.   $$('.interactive').each(function(el) { SublimeVideo.playlists[el.id] = new SublimeVideoPlaylist(el.id); });
  9. });
  10.  
  11. var SublimeVideoPlaylist = Class.create({
  12.   initialize: function(playlistWrapperId) {
  13.     if (!$(playlistWrapperId)) return;
  14.  
  15.     this.playlistWrapperId = playlistWrapperId;
  16.     this.videosCount = $$("#" + this.playlistWrapperId + " .video_wrap").size();
  17.  
  18.     var matches = $$("#" + this.playlistWrapperId + " video")[0].id.match(/^video([0-9]+)$/);
  19.     this.firstVideoIndex = parseInt(matches[1], 10);
  20.     this.lastVideoIndex = this.firstVideoIndex + this.videosCount - 1;
  21.  
  22.     this.setupObservers();
  23.  
  24.     this.loadDemo();
  25.   },
  26.   setupObservers: function() {
  27.     $$("#" + this.playlistWrapperId + " li").each(function(thumb) {
  28.       thumb.on("click", function(event) {
  29.         event.stop();
  30.  
  31.         if (!thumb.hasClassName("active")) {
  32.           this.clickOnThumbnail(thumb.readAttribute("id"));
  33.         }
  34.       }.bind(this));
  35.     }.bind(this));
  36.   },
  37.   loadDemo: function() {
  38.     // Only if not the first time here
  39.     if (this.activeVideoId) this.reset();
  40.  
  41.     this.activeVideoId = "video" + this.firstVideoIndex;
  42.  
  43.     // Show first video
  44.     this.showActiveVideo();
  45.   },
  46.   reset: function() {
  47.     // Hide the current active video
  48.     $$("#" + this.playlistWrapperId + " .video_wrap.active").first().removeClassName("active");
  49.  
  50.     // Get current active video and unprepare it
  51.     // we could have called sublimevideo.unprepare() without any arguments, but this is faster
  52.     sublimevideo.unprepare(this.activeVideoId);
  53.  
  54.     // Deselect its thumbnail
  55.     this.deselectThumbnail(this.activeVideoId);
  56.   },
  57.   clickOnThumbnail: function(thumbnailId) {
  58.     // Basically undo all the stuff and bring it back to the point before js kicked in
  59.     this.reset();
  60.  
  61.     // Set the new active video
  62.     this.activeVideoId = thumbnailId.replace(/^thumbnail_/, "");
  63.  
  64.     // And show the video
  65.     this.showActiveVideo();
  66.  
  67.     // And finally, prepare and play it
  68.     sublimevideo.prepareAndPlay(this.activeVideoId);
  69.   },
  70.   selectThumbnail: function(videoId) {
  71.     $("thumbnail_" + videoId).addClassName("active");
  72.   },
  73.   deselectThumbnail: function(videoId) {
  74.     $("thumbnail_" + videoId).removeClassName("active");
  75.   },
  76.   showActiveVideo: function() {
  77.     // Select its thumbnail
  78.     this.selectThumbnail(this.activeVideoId);
  79.  
  80.     // Show it
  81.     $(this.activeVideoId).up().addClassName("active");
  82.   },
  83.   handleAutoNext: function(endedVideoId) {
  84.     var nextId = parseInt(endedVideoId.replace(/^video/, ""), 10) + 1;
  85.     if (nextId > 1 && nextId <= this.lastVideoIndex) {
  86.       this.clickOnThumbnail("thumbnail_video" + nextId);
  87.     }
  88.   }
  89. });
  90.  
  91. sublimevideo.ready(function() {
  92.   // To autoplay a playlist:
  93.   // - remove the "sublime" class of the first video in the playlist to autoplay
  94.   // - uncomment the following line (you can replace "$$('.playlist')[0].id" by the id of the playlist to autoplay)
  95.   // sublimevideo.prepareAndPlay(SublimeVideo.playlists[$$('.playlist')[0].id].activeVideoId);
  96.  
  97.   sublimevideo.onEnd(function(sv) {
  98.     var matches = sv.element.id.match(/^video([0-9]+)$/);
  99.     if (matches != undefined) {
  100.       var playlistId = sv.element.up('.interactive').id;
  101.       if (parseInt(matches[1], 10) < SublimeVideo.playlists[playlistId].lastVideoIndex) {
  102.         SublimeVideo.playlists[playlistId].handleAutoNext(sv.element.id);
  103.       }
  104.       // Uncomment the following 4 lines if you want the whole playlist to loop
  105.       // else {
  106.       //   SublimeVideo.playlists[playlistId].loadDemo();
  107.       //   sublimevideo.prepareAndPlay(SublimeVideo.playlists[playlistId].activeVideoId);
  108.       // }
  109.     }
  110.   });
  111. });