// $Id: ajaxsubmit.js,v 1.10 2007/06/27 15:36:29 nedjo Exp $

/**
 * Attaches the ajaxsubmit behaviour to forms.
 */
Drupal.behaviors.ajaxSubmit = function (context) {
  $('form.ajaxsubmit:not(.ajaxsubmit-processed)').each(function () {
    if (this.ajaxsubmit_target) {
      var target = this.ajaxsubmit_target.value;
    }
    else {
      var target = document.createElement('div');
      $(target).addClass('ajaxsubmit-message');
      $(this).before(target);
    }

    var preview = (this.ajaxsubmit_preview) ? this.ajaxsubmit_preview.value : 0;

    // Set a flag to indicate that the form is using ajaxsubmit.
    if (!this.ajaxsubmit) {
      var ajaxsubmitInput = document.createElement('input');
      $(ajaxsubmitInput)
        .attr('type', 'hidden')
        .attr('name', 'ajaxsubmit')
        .attr('value', '1');
      $(this).append(ajaxsubmitInput);
    }
    $(this).addClass('ajaxsubmit-processed');
    new Drupal.ajaxsubmit(this, target, preview);
  });
};

/**
 * JS ajaxsubmit object.
 */
Drupal.ajaxsubmit = function (form, target, preview) {
  this.target  = target;
  this.preview = preview;
  this.form    = form;
  Drupal.redirectFormSubmit($(form).attr('action'), form, this);
};

/**
 * Handler for the form redirection submission.
 */
Drupal.ajaxsubmit.prototype.onsubmit = function () {
  // Remove any error messages.
  var form = this.form;
  $(form).addClass('submitting');
  GB_throb();
  GB_throb_ajaxsubmit(form);

  for (var i = 0; elt = form.elements[i]; i++) {
    $(elt).removeClass('error');
  }
  $(this.target).html('');
};

/**
 * Handler for the form redirection completion.
 */
Drupal.ajaxsubmit.prototype.oncomplete = function (data) {
  form = this.form;
  $(form).removeClass('submitting');
  GB_throb_off();
  GB_throb_ajaxsubmit_off(form);

  $(this.target).html(data['message']);
  if (data['errors']) {
    for (id in data['errors']) {
      // edit[foo][bar] -> foo-bar
      $('#edit-' + id.replace('][', '-')).addClass('error');
    }
  }
  // Set preview.
  if (data['preview'] && this.preview) {
    $(this.preview).html(data['preview']);
  }

  // Invoke any callback functions.
  if (data.__callbacks) {
    $.each(data.__callbacks, function(i, callback) {
      eval(callback);
    });
  }


  // Redirect.
  if (data['destination']) {
    window.location = Drupal.url(data['destination']);
  }
  //Drupal.scrollTo(this.target);
  Drupal.attachBehaviors(this.target);
};

/**
 * Handler for the form redirection error.
 */
Drupal.ajaxsubmit.prototype.onerror = function (error) {
  // Remove progressbar
  //$(this.progress.element).remove();
  //this.progress = null;
  //IDEAHACK
  $('.throbber-fast', form).removeClass('throbbing');
  GB_throb_off();
  GB_throb_ajaxsubmit_off(form);

  // Go to a designated error page, if any.
  var form = this.form;
  $(this.target).html(form.ajaxsubmit_error_message ? form.ajaxsubmit_error_message.value : 'An error occurred:<br /><br />'+ error);
  if (form.ajaxsubmit_error_redirect) {
    window.location.href = form.ajaxsubmit_error_redirect.value;
  }
};

