(function() {
  var VideoProgressController, embedVideo;
  embedVideo = function(previewURL, videoURL, selector, media_url) {
    var attributes, flashvars, params;
    flashvars = {
      image: previewURL,
      movie: videoURL,
      autoplay: "false",
      loop: "false",
      autohide: "false",
      fullscreen: "true",
      color_text: "0xFFFFFF",
      color_seekbar: "0xD0314F",
      color_loadingbar: "0x828282",
      color_seekbarbg: "0x333333",
      color_button_out: "0x333333",
      color_button_over: "0x000000",
      color_button_highlight: "0xffffff"
    };
    params = {
      allowfullscreen: "true",
      allowscriptaccess: "always",
      bgcolor: "#000000",
      wmode: "transparent"
    };
    attributes = {
      align: "middle"
    };
    videoURL = "v2/swf/mario.flv";
    return swfobject.embedSWF(media_url + "swf/player.swf", selector, "330", "240", "9.0.45", "", flashvars, params, attributes);
  };
  VideoProgressController = (function() {
    function VideoProgressController(el, threshold, callback, verbose) {
      this.el = el;
      this.callback = callback;
      this.verbose = verbose;
      if (typeof threshold === "string" && threshold.indexOf("%") > -1) {
        this.thresholdIsAbsolute = false;
        this.threshold = parseInt(threshold.substring(0, threshold.length - 1));
      } else {
        this.thresholdIsAbsolute = true;
        this.threshold = parseInt(threshold);
      }
      if (verbose) {
        console.log("@threshold ", this.thresholdIsAbsolute, this.treshold);
      }
      this.secondsWatched = 0;
    }
    VideoProgressController.prototype.start = function() {
      var self;
      self = this;
      this.lastTimeChecked = 0;
      this.intervalID = setInterval(function() {
        return self.onTimeTicked();
      }, 1010);
      this.timeElapsed = 0;
      if (this.verbose) {
        return console.log("starting");
      }
    };
    VideoProgressController.prototype.pause = function() {
      clearInterval(this.intervalID);
      if (this.verbose) {
        return console.log("stopping");
      }
    };
    VideoProgressController.prototype.stop = function() {
      this.pause();
      this.timeElapsed = 0;
      if (this.verbose) {
        return console.log("stop");
      }
    };
    VideoProgressController.prototype.onTimeTicked = function() {
      var timeData, videoTime;
      timeData = this.el.getVideoTimeData();
      if (!timeData.playing) {
        return;
      }
      videoTime = timeData != null ? timeData.currentTime : void 0;
      if (!videoTime || videoTime === 0) {
        return;
      }
      if (this.thresholdIsAbsolute) {
        videoTime = timeData.currentTime;
      } else {
        this.secondsWatched += 1;
        videoTime = this.secondsWatched;
      }
      if (this.verbose) {
        console.log("onTimeTicked videoTime: " + videoTime + ", last checked " + this.lastTimeChecked + ", watched " + this.secondsWatched);
      }
      this.lastTimeChecked = videoTime;
      if (this.areWeDone(videoTime, timeData.duration)) {
        return this.onTriggered();
      }
    };
    VideoProgressController.prototype.onTriggered = function() {
      if (this.verbose) {
        console.log("should trigger!");
      }
      this.stop();
      return this.callback();
    };
    VideoProgressController.prototype.areWeDone = function(t, duration) {
      var percentDone;
      if (this.thresholdIsAbsolute === true) {
        return t >= this.threshold;
      } else {
        percentDone = Math.ceil(100 * (t / duration));
        return duration > 0 && percentDone > this.threshold;
      }
    };
    return VideoProgressController;
  })();
  window.onVideoPlayerInited = function() {
    var controller, debug;
    debug = window.location.search && window.location.search.substring(1).split("=")[0] === "debug";
    controller = new VideoProgressController($("#video-player").eq(0)[0], "50%", function() {
      return window["enableRating"]();
    }, debug);
    controller.start();
    return window.x = controller;
  };
  window.embedVideo = embedVideo;
}).call(this);

