 UploadSimilarSearch::thumbnail_complete = (result) ->
   if !result.success
     @results.innerHTML = 'Image load failed.'
-    @results.show()
+    @results.style.display = ''
     return
 
-  ### Grab a data URL from the canvas; this is what we'll send to the server. ###
+  jQuery.ajax '/post/similar.json',
+    data:
+      url: result.canvas.toDataURL()
+    dataType: 'json'
+    method: 'POST'
+  .always =>
+    @results.innerHTML = ''
+    @results.style.display = ''
+  .done (json) =>
+    if json.posts.length > 0
+      posts = []
+      shownPosts = 3
+      makeUrl =
+          if User.get_use_browser()
+            (post) => "/post/browse##{post.id}"
+          else
+            (post) => "/post/show/#{post.id}"
+      posts = json.posts.slice(0, shownPosts).map (post) =>
+        "<a href='#{makeUrl(post)}'>post ##{post.id}</a>"
+      seeAll = "<a href='/post/similar?search_id='#{json.search_id}'>(see all)</a>"
+      html = "Similar posts #{seeAll}: #{posts.join(', ')}"
 
-  data_url = result.canvas.toDataURL()
+      if json.posts.length > shownPosts
+        remainingPosts = json.posts.length - shownPosts
+        html += " (#{remainingPosts} more)"
 
-  ### Create the FormData containing the thumbnail image we're sending. ###
+      message = html
+    else
+      message = 'No similar posts found.'
 
-  form_data = new FormData
-  form_data.append 'url', data_url
-  req = new (Ajax.Request)('/post/similar.json',
-    method: 'post'
-    postBody: form_data
-    contentType: null
-    onComplete: ((resp) ->
-      @results.innerHTML = ''
-      @results.show()
-      json = resp.responseJSON
-      if !json.success
-        @results.innerHTML = json.reason
-        return
-      if json.posts.length > 0
-        posts = []
-        shown_posts = 3
-        json.posts.slice(0, shown_posts).each (post) ->
-          url = undefined
-          if User.get_use_browser()
-            url = '/post/browse#' + post.id
-          else
-            url = '/post/show/' + post.id
-          s = '<a href=\'' + url + '\'>post #' + post.id + '</a>'
-          posts.push s
-          return
-        post_links = posts.join(', ')
-        see_all = '<a href=\'/post/similar?search_id=' + json.search_id + '\'>(see all)</a>'
-        html = 'Similar posts ' + see_all + ': ' + post_links
-        if json.posts.length > shown_posts
-          remaining_posts = json.posts.length - shown_posts
-          html += ' (' + remaining_posts + ' more)'
-        @results.innerHTML = html
-      else
-        @results.innerHTML = 'No similar posts found.'
-      return
-    ).bind(this))
-  return
+    @results.innerHTML = message
+  .fail (xhr) =>
+    @results.innerHTML = xhr.responseJSON?.reason ? 'unknown error'

  if !key
    key = e.keyCode

  ### Opera ###

  if key != Event.KEY_ESC
    return
  @cancel()

  $('post-exists').hide()
  $('post-upload-error').hide()

  # If the files attribute isn't supported, or we have no file (source upload), use regular
  # form submission.
  post_file = $('post_file')
  if !post_file.files? or post_file.files.length == 0
    return
  e.stop()
  @set_progress 0
  @request_starting()
  formData = new FormData(@form_element)

  onprogress = (e) =>
    console.log 'hi'
    done = e.loaded
    total = e.total
    progress = if total > 0 then done / total else 1

    @set_progress progress

  @current_request = jQuery.ajax '/post/create.json',
    contentType: false
    data: formData
    dataType: 'json'
    method: 'POST'
    processData :false
    xhr: =>
      xhr = new XMLHttpRequest
      xhr.upload.addEventListener('progress', onprogress)
      xhr
  .always =>
    @current_request = null
    @request_ending()
  .done (json) =>
    # If a post/similar link was given and similar results exists, go to them.  Otherwise,
    # go to the new post.
    window.location.href =
      if json.similar_location && json.has_similar_hits
        json.similar_location
      else
        json.location
  .fail (xhr) =>
    json = xhr.responseJSON

    if json? && json.location
      a = document.querySelector('#post-exists-link')
      a.text = "post ##{json.post_id}"
      a.href = json.location
      document.querySelector('#post-exists').style.display = ''
      return

    errorLabel = document.querySelector('#post-upload-error')
    errorLabel.text = json?.reason ? 'unknown error'
    errorLabel.style.display = ''

  return

# Cancel the running request, if any.
PostUploadForm::cancel = ->
  if !@current_request?
    return

  # Don't clear this.current_request; it'll be done by the onComplete callback.
  @current_request.abort()
  return

# When file_field is changed to an image, run an image search and put a summary in
# results.
window.UploadSimilarSearch = (file_field, results) ->
  if !ThumbnailUserImage
    return
