/* 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,

  // inject waiting near target
  inlineInject: true,

  // Start waiting status - show loading element
  startWaiting: function(className, timeUntilShow, opacity, inlineInject) {
  	element = this;
    className = className || 'waiting';
    timeUntilShow = timeUntilShow || MooWaiter.timeUntilShow;
    opacity = opacity || MooWaiter.opacity;
    inlineInject = (inlineInject != null) ? inlineInject : MooWaiter.inlineInject;
    element.store('_waiting', true);
    if (!(element.retrieve('_loading'))) {      var e = new Element('div', {
      	'styles': {
      		'position': 'absolute',
      		'opacity': opacity
      	}
      }).injectInside(inlineInject ? (($(element).get('tag') == 'td') ? document.body : ($(element).getParent() || document.body)) : document.body);
      element.store('_loading', e); 
    }
    element.retrieve('_loading').className = className;
    (function() {
      if (this.retrieve('_waiting')) {
        $(this.retrieve('_loading')).setStyles({
          'left': this.getLeft(),
          'top': this.getTop(),
          'width': this.getSize().x,
          'height': this.getSize().y
        });
      }
    }).bind(element).delay(timeUntilShow);
    return this;
  },
  
  // Stop waiting status - hide loading element
  stopWaiting: function() {
    element = this;
    if (element.retrieve('_waiting')) {
      element.eliminate('_waiting');
      element.retrieve('_loading').destroy();
      element.eliminate('_loading');
    }
    return this;
  }
};

if (MooTools) {
  Element.implement(MooWaiter);
}
