/* protoload 0.2 beta 
 * Created by Andreas Kalsch
 * Modified by Terence Johnson, Scribendi.com
 * Modified by Kurt Moyst, Scribendi.com
 * license: GPL
 * last change: 04.04.2007
 *
 * This simple piece of code automates the creation of Ajax loading symbols.
 * The loading symbol covers an HTML element with correct positioning and size - example:
 * $('myElement').showLoading() and $('myElement').hideLoading()
 */

if (window.Prototype) {
	Element.addMethods({
		// Start waiting status - show loading element
		showLoading: function(element) {
			element = $(element);
			var smallImg = '/img/css/waiting.gif';
			var largeImg = '/img/css/waiting_32.gif';
			var options = {
				delay: 200, 
				style: {
					backgroundColor: '#FFF', 
					color: '#000', 
					textAlign:'center',
					lineHeight: element.getHeight()+"px",
					fontWeight: 'bold'
				}, 
				message: undefined, 
				callback: undefined,
				safari_offset: 0
			}
			if (arguments[1] != undefined) {
				var user_options = arguments[1];
				if(user_options.style != undefined) {
					Object.extend(options.style, user_options.style);
					delete(user_options.style);
				}
				Object.extend(options, user_options);
			}						
			if ((element.getHeight() <= 48) || (element.getWidth() <= 48)) {
				imageName = smallImg;
			} else {
				imageName = largeImg;
			}
			element._waiting = true;
//			if(element._cachedColor == undefined) {
//				element._cachedColor = element.style.color;
//			}
			if (!element._loading) {
				var e = new Element('div');
				e.hide();
				element._loading = e;
				element.getOffsetParent().appendChild(e);
				e.setStyle(options.style);				
				e.setStyle({ 
					position: 'absolute' // cannot be overridden by options
				});
				if(options.message == undefined) {
					e.setStyle({
						backgroundImage: 'url(' + imageName + ')',
						backgroundRepeat: 'no-repeat',
						backgroundPosition: 'center center'
					});
				} else {
					var img = new Element("img");
					img.setStyle({
						paddingRight: '10px',
						verticalAlign: 'text-top'
					});
					img.src = imageName;
					e.insert(img);
					e.insert(options.message);
				}
			}
			window.setTimeout((function() {
				if (this._waiting) {
					if(options.callback != undefined) {
						options.callback();
					}
					this._loading.clonePosition(this);
					if(navigator.userAgent.match(/safari/i)) {
						this._loading.setStyle({
							top: parseInt(this._loading.style.top.split(/px/).first())-options.safari_offset+"px"
						})
					}
					this._loading.show().setOpacity(0.9);
//					this.style.color = "#aaa";
				}
			}).bind(element), options.delay);
		},		
		// Stop waiting status - hide loading element
		hideLoading: function(element) {
			if (element._waiting) {
				element._waiting = false;
				element._loading.parentNode.removeChild(element._loading);
				element._loading = null;
//				element.style.color = element._cachedColor;
			}
		}
	});
}
/* */