/* MooLoad 0.1 beta by Gaurav verma
 * Created on : 27th Sept 07
 *
 * 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/Mooload/ All credit goes to him
 */
Element.implement({
	// 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) {
			element = this;
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = 250;
		element._waiting = true;
		if (!element._loading) {
			var e = document.createElement('div');
			(document.body).appendChild(element._loading = e);
			e.style.position = 'absolute';
			try {e.style.opacity =0.8;} catch(e) {}
			try {e.style.MozOpacity = 0.8;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(0.8 * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = 0.8;} catch(e) {}
		}
		element._loading.className = className;
		window.setTimeout((function() {
			if (this._waiting) {
				var left = this.getLeft(), 
					top = this.getTop(),
					width = this.getCoordinates().width,
					height = this.getCoordinates().height,
					marginright = parseInt(element._loading.getStyle('margin-right')),
					marginleft = parseInt(element._loading.getStyle('margin-left')),
					margintop = parseInt(element._loading.getStyle('margin-top')),
					marginbottom = parseInt(element._loading.getStyle('margin-bottom')),
					l = this._loading;
				
				hMargin = marginleft + marginright;
				vMargin = margintop + marginbottom;
				
				l.style.left = left+'px';
				l.style.top = top+'px';
				l.style.width = (width - hMargin) + 'px';
				l.style.height = (height - vMargin) +'px';
				l.style.display = 'inline';
			}
		}).bind(element), timeUntilShow);
	},
	
	// Stop waiting status - hide loading element
	stopWaiting: function() {
		element = this;
		if (element._waiting) {
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;
		}
	}
});
