/** http://docs.sublimevideo.net/playlists **/
/** Prototype version **/

var SublimeVideo = SublimeVideo || {};

document.observe("dom:loaded", function() {
  // Automatically instantiate all the playlists in the page
  $$('.interactive').each(function(el) { SublimeVideo[el.id] = new InteractiveThumbnailsHandler(el.id); });
});

var InteractiveThumbnailsHandler = Class.create({
  initialize: function(interactiveWrapperId) {
    this.interactiveWrapperId = interactiveWrapperId;
    this.videosCount = $$("#" + this.interactiveWrapperId + " .video_wrap").size();

    var matches = $$("#" + this.interactiveWrapperId + " video")[0].id.match(/^video(\d+)$/);
    this.firstVideoIndex = parseInt(matches[1], 10);

    this.setupObservers();

    this.loadDemo();
  },
  setupObservers: function() {
    $$("#" + this.interactiveWrapperId + " li").each(function(thumb) {
      thumb.on("click", function(event) {
        event.stop();

        if (!thumb.hasClassName("active")) {
          this.clickOnThumbnail(thumb.readAttribute("id"));
        }
      }.bind(this));
    }.bind(this));
  },
  loadDemo: function() {
    // Only if not the first time here
    if (this.activeVideoId) this.reset();

    this.activeVideoId = "video" + this.firstVideoIndex;

    // Show first video
    this.showActiveVideo();
  },
  reset: function() {
    // Hide the current active video
    $$("#" + this.interactiveWrapperId + " .video_wrap.active").first().removeClassName("active");

    // Get current active video and unprepare it
    // we could have called sublimevideo.unprepare() without any arguments, but this is faster
    sublimevideo.unprepare(this.activeVideoId);

    // Deselect its thumbnail
    this.deselectThumbnail(this.activeVideoId);
  },
  clickOnThumbnail: function(thumbnailId) {
    // Basically undo all the stuff and bring it back to the point before js kicked in
    this.reset();

    // Set the new active video
    this.activeVideoId = thumbnailId.replace(/^thumbnail_/, "");

    // And show the video
    this.showActiveVideo();

    // And finally, prepare and play it
    sublimevideo.prepareAndPlay(this.activeVideoId);
  },
  selectThumbnail: function(videoId) {
    $("thumbnail_" + videoId).addClassName("active");
  },
  deselectThumbnail: function(videoId) {
    $("thumbnail_" + videoId).removeClassName("active");
  },
  showActiveVideo: function() {
    // Select its thumbnail
    this.selectThumbnail(this.activeVideoId);

    // Show it
    $(this.activeVideoId).up().addClassName("active");
  },
  handleAutoNext: function(endedVideoId) {
    var nextId = parseInt(endedVideoId.replace(/^video/, ""), 10) + 1;
    if (nextId > 1 && nextId < this.firstVideoIndex + this.videosCount) {
      this.clickOnThumbnail("thumbnail_video" + nextId);
    }
  }
});

sublimevideo.ready(function() {
  sublimevideo.onEnd(function(sv) {
    var matches = sv.element.id.match(/^video(\d+)$/);
    if (matches != undefined) {
      var playlistId = sv.element.up('.interactive').id;
      if (parseInt(matches[1], 10) <= SublimeVideo[playlistId].firstVideoIndex + SublimeVideo[playlistId].videosCount) {
        SublimeVideo[playlistId].handleAutoNext(sv.element.id);
      }
    }
  });
});
