/* protoload 0.1 beta by Andreas Kalsch
 * last change: 09.07.2007
 *
 * 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()
 */

Protoload = {
	// 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(element, className, timeUntilShow) {
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = Protoload.timeUntilShow;

		element._waiting = true;

		if (!element._loading)
		{
			var e = new Element('div').setStyle({position: 'absolute'}), p = element.getOffsetParent();

			try {e.style.opacity = Protoload.opacity;} catch(e) {}
			try {e.style.MozOpacity = Protoload.opacity;} catch(e) {}
			try {e.style.filter = 'alpha(opacity='+Math.round(Protoload.opacity * 100)+')';} catch(e) {}
			try {e.style.KhtmlOpacity = Protoload.opacity;} catch(e) {}

			// TODO: get rid of this crap.
			if ('HTML' == p.tagName)
			{
				p = document.body;
			}

			p.insert({'top' : (element._loading = e)});
		}

		element._loading.className = className;

		(function() {
			var co = $(this).positionedOffset();

			if (Prototype.Browser.IE)
			{
				this.select('select').each(function(el) {
					el._visibility = el.getStyle('visibility');
					el.setStyle({visibility: 'hidden'});
				});
			}

			if (this._waiting)
			{
				var left = co[0],
					top = co[1],
					width = this.offsetWidth,
					height = this.offsetHeight,
					l = this._loading;

				l.style.left = left - 0 + 'px';
				l.style.top = top - 0 + 'px';
				l.style.width = width + 0 + 'px';
				l.style.height = height + 0 + 'px';
				l.style.display = 'inline';
			}
		}.bind(element))();
	},

	// Stop waiting status - hide loading element
	stopWaiting: function(element) {
		if (element._waiting)
		{
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;

			if (Prototype.Browser.IE)
			{
				element.select('select').each(function(el) {
					if (el._visibility)
					{
						el.setStyle({visibility: el._visibility});
					}
				});
			}
		}
	}
};

if (Prototype) {
	Element.addMethods(Protoload);
	Object.extend(Element, Protoload);
}
/* */