LibManager.require( 'frame.scriptaculous.effects' );

function MapLoader(map, statusElement, textElement, barElement, fillElement) {
	this.map = map;
	this.statusElement = statusElement;
	this.bar = barElement;
	this.fill = fillElement;
	this.textElement = textElement;
}
MapLoader.prototype.map = null;
MapLoader.prototype.statusElement = null;
MapLoader.prototype.textElement = null;
MapLoader.prototype.bar = null;
MapLoader.prototype.fill = null;
MapLoader.prototype.hideMe = true;

MapLoader.prototype.show = function(text, pBar, fade) {
	if (fade) this.statusElement.hide();
	else this.statusElement.show();
	this.statusElement.style.height = "auto";
	this.textElement.innerHTML = text;
	if (pBar) {
		//this.map.disableDragging();
		this.bar.show();
	} else {
		this.bar.hide();
	}
	this.hideMe = pBar;
	if (fade) {
		Effect.Appear(this.statusElement, { duration: 0.25, from: 0, to: 0.9 });
	}
	if (Element.getDimensions(this.statusElement).height > 490) {
		this.statusElement.style.height = "490px";
	}
}
MapLoader.prototype.hide = function(hideMe, fade) {
	this.fill.style.width = "1px";
	if (hideMe) {
		//if (fade) Effect.Fade(this.statusElement, { duration: 0.2 });
		/*else*/ this.statusElement.hide();
	}
	this.map.enableDragging();
}
MapLoader.prototype.queue = null;
MapLoader.prototype.add = function(o, f, a) {
	if (!this.queue)
		this.queue = new Array();
	this.queue[this.queue.length] = function() {
		// execute once then remove all references
		o.f = f;
		o.f(a);
		a = null;
		o.f = null;
		o = null;
	};
}
MapLoader.prototype.queueCounter = 0;
MapLoader.prototype.execute = function(hideOnComplete) {
	// netscape hack to prevent weird effects caused by overflow:auto
	if (isBrowser('netscape')) {
		alert("this is ie????");
		$('mapSidebar').style.overflow = "hidden";
		$('mapLoading').style.overflow = "hidden";
	}
	var qInc = 5;
	if ((this.queueCounter > 0) && (this.queue))
		this.fill.style.width = (100 * this.queueCounter / this.queue.length) + "%";
	else
		this.fill.style.width = "1px";
	
	var stopAt = this.queueCounter + qInc;
	while ((this.queue) && (this.queueCounter < this.queue.length) && (this.queueCounter < stopAt)) {
		this.queue[this.queueCounter]();
		this.queue[this.queueCounter++] = null;
	}
	
	if ((this.queue) && (this.queueCounter < this.queue.length)) {
		setTimeout(this.execute.bind(this), 20);
	} else {
		for (var i = 0; (this.queue) && (i < this.queue.length); i++)
			this.queue[i] = null;
		this.queue = null;
		this.queueCounter = 0;
		this.hide(this.hideMe);
		// netscape hack to prevent weird effects caused by overflow:auto
		if (isBrowser('netscape')) {
			alert("this is ie????");
			$('mapSidebar').style.overflow = "auto";
			$('mapLoading').style.overflow = "auto";
		}
	}
}
MapLoader.prototype.cancel = function() {
	for (var i = 0; (this.queue) && (i < this.queue.length); i++)
		this.queue[i] = null;
	this.queue = null;
	this.queueCounter = 0;
	this.hide();
}