(function() {
  var Lowbrow;
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.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;
  };
  Lowbrow = {};
  Lowbrow.URL = 'http://thebrow.naturalzesty.com/moments';
  Lowbrow.Moment = (function() {
    __extends(Moment, Backbone.Model);
    function Moment() {
      this.url = __bind(this.url, this);
      Moment.__super__.constructor.apply(this, arguments);
    }
    Moment.prototype.url = function() {
      return Lowbrow.URL + '/' + this.id;
    };
    return Moment;
  })();
  Lowbrow.MomentList = (function() {
    __extends(MomentList, Backbone.Collection);
    function MomentList() {
      MomentList.__super__.constructor.apply(this, arguments);
    }
    MomentList.prototype.model = Lowbrow.Moment;
    MomentList.prototype.url = Lowbrow.URL;
    return MomentList;
  })();
  Lowbrow.SavedMoments = (function() {
    __extends(SavedMoments, Backbone.Collection);
    function SavedMoments() {
      this.initialize = __bind(this.initialize, this);
      SavedMoments.__super__.constructor.apply(this, arguments);
    }
    SavedMoments.prototype.model = Lowbrow.Moment;
    SavedMoments.prototype.localStorage = new Store('saved');
    SavedMoments.prototype.initialize = function() {
      return this.sync = Backbone.Local.sync;
    };
    return SavedMoments;
  })();
  Lowbrow.MomentView = (function() {
    __extends(MomentView, Backbone.View);
    function MomentView() {
      this.render = __bind(this.render, this);
      MomentView.__super__.constructor.apply(this, arguments);
    }
    MomentView.prototype.tagName = 'li';
    MomentView.prototype.template = _.template($('#moment-view').html());
    MomentView.prototype.render = function() {
      $(this.el).html(this.template({
        id: this.model.id,
        quote: this.parseBody(this.model.get('quote'))
      }));
      return this;
    };
    MomentView.prototype.parseBody = function(body) {
      return body.replace(/(\r\n)+|(\r)+|(\n)+/g, '<br/><br/>');
    };
    return MomentView;
  })();
  Lowbrow.MomentListView = (function() {
    __extends(MomentListView, Backbone.View);
    function MomentListView() {
      this.clear = __bind(this.clear, this);
      this.all = __bind(this.all, this);
      this.add = __bind(this.add, this);
      MomentListView.__super__.constructor.apply(this, arguments);
    }
    MomentListView.prototype.initialize = function(list) {
      var _ref;
      _ref = ['#moments', list], this.el = _ref[0], this.collection = _ref[1];
      this.clear();
      this.collection.bind('add', this.add, this);
      return this.collection.bind('reset', this.all, this);
    };
    MomentListView.prototype.add = function(moment) {
      var v;
      v = new Lowbrow.MomentView({
        model: moment
      });
      return $(this.el).append(v.render().el);
    };
    MomentListView.prototype.all = function() {
      return this.collection.each(this.add);
    };
    MomentListView.prototype.clear = function() {
      return $(this.el).html('');
    };
    return MomentListView;
  })();
  Lowbrow.AppView = (function() {
    __extends(AppView, Backbone.View);
    function AppView() {
      this._clickRefresh = __bind(this._clickRefresh, this);
      this._clickMore = __bind(this._clickMore, this);
      this.reset = __bind(this.reset, this);
      this.refresh = __bind(this.refresh, this);
      this.render = __bind(this.render, this);
      AppView.__super__.constructor.apply(this, arguments);
    }
    AppView.prototype.el = 'body';
    AppView.prototype.ui = {
      more: '.more',
      refresh: '.refresh'
    };
    AppView.prototype.events = {
      'click .refresh': '_clickRefresh',
      'click .more': '_clickMore'
    };
    AppView.prototype.template = _.template($('#app-view').html());
    AppView.prototype.initialize = function(opts) {
      var _ref, _ref2;
      this.collection = (_ref = opts.collection) != null ? _ref : {};
      return this.listView = (_ref2 = opts.listView) != null ? _ref2 : new Lowbrow.MomentListView(this.collection);
    };
    AppView.prototype.render = function() {
      return $(this.el).append(this.template());
    };
    AppView.prototype.refresh = function() {
      this.reset();
      return this.collection.fetch();
    };
    AppView.prototype.reset = function() {
      return this.listView.clear();
    };
    AppView.prototype._clickMore = function() {
      $(this.ui.more).hide();
      $(this.el).append('<div id="loading"></div>');
      return this.collection.fetch({
        success: __bind(function() {
          $('#loading').remove();
          return $(this.ui.more).show();
        }, this)
      });
    };
    AppView.prototype._clickRefresh = function() {
      router.navigate('#random', true);
      if (document.location.hash === '#random') {
        return this.refresh();
      }
    };
    return AppView;
  })();
  Lowbrow.Router = (function() {
    __extends(Router, Backbone.Router);
    function Router() {
      this.moment = __bind(this.moment, this);
      this.home = __bind(this.home, this);
      Router.__super__.constructor.apply(this, arguments);
    }
    Router.prototype.routes = {
      '': 'home',
      'random': 'home',
      'moments/:id': 'moment'
    };
    Router.prototype.initialize = function() {
      this.moments = new Lowbrow.MomentList();
      this.saved = new Lowbrow.SavedMoments();
      this.app = new Lowbrow.AppView({
        collection: this.moments
      });
      return this.app.render();
    };
    Router.prototype.home = function() {
      return this.app.refresh();
    };
    Router.prototype.moment = function(id) {
      var m;
      this.app.reset();
      m = new Lowbrow.Moment({
        id: id
      });
      return m.fetch({
        success: __bind(function() {
          return this.moments.add(m);
        }, this)
      });
    };
    return Router;
  })();
  window.router = new Lowbrow.Router();
  Backbone.history.start();
}).call(this);

