   start: ->
     unless @started
       addEventListener("click", @clickCaptured, true)
+      addEventListener("DOMContentLoaded", @pageLoaded, false)
       @history.start()
       @started = true
 
   stop: ->
     if @started
       removeEventListener("click", @clickCaptured, true)
+      removeEventListener("DOMContentLoaded", @pageLoaded, false)
       @history.stop()
       @started = false
 


  loadResponse: (response) ->
    @view.loadHTML(response)

  # Page snapshots



  # Event handlers

  pageLoaded: =>
    @notifyApplicationOfPageChange()

  clickCaptured: =>
    removeEventListener("click", @clickBubbled, false)
    addEventListener("click", @clickBubbled, false)

  clickBubbled: (event) =>
    if not event.defaultPrevented and location = @getVisitableLocationForEvent(event)
      if @applicationAllowsChangingToLocation(location)
        event.preventDefault()
        @visit(location)

  # Events

  applicationAllowsChangingToLocation: (location) ->
    @triggerEvent("page:before-change", data: { url: location }, cancelable: true)

  notifyApplicationOfSnapshotRestoration: ->
    @triggerEvent("page:restore")

  notifyApplicationOfPageChange: ->
    @triggerEvent("page:change")
    @triggerEvent("page:update")

  # Private

  triggerEvent: (eventName, {cancelable, data} = {}) ->
    event = document.createEvent("Events")
    event.initEvent(eventName, true, cancelable is true)
    event.data = data
    document.dispatchEvent(event)
    console.log "dispatched #{eventName} =>", not event.defaultPrevented
    not event.defaultPrevented

  getVisitableLocationForEvent: (event) ->
    link = Turbolinks.closest(event.target, "a")
