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

  ClientUI = (function() {

    function ClientUI() {
      this.root = $('#root');
      this.goDirectoryList();
    }

    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.goBrowse = function(path) {
      var _this = this;
      return this.go("browse/" + this.currentDirectoryId + "/" + path, function() {
        _this.root.find('#go-back').click(function() {
          return _this.goDirectoryList();
        });
        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);
          });
        }
        return _this.root.find('a.browse-item').each(function(i, e) {
          return $(e).click(function() {
            path = path + '/' + $(e).attr('data-id');
            return _this.goBrowse(path);
          });
        });
      });
    };

    return ClientUI;

  })();

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

}).call(this);
