/* 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
 */

if (MooTools) {
	Element.implement({
		timeoutID: null,
		opacity: 0.8,
		startWaiting: function(className) {
			element = this;
		if (className == undefined)
			className = 'bigBlackWaiting';
		element._waiting = true;
		if (!element._loading) {
			var e = document.createElement('div');
			(element.offsetParent || document.body).appendChild(element._loading = e);
			e.style.position = 'absolute';
			try {e.style.opacity = this.opacity;} catch(e) {}
			try {e.style.MozOpacity = this.opacity;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(this.opacity * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = this.opacity;} catch(e) {}
		}
		element._loading.className = className;
		window.setTimeout((function() {
			if (this._waiting) {
				var left = this.getLeft(), 
					top = this.getTop(),
					width = this.getSize().x,
					height = this.getSize().y,
					l = this._loading;
					
				l.style.left = left+'px';
				l.style.top = top+'px';
				l.style.width = width+'px';
				l.style.height = height+'px';
				l.style.display = 'inline';
			}
		}).bind(element), 1);
		element.timeoutID = window.setTimeout((function() { this.stopWaiting(); }).bind(element), 10000);
	},
	
	// Stop waiting status - hide loading element
	stopWaiting: function() {
		element = this;
		if (element._waiting) {
			window.clearTimeout(element.timeoutID);
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;
		}
	},
	showMsg: function(strMsg) {
		element = this;
		element.focus();
		if (element._waiting) {
			window.clearTimeout(element.timeoutID);
			var e = element._loading;
			try {e.style.opacity = 0.9;} catch(e) {}
			try {e.style.MozOpacity = 0.9;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(0.9 * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = 0.9;} catch(e) {}
			element._loading.className = "showMsg";
			element._loading.innerHTML = strMsg;
		}
		else {
			element._waiting = true;
			if (!element._loading) {
				var e = document.createElement('div');
				(element.offsetParent || document.body).appendChild(element._loading = e);
				try {e.style.opacity = this.opacity;} catch(e) {}
				try {e.style.MozOpacity = this.opacity;} catch(e) {}
				try {e.style.filter = 'alpha(opacity='+Math.round(this.opacity * 100)+')';} catch(e) {}
				try {e.style.KhtmlOpacity = this.opacity;} catch(e) {}
			}
			element._loading.className = "showMsg";
			element._loading.innerHTML = strMsg;
			window.setTimeout((function() {
				if (this._waiting) {
					var left = this.getLeft(), 
						top = this.getTop(),
						width = this.getSize().x,
						height = this.getSize().y,
						l = this._loading;
						
					l.style.left = left+'px';
					l.style.top = top+'px';
					l.style.width = width+'px';
					l.style.height = height+'px';
					l.style.display = 'inline';
				}
			}).bind(element), 1);
		}		
	}
	});
}