window.displayFunc = (column, asc) ->
  $th = $("th[filter='#{column}']")
  if $th.hasClass('date')
    asc_sort = (a,b) -> if moment(a[column], "MM/DD/YY HH:mm:ss").isAfter(moment(b[column], "MM/DD/YY HH:mm:ss")) then -1 else (if moment(a[column], "MM/DD/YY HH:mm:ss").isSame(moment(b[column], "MM/DD/YY HH:mm:ss")) then 0 else 1)
    desc_sort = (a,b) -> asc_sort(b,a)
  else
    asc_sort = (a,b) -> if a[column] > b[column] then -1 else (if a[column] == b[column] then 0 else 1)
    desc_sort = (a,b) -> asc_sort(b,a)
  accounts.sort(if asc then asc_sort else desc_sort)
  total_html = ""
  for account, index in accounts
    total_html += """
            <tr class='page-#{ Math.floor(index / 50) } main'>
                <td><input name="export[]" value="#{ account.id }" type="checkbox" /></td>
                <td>#{ account.created_at }</td>
                <td>#{ account.email }</td>
                <td>#{ account.first_name }</td>
                <td>#{ account.last_name }</td>
                <td>#{ account.phone }</td>
            </tr>
            <tr class='page-#{ Math.floor(index / 50) } more'>
              <td class='message'><div class='message'>#{ account.message }</div></td>
            </tr>"""
  return total_html


window.renderFunc = () ->
  $('tr.main').click((evt) ->
    if ($(evt.srcElement).prop("tagName") == "INPUT")
      return
    next = $(this).next('tr.more')[0]
    if (next.style.display == 'none')
      $(this).next('tr.more').show()
    else
      $(this).next('tr.more').hide()
      $('table').removeClass('table-bordered')
      setTimeout( ->
        $('table').addClass('table-bordered')
      , 10)
  )
