   generate: =>
     pages = []
 
-    if @total_pages > 1
+    if @totalPages > 1
       pages = for i in @windowedPageNumbers()
-        if i is @current_page
+        if i is @currentPage
           "<li class=\"current\">#{Fustrate.linkTo(i, '#')}</li>"
         else if i is 'gap'
           '<li class="unavailable"><span class="gap">…</span></li>'

    $('<ul class="pagination">').html pages.join(' ')

  windowedPageNumbers: =>
    window_from = @current_page - 4
    window_to = @current_page + 4

    if window_to > @total_pages
      window_from -= window_to - @total_pages
      window_to = @total_pages

    if window_from < 1
      window_to += 1 - window_from
      window_from = 1
      window_to = @total_pages if window_to > @total_pages

    middle = [window_from..window_to]

    left = if 4 < middle[0] then [1, 2, 'gap'] else [1...middle[0]]

    if @total_pages - 3 > middle.last()
      right = [(@total_pages - 1)..@total_pages]
      right.unshift 'gap'
    else if middle.last() + 1 <= @total_pages
      right = [(middle.last() + 1)..@total_pages]
    else
      right = []


class Fustrate.Components.Pagination extends Fustrate.Components.Base
  constructor: ({ @currentPage, @totalPages, @totalEntries, @perPage }) ->
    @base = @constructor._getPreppedPaginationURL()

  link: (text, page, options = {}) =>
    Fustrate.linkTo(text, "#{@base}page=#{page}", options)

  previousLink: =>
    if @currentPage > 1
      return "
        <li class=\"previous_page\">
          #{@link('← Previous', @currentPage - 1, rel: 'prev')}
        </li>"

    '<li class="previous_page unavailable"><a href="#">← Previous</a></li>'

  nextLink: =>
    if @currentPage < @totalPages
      return "
        <li class=\"next_page\">
          #{@link('Next →', @currentPage + 1, rel: 'next')}
        </li>"

    '<li class="next_page unavailable"><a href="#">Next →</a></li>'
