 SubstoryModel = Backbone.Model.extend
   decoratedJSON: ->
-    json = this.toJSON()
+    json = @toJSON()
     json["type"] = {}
-    json["type"][this.get("substory_type")] = true
+    json["type"][@get("substory_type")] = true
     return json
 
 StoryModel = Backbone.Model.extend
   initialize: ->
-    substories = this.get("substories")
+    substories = @get("substories")
 
     substoriesCollection = new SubstoryCollectionClass
-    this.set "substories", substoriesCollection
+    @set "substories", substoriesCollection
 
     that = this
     _.each substories, (x) -> x.user = that.get("user")

  return -moment(story.get("updated_at")).unix()

StoryView = Backbone.View.extend
  initialize: -> this.expanded = false
  template: HandlebarsTemplates["stories/story"]
  
  render: ->
    json = this.model.toJSON()
    json["type"] = {}
    json["type"][this.model.get("story_type")] = true
    
    json["morethantwo"] = json["substories"].length > 2
    json["additional"] = json["substories"].length - 2

      q["type"][s.get("substory_type")] = true
      return q
    
    unless @expanded
      json["substories"] = json["substories"].slice(0, 2)
    
    json["expanded"] = @expanded
    
    @$el.html @template(json)
    
    that = this
    @$el.find("a.show-more").click ->
      that.toggleExpand()
      
  toggleExpand: ->
    @expanded = !@expanded
    @render()
    
StoryCollectionViewClass = Backbone.View.extend
  initialize: ->
    _(this).bindAll 'add', 'remove'
    @views = {}
    @collection.bind 'add', @add
    @collection.bind 'remove', @remove
    @loadedAll = false
    @fetchInProgress = false
  add: (story) ->
    @views[story.cid] = new StoryView
      model: story
  remove: (story) ->
    delete @views[story.cid]
  render: ->
    @$el.empty()
    that = this
    @collection.each (model) ->
      view = that.views[model.cid]
      view.render()
      that.$el.append view.$el
    $(".activity-feed").append @$el
  fetchMore: (baseURL) ->
    unless @loadedAll or @fetchInProgress
      page = 1 + Math.floor(@collection.length / 20)
      @fetchInProgress = true
      that = this
      $.ajax baseURL + "page=" + page,
        dataType: "json"
