     if hits.length > 0
       page.story.push heading, hits...
       page.story[0].text = 'We could not find this page in the expected context.'
-    wiki.buildPage( page, undefined, pageElement ).addClass('ghost')
+
+    wiki.buildPage( page, undefined, $pageElement ).addClass('ghost')
 
   registerNeighbors = (data, site) ->
     if _.include ['local', 'origin', 'view', null, undefined], site

      favicon_src: "/favicon.png"
      title: page.title

  $(pageElement).append( pageHeader )
  
  unless loading_a_remote_page
    $('img.favicon',pageElement).error (e)->
      plugin.get 'favicon', (favicon) ->
        favicon.create()

  if (rev = pageElement.attr('id').split('_rev')[1])?
    date = page.journal[page.journal.length-1].date
    $(pageElement).addClass('ghost').data('rev',rev).append $ """
      <h2 class="revision">
        <span>
          #{if date? then util.formatDate(date) else "Revision #{rev}"}
        </span>
      </h2>
    """

wiki.buildPage = (data,siteFound,pageElement) ->

  if siteFound == 'local'
    pageElement.addClass('local')
  else
    pageElement.data('site', siteFound)

  if not data?
    pageElement.find('.item').each (i, each) ->
      item = wiki.getItem($(each))
      plugin.get item.type, (plugin) ->
        plugin.bind $(each), item
  else
    page = $.extend(util.emptyPage(), data)
    $(pageElement).data("data", page)
    slug = $(pageElement).attr('id')
    site = $(pageElement).data('site')

    context = ['view']
    context.push site if site?
    addContext = (site) -> context.push site if site? and not _.include context, site
    addContext action.site for action in page.journal.slice(0).reverse()
    wiki.resolutionContext = context

    wiki.log 'buildPage', slug, 'site', site, 'context', context.join ' => '
    emitHeader pageElement, page

    [storyElement, journalElement, footerElement] = ['story', 'journal', 'footer'].map (className) ->
      $("<div />").addClass(className).appendTo(pageElement)

    doItem = (i) ->
      return if i >= page.story.length
      item = page.story[i]
      item = item[0] if $.isArray item # unwrap accidentally wrapped items
      div = $("<div />").addClass("item").addClass(item.type).attr("data-id", item.id)
      storyElement.append div
      plugin.do div, item, -> doItem i+1
    doItem 0

    $.each page.journal, (i, action) ->
      wiki.addToJournal journalElement, action

    journalElement.append """
      <div class="control-buttons">
        <a href="#" class="button fork-page" title="fork this page">#{util.symbols['fork']}</a>
        <a href="#" class="button add-factory" title="add paragraph">#{util.symbols['add']}</a>
      </div>
                          """
    footerElement
      .append('<a id="license" href="http://creativecommons.org/licenses/by-sa/3.0/">CC BY-SA 3.0</a> . ')
      .append("<a class=\"show-page-source\" href=\"/#{slug}.json?random=#{util.randomBytes(4)}\" title=\"source\">JSON</a> . ")
      .append("<a>#{siteFound || 'origin'}</a>")

    state.setUrl()

  initDragging pageElement
  initAddButton pageElement
  pageElement


module.exports = refresh = wiki.refresh = ->
  pageElement = $(this)

  [slug, rev] = pageElement.attr('id').split('_rev')
  pageInformation = {
    slug: slug
    rev: rev
    site: pageElement.data('site')
    wasServerGenerated: pageElement.attr('data-server-generated') == 'true'
  }

  # No longer used, now that we create ghost pages for unknown pages
  #createPage = ->
    #title = $("""a[href="/#{slug}.html"]:last""").text()
    #title or= slug
    #pageHandler.put $(pageElement), {type: 'create', id: util.randomBytes(8), item: {title}}
    #wiki.buildPage( {title}, undefined, pageElement )

  createGhostPage = ->
    title = $("""a[href="/#{slug}.html"]:last""").text() or slug

      neighborhood.registerNeighbor action.site if action.site?

  whenGotten = (data,siteFound) ->
    wiki.buildPage( data, siteFound, $pageElement )
    registerNeighbors( data, siteFound )

  pageHandler.get
