/* MooWaiter 1.0 by Andy Chentsov
 * Created on: 6.02.2008
 *
 * This simple piece of code automates the creating of Ajax loading symbols.
 * The loading symbol covers an HTML element with correct position and size - example:
 * $('myElement').startWaiting() and $('myElement').stopWaiting()
 *
 * Ported for Mootools from Protoload by Andreas Kalsch http://aka-fotos.de/MooWaiter/ All credit goes to him
 */
MooWaiter = {
  // the script to wait this amount of msecs until it shows the loading element
  timeUntilShow: 250,

  // opacity of loading element
  opacity: 0.8,

  // Start waiting status - show loading element
  startWaiting: function(className, timeUntilShow, opacity) {
    element = this;
    className = className || 'waiting';
    timeUntilShow = timeUntilShow || MooWaiter.timeUntilShow;
    opacity = opacity || MooWaiter.opacity;
    element._waiting = true;
    if (!element._loading) {
      var e = element._loading = new Element('div', {
      	'styles': {
      		'position': 'absolute',
      		'opacity': opacity
      	}
      }).injectInside(/*element.offsetParent*/$(element).getParent() || document.body);
    }
    element._loading.className = className;
    (function() {
      if (this._waiting) {
        $(this._loading).setStyles({
          'left': this.getLeft(),
          'top': this.getTop(),
          'width': this.getSize().size.x,
          'height': this.getSize().size.y
        });
      }
    }).bind(element).delay(timeUntilShow);
    return this;
  },

  // Stop waiting status - hide loading element
  stopWaiting: function() {
    element = this;
    if (element._waiting) {
      element._waiting = false;
      //element._loading.parentNode.removeChild(element._loading);
      $(element._loading).remove();
      element._loading = null;
    }
    return this;
  }
};

if (MooTools) {
  Element.extend(MooWaiter);

  /* Extends functionality from <Waiter> into <Ajax>.

     Additional Options:
     useWaiter - (boolean) if true will automatically apply a <Waiter> to the update target; defaults to false. Note: if you do not specify a value for update option this is ignored.
     waiterOptions - (object) options value passed on to <Waiter> class.
     waiterTarget - (element) if specified, the Waiter will overlay this element, otherwise it uses the update target specified in the ajax options.
  */
  if (typeof Ajax != "undefined") {
     var Ajax = Ajax.extend({
         options: {
           useWaiter: false,
           waiterOptions: {
             className: 'waiting',
             timeUntilShow: MooWaiter.timeUntilShow,
             opacity: MooWaiter.opacity
           },
           waiterTarget: false
         },
         initialize: function(url, options) {
           this.parent(url, options);
           if (this.options.useWaiter && ($(this.options.waiterTarget) || $(this.options.update))) {
             var stop = function() {
               ($(this.options.waiterTarget) || $(this.options.update)).stopWaiting();
             }.bind(this);
             this.addEvent('onComplete', stop);
             this.addEvent('onFailure', stop);
           }
         },
         request: function(data) {
           if (this.options.useWaiter) {
             try {
               ($(this.options.waiterTarget) || $(this.options.update)).startWaiting(this.options.waiterOptions.className, this.options.waiterOptions.timeUntilShow, this.options.waiterOptions.opacity);
             } catch(e) {}
           }
           this.parent(data);
           return this;
         }
     });
  }
}
