-DEBUG = true
-
-log = (args...) ->
-  console.log(args...) if DEBUG
-
 on_repo_page = ->
   $(".repohead").length > 0
 


# Models

class Event
  constructor: (@caption, @name) ->

class FilterSet
  constructor: (@repo_name) ->


class NewsFeedFilter
  inject: ->
    @rebind_events()
    @apply_to(@$alerts())
    @maybe_load_more()

  rebind_events: ->
    @$news().delegate ".ajax_paginate", "click", (event) =>
      event.stopPropagation()
      event.preventDefault()

      link = $(event.target)
      div = link.parent ".ajax_paginate"

      unless div.hasClass "loading"
        div.addClass "loading"

        $.ajax({
          type: "GET"
          url: link.attr "href"

          complete: =>
            div.removeClass "loading"

          success: (data) =>
            new_items = $("<div>").append(data).children()
            @apply_to(new_items)
            div.replaceWith new_items
            @maybe_load_more()
        })

  apply_to: (items) ->
    items.each (i, element) =>
      event_name = element.className.replace /alert\s+/, ""
      if @filter_set(element).is_filtered(event_name)
        #console.log "Filtering: #{event_name} on #{@repo_name(element)}"
        #$(element).css "opacity", 0.2
        $(element).remove()

  maybe_load_more: ->
    if @$alerts().length < 20
      @$news().find(".ajax_paginate a").click()

  $news: ->
    $("#dashboard > .news")

  $alerts: ->
    @$news().find(".alert")

  filter_set: (element) ->
    new FilterSet(@repo_name(element))
