// Generated by CoffeeScript 1.4.0
(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  window.Controls.video = (function(_super) {

    __extends(video, _super);

    function video() {
      return video.__super__.constructor.apply(this, arguments);
    }

    video.prototype.createDom = function() {
      var timestamp,
        _this = this;
      this.dom = $("<div class=\"control video\">\n    <video controls>\n        <source src=\"" + (this.properties.path ? "/elements/video/" + this.properties.path : '') + "\" type=\"video/mp4\">\n    </video>\n    <div class=\"timestamp\">\n        00:00:00.000\n    </div>\n    <div>\n        <a class=\"control button style-normal\"><i class=\"icon-cut\"></i> Cut</a>\n    </div>\n</div>");
      timestamp = this.dom.find('.timestamp');
      this.video = this.dom.find('video')[0];
      this.video.addEventListener('canplay', function() {
        if (!_this.video) {
          return;
        }
        _this.video.pause();
        if (_this.properties.position) {
          _this.video.currentTime = _this.properties.position;
        }
        if (_this.properties.autoplay) {
          return _this.video.play();
        }
      });
      this.video.addEventListener('timeupdate', function() {
        var h, m, ms, s, ts;
        if (!_this.video) {
          return;
        }
        ts = _this.video.currentTime;
        _this.lastTimestamp = ts;
        h = Math.floor(ts / 3600);
        m = Math.floor(ts / 60 % 60);
        s = Math.floor(ts % 60);
        ms = Math.floor(ts * 1000 % 1000);
        ts = h + ':' + (m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s + '.' + (ms < 100 ? '0' : '') + (ms < 10 ? '0' : '') + ms;
        timestamp.text(ts);
        return _this.lastTimestampText = ts;
      });
      return this.dom.find('a').click(function() {
        return _this.event('cut', {
          time: _this.lastTimestamp,
          time_text: _this.lastTimestampText
        });
      });
    };

    video.prototype.detectUpdates = function() {
      var r;
      r = {};
      if (this.video.paused === this.properties.autoplay) {
        r.autoplay = !this.video.paused;
      }
      if (this.video.currentTime !== this.properties.position) {
        r.position = this.video.currentTime;
      }
      return r;
    };

    video.prototype.onBroadcast = function(msg) {
      if (msg === 'destruct') {
        this.video.pause();
        delete this.video;
        return $(this.dom).empty();
      }
    };

    return video;

  })(window.Control);

}).call(this);
