render = () ->
  $('tr').hide()
  $('tr.page-header').show()
  $('tr.page-0.main').show()
  window.renderFunc()
  window.currentPage = 0
  window.maxPages = accounts.length - 1
  if (window.maxPages <= 1)
    $("#pageNext").hide()
  $("#pagePrev").hide();


displayTable = (column, asc) ->
  $("tbody").html(window.displayFunc(column, asc))
  render()


$(document).ready ->


  $('#pageNext').click(() ->
    $('tr.page-' + window.currentPage).hide()
    window.currentPage++
    $('tr.page-' + window.currentPage).show()
    $("#pagePrev").show()
    if (window.currentPage == window.maxPages)
      $("#pageNext").hide()
  )
  $('#pagePrev').click( () ->
    $('tr.page-' + window.currentPage).hide()
    window.currentPage--
    $('tr.page-' + window.currentPage).show()
    $("#pageNext").show()
  )
  $("#export").click(() -> $("form").submit())

  displayTable('id',true)
  $(".tableOptions a.select").click ->
    if !$(this).hasClass("active")
      $("table td input[type='checkbox']").prop("checked", true)
    else
      $("table td input[type='checkbox']").prop("checked", false)
    $(this).toggleClass("active")

  $("td.date").each ->
    date = moment($(this).text(),"MMM. D, YYYY, h:mm a")
    $(this).text(date.format("M-D-YY"))
    $(this).next("td.days").text(date.fromNow())

  $('thead tr th').click ->
    if !$(this).hasClass('ascending') and !$(this).hasClass('descending')
      $('thead tr th').removeClass('ascending').removeClass('descending')
      $(this).addClass('ascending')
      displayTable($(this).attr('filter'), true)
    else if $(this).hasClass('ascending')
      $('thead tr th').removeClass('ascending').removeClass('descending')
      $(this).addClass('descending')
      displayTable($(this).attr('filter'), false)
    else
      $('thead tr th').removeClass('ascending').removeClass('descending')
      displayTable('id', true)

