var preloader = Class.create({
	initialize: function(element) {
		this.element = $(element);
		
		if (!arguments[1])
		{
			arguments[1] = new Array();
			if (this.element.style.width)
				arguments[1].width = this.element.style.width;
			else
				arguments[1].width = this.element.width;
			if (this.element.style.height)
				arguments[1].height = this.element.style.height;
			else
				arguments[1].height = this.element.height;
		}
		else
		{
			if (!arguments[1].width)
				if (this.element.style.width)
					arguments[1].width = this.element.style.width;
				else
					arguments[1].width = this.element.width;
			if (!arguments[1].height)
				if (this.element.style.height)
					arguments[1].height = this.element.style.height;
				else
					arguments[1].height = this.element.height;
		}
		
		this.options = Object.extend({
			min_wait: 2000,
			width: '100px',
			height: '100px',
			zindex: parseInt(this.element.style.zIndex)+302
		}, arguments[1] || {});
		
		this.activate();
	},
	
	activate: function() {
		this.loader = new Element('div').setStyle({ zIndex: this.options.zindex, width: this.options.width, height: this.options.height, opacity: 0.5, position: 'absolute', top: this.element.offsetTop, left: this.element.offsetLeft, backgroundColor: '#000000' });
		this.loader.innerHTML = '<img src="/img/progress2.gif" style="position: absolute; left: '+(parseInt(this.options.width)/2-8)+'; top: '+(parseInt(this.options.height)/2-8)+';">';
		this.id = this.loader.identify();
		 
		this.element.parentNode.appendChild (this.loader);

		this.stopped = true;
		
		this.timer = setTimeout(this.timer_complete.bind(this), this.options.min_wait);
	},
	
	deactivate: function() {
		if (this.stopped)
			this.loader.remove();
		else
			this.timer_check = setTimeout(this.check_timer.bind(this), 10);
	},
	
	timer_complete: function() {
		this.timer = 0;
		this.stopped = true;
	},
	
	check_timer: function() {
		if (this.stopped)
		{
			this.timer = 0;
			this.timer_check = 0;
			this.loader.remove();
		}
	}
});


pre_loader = {
	activate: function(element) {
		this.element = $(element);
		
		if (!arguments[1])
		{
			arguments[1] = new Array();
			if (this.element.style.width)
				arguments[1].width = this.element.style.width;
			else
				arguments[1].width = this.element.width;
			if (this.element.style.height)
				arguments[1].height = this.element.style.height;
			else
				arguments[1].height = this.element.height;
		}
		else
		{
			if (!arguments[1].width)
				if (this.element.style.width)
					arguments[1].width = this.element.style.width;
				else
					arguments[1].width = this.element.width;
			if (!arguments[1].height)
				if (this.element.style.height)
					arguments[1].height = this.element.style.height;
				else
					arguments[1].height = this.element.height;
		}
		
		this.options = Object.extend({
			min_wait: 2000,
			width: '100px',
			height: '100px',
			zindex: parseInt(this.element.style.zIndex)+302
		}, arguments[1] || {});
		
		this.loader = new Element('div').setStyle({ zIndex: this.options.zindex, width: this.options.width, height: this.options.height, opacity: 0.5, position: 'absolute', top: this.element.offsetTop, left: this.element.offsetLeft, backgroundColor: '#000000' });
		this.loader.innerHTML = '<img src="/img/progress2.gif" style="position: absolute; left: '+(parseInt(this.options.width)/2-8)+'; top: '+(parseInt(this.options.height)/2-8)+';">';
		this.element._id = this.loader.identify();
		
		this.element.parentNode.appendChild (this.loader);

		this.element._stopped = false;
		
		this.element._timer = setTimeout((function(){
			this._timer = 0;
			this._stopped = true;
		}).bind(this.element), this.options.min_wait);
	},
	
	deactivate: function(element) {
		this.element = $(element);
		if (this.element._stopped)
			$(this.element._id).remove();
		else
			this.element._timer_check = setTimeout((function () {
			if (this.element._stopped)
			{
				this.element._timer = 0;
				this.element._timer_check = 0;
				$(this.element._id).remove();
			}		
		}).bind(this.element), 10);
	}
};
