////
// Callback receives responseText and 'success' / 'error'
// based on response.
//
// settings hash:
//   facebox: true        // a facebox 'loading' will open pre-submit
//   confirmation: string // a confirm pop-up will open with the supplied string
//
$.fn.spamjax = function(callback, settings) {
  var settings = settings || {}
  var options  = {}

  options.complete = function(xhr, ok) { callback.call(this, xhr.responseText, ok) }

  if (settings.confirmation) {
    options.beforeSubmit = function() {
      var execute = confirm(settings.confirmation)
      if (!execute) return false
      if (settings.facebox) $.facebox.loading()
    }
  } else if (settings.facebox) { 
    options.beforeSubmit = $.facebox.loading
  }

  // TODO: test this, yo
  $(this).ajaxForm($.extend(settings, options))
  return this
}

////
// Behaviors
$(document).ready(function() {
  ////
  // new conversation scroller
  $('.jump_to_new_conversation').click(function() {
    $('#new-conversation').scrollTo().queue(function() {
      $('#message_subject').focus()
    })
    return false
  })

  // lightbox activation
  if ($.facebox) 
    $('a[rel*=facebox]').facebox({
      next_image    : 'images/icons/small/fast_forward.png',
      play_image    : 'images/icons/small/play.png',
      pause_image   : 'images/icons/small/pause.png',
      prev_image    : 'images/icons/small/rewind.png'
    }) 
});

////
// add Accept:text/javascript header to jQuery ajax requests
$.ajaxSetup({ 'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} })
