-
 pageCache    = []
 currentState = null
 initialized  = false
+
 
 visit = (url) ->
   if browserSupportsPushState

    visit link.href
    event.preventDefault()

browserSupportsPushState =
  window.history and window.history.pushState and window.history.replaceState


if browserSupportsPushState

  window.addEventListener 'popstate', (event) ->
    fetchHistory event.state if event.state?.turbolinks



fetchHistory = (state) ->
  cacheCurrentPage()

  if page = pageCache[state.position]
    changePage page.title, page.body.cloneNode(true)
    recallScrollPosition page
  else
    fetchReplacement document.location.href


cacheCurrentPage = ->
  rememberInitialPage()

  pageCache[currentState.position] =
    url:       document.location.href,
    body:      document.body,
    title:     document.title,
    positionY: window.pageYOffset,
    positionX: window.pageXOffset

  constrainPageCacheTo(10)

constrainPageCacheTo = (limit) ->
  delete pageCache[currentState.position - limit] if currentState.position == window.history.length - 1


changePage = (title, body) ->
