   document.documentElement.appendChild body, originalBody
   document.documentElement.removeChild originalBody
   document.title = title
+  historyCache[window.history.state.position] = {url:document.location.href,body:body,title:title} if cache
+
 
 
 extractLink = (event) ->

@historyCache = []

visit = (url) ->
  if browserSupportsPushState? and document.location.href != url
    @historyCache.push({url:document.location.href,body:document.body,title:document.title})
    reflectNewUrl url
    fetchReplacement url
  else

  xhr.onabort = -> console.log("Aborted turbolink fetch!")
  xhr.send()

fetchHistory = (state) ->
  cache = historyCache[state.position]
  if cache
    console.log(state.position)
    console.log(cache)
    replaceDocument cache.body, cache.title
    triggerPageChange
  else
    fetchReplacement document.location.href

fullReplacement = (html, url) ->
  replaceHTML html
  triggerPageChange()

reflectNewUrl = (url) ->
  window.history.pushState { turbolinks: true,position: window.history.length }, "", url



triggerPageChange = ->
  event = document.createEvent 'Events'
