/**
 * Upload form widget
 */
App.widgets.FormPendingAction = function () {

  return {

    /**
     * Initialize Upload Form
     */
    init : function (form) {
      if (!form.is('.uploading')) {
        return true;
      } // if

      var button_holder = form.find('.button_holder');
      var button_holder_elements = button_holder.find('*:visible').hide();
      var cancel_submission = $('<button type="button" class="login">' + App.lang('Cancel form submission') + '</button>').appendTo(button_holder);

      var pending_message = App.lang('Waiting for actions to complete');
      if (form.is('.uploading')) {
        pending_message = App.lang('Waiting for uploads to complete');
      } // if

      var pending_message_span = $('<span class="form_pending_action_label">' + pending_message + '</span>').insertAfter(cancel_submission).css('background-image', 'url(' + App.Wireframe.Utils.indicatorUrl('small') + ')');

      /**
       * Cancel waiting to submit
       */
      var cancel_pending = function () {
        cancel_submission.remove();
        pending_message_span.remove();
        button_holder_elements.show();
      }; // cancel_pending

      // check every second if form is ready for submission
      var interval = setInterval(function () {
        if (!form.is('.uploading')) {
          clearInterval(interval);
          cancel_pending();
          form.submit();
        } // if
      }, 1000);

      cancel_submission.click(function () {
        clearInterval(interval);
        cancel_pending();
      });

      return false;
    }

  };

}();