/* 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, newOpacity) {
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = Protoload.timeUntilShow;
		if (typeof newOpacity == 'string')
			Protoload.opacity = newOpacity;

		element._waiting = true;

		if (!element._loading) {
			var e = document.createElement('div');

			//(element.offsetParent || document.body).appendChild(element._loading = e);
			document.body.appendChild(element._loading = e);

			e.style.position = 'absolute';
			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) {}

			/*var zIndex = 0;
			if (window.UI)
				if (UI.zIndex)
					zIndex = ++UI.zIndex;
			if (!zIndex)
				zIndex = ++Protoload._zIndex;
			e.style.zIndex = zIndex;*/
		}
		element._loading.className = className;
		window.setTimeout((function() {
			if (this._waiting) {
				pos = Position.cumulativeOffset(this);
				var left = pos[0],
					 top = pos[1],
					 width = this.offsetWidth,
					 height = this.offsetHeight,
					 l = this._loading;
				/*
				var left = this.offsetLeft,
					top = this.offsetTop,
					width = this.offsetWidth,
					height = this.offsetHeight,
					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), timeUntilShow);
	},
	startCurtain: function(element, className, timeUntilShow, newOpacity) {
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (className == undefined)
			className = 'waiting';
		if (timeUntilShow == undefined)
			timeUntilShow = Protoload.timeUntilShow;
		if (typeof newOpacity == 'string')
			Protoload.opacity = newOpacity;

		element._waiting = true;

		if (!element._loading) {
			var e = document.createElement('div');

			if(navigator.userAgent.toLowerCase().indexOf("msie 6.0")>0){
				/*
				var tmpFrame = document.createElement('iframe');
				//tmpFrame.style.backgroundColor="#FF0000";
				tmpFrame.style.width=(screen.availWidth-20)+"px";
				tmpFrame.style.height=(screen.availHeight-90)+"px";
				e.appendChild(tmpFrame);
				*/

				if(screen.availHeight > document.body.scrollHeight){
					e.innerHTML="<iframe width='"+(screen.availWidth-16)+"px"+"' height='"+(screen.availHeight-85)+"px"+"'></iframe>";
				}else if(screen.availHeight < document.body.scrollHeight){
					e.innerHTML="<iframe width='"+(screen.availWidth-16)+"px"+"' height='"+(document.body.scrollHeight-85)+"px"+"'></iframe>";
				}

			}

			//(element.offsetParent || document.body).appendChild(element._loading = e);
			document.body.appendChild(element._loading = e);

			e.style.position = 'absolute';
			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) {}

			/*var zIndex = 0;
			if (window.UI)
				if (UI.zIndex)
					zIndex = ++UI.zIndex;
			if (!zIndex)
				zIndex = ++Protoload._zIndex;
			e.style.zIndex = zIndex;*/
		}
		element._loading.className = className;
		window.setTimeout((function() {
			if (this._waiting) {
				pos = Position.cumulativeOffset(this);
				var left = pos[0], top = pos[1], width = screen.availWidth-16, l = this._loading;
				if(screen.availHeight > document.body.scrollHeight){
					var height = screen.availHeight-85;
				}else if(screen.availHeight < document.body.scrollHeight){
					var height = document.body.scrollHeight;
				}
				/*
				var left = this.offsetLeft,
					top = this.offsetTop,
					width = this.offsetWidth,
					height = this.offsetHeight,
					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), timeUntilShow);
	},
	// Stop waiting status - hide loading element
	stopWaiting: function(element) {
		if (element._waiting) {
			element._waiting = false;
			element._loading.parentNode.removeChild(element._loading);
			element._loading = null;
		}
	}/*,

	_zIndex: 1000000*/
};

if (Prototype) {
	Element.addMethods(Protoload);
	Object.extend(Element, Protoload);
}
/* */