// Generated by CoffeeScript 1.4.0
(function() {
  var ClientUI;

  ClientUI = (function() {

    function ClientUI() {
      this.root = $('#root');
      this.goDirectoryList();
      this.currentDirectoryId = 1;
      this.goBrowse('/home/eugeny/source/P2-dvcpro25/CONTENTS/VIDEO/000000.MXF ');
    }

    ClientUI.prototype.replace_view = function(html) {
      return this.root.html(html);
    };

    ClientUI.prototype.go = function(url, callback) {
      var _this = this;
      return $.get('/media/ajax/' + url, function(html) {
        _this.replace_view(html);
        return callback();
      });
    };

    ClientUI.prototype.goDirectory = function(id) {
      var _this = this;
      this.currentDirectoryId = id;
      this.goBrowse('/');
      return;
      return this.go("directory/" + id, function() {
        _this.root.find('#go-back').click(function() {
          return _this.goDirectoryList();
        });
        return _this.root.find('#go-browse').click(function() {
          return _this.goBrowse('/');
        });
      });
    };

    ClientUI.prototype.goDirectoryList = function() {
      var _this = this;
      return this.go('directory-list', function() {
        return _this.root.find('a').each(function(i, e) {
          return $(e).click(function() {
            return _this.goDirectory($(e).attr('data-id'));
          });
        });
      });
    };

    ClientUI.prototype.reloadComments = function() {
      var _this = this;
      return $.get("/media/ajax/browse/" + this.currentDirectoryId + "/" + this.currentPath + "?comments", function(html) {
        return $('#comments').html(html);
      });
    };

    ClientUI.prototype.goBrowse = function(path) {
      var _this = this;
      this.currentPath = path;
      return this.go("browse/" + this.currentDirectoryId + "/" + path, function() {
        var video;
        if (path === '/') {
          _this.root.find('#go-up').hide();
          path = '';
        } else {
          _this.root.find('#go-up').click(function() {
            var components;
            components = path.split('/');
            components = components.splice(0, components.length - 1);
            path = components.join('/');
            return _this.goBrowse(path);
          });
        }
        _this.root.find('a.browse-item').each(function(i, e) {
          return $(e).click(function() {
            path = path + '/' + $(e).attr('data-id');
            return _this.goBrowse(path);
          });
        });
        if ($('video')) {
          video = $('video')[0];
          video.addEventListener('timeupdate', function() {
            var h, m, ms, s, ts;
            ts = video.currentTime;
            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;
            return $('.timestamp').text(ts);
          });
          $('a[data-time]').click(function() {
            var t;
            t = parseFloat($(this).attr('data-time'));
            return video.currentTime = t;
          });
          _this.reloadComments();
          return $('.comment-form a').click(function() {
            var text;
            text = $('.comment-form input').val();
            $('.comment-form input').val('');
            return $.post("/media/ajax/browse/" + _this.currentDirectoryId + "/" + path, {
              comment: text,
              time: video.currentTime
            }, function(e) {
              _this.reloadComments();
              return e.preventDefault();
            });
          });
        }
      });
    };

    return ClientUI;

  })();

  $(function() {
    return new ClientUI();
  });

}).call(this);
