/*
ProgressBar 2.0.3 (c) 2007 by J. Reijers

### EXAMPLE ###

<body>
<script type="text/javascript">
pBar = new ProgressBar();
with (pBar) {
	width = 200;
	height = 12;
	colour = "#FFF";
	fillColour = "#333";
	opacity = 80;
	init();
}

for (i = 0; i < 400; i++) pBar.set(i, 400);
</script>
*/

function ProgressBar(id) {
	if (typeof id == "undefined") {
		if (typeof progBarOccurence == "undefined") progBarOccurence = 0; else progBarOccurence++;
		this.id = "progBar_" + progBarOccurence;
	} else this.id = id;
	this.centreX = parseInt(document.body.offsetWidth / 2, 10);
	this.centreY = parseInt(document.body.offsetHeight / 2, 10);
	this.width = 0;
	this.height = 0;
	this.x = -1;
	this.y = -1;
	this.colour = "";
	this.fillColour = "blue";
	this.text = "";
	this.textSize = 0;
	this.textColour = "white";
	this.activeText = "";
	this.opacity = 100;
	this.hideWhenFinished = true;
	this.hideByFading = true;
	this.fadeDelay = 20; // milliseconds
	this.fadeStep = 5;

	eval(this.id + "_globVar = this;");

	this.init = function() {
		if (this.x == -1) this.x = this.centreX - parseInt(this.width / 2, 10);
		if (this.y == -1) this.y = this.centreY - parseInt(this.height / 2, 10);
		if (this.textSize == 0) this.textSize = this.height;
		this.origOpacity = this.opacity;
		document.write("<div id='" + this.id + "' style='position: absolute; left: " + this.x + "; top: " + this.y + "; width: " + this.width + "px; height: " + this.height + "px; font-size: " + this.textSize + "; color: " + this.fillColour + "'><span id='" + this.id + "_text' style='width: " + this.width + "px; overflow: hidden'>" + this.text + "</span>" + (this.text == "" ? "" : "<br>") + "<div style='border: 1px solid " + this.fillColour + "; background-color: " + this.colour + "; padding: 1px; margin: 2px'><div id='" + this.id + "_filler' style='background-color: " + this.fillColour + "; width: 0; height: " + this.height + "px; font-size: " + this.textSize + "px; color: " + this.textColour + "; overflow-x: hidden'></div></div></div>");
		this.divShell = document.getElementById(this.id);
		this.divFiller = document.getElementById(this.id + "_filler");
		this.divText = document.getElementById(this.id + "_text");
		this.setOpacity();
		this.activeText = this.text;
	}

	this.setOpacity = function(value) {
		if (typeof value == "undefined") var value = this.opacity;
		if (value < 0) value = 0;
		if (value == 0) this.divShell.style.display = "none"; else {
			this.divShell.style.filter = "alpha(opacity=" + value + ")";
			this.divShell.style.opacity = value / 100;
			this.divShell.style.mozOpacity = value / 100;
		}
		this.opacity = value;
	}

	this.set = function(newNr, finNr) {
		if (typeof finNr == "undefined") var finNr = 100;
		var newPerc = (finNr == 0) ? 100 : parseInt((newNr / finNr) * 100, 10);
		this.divFiller.style.width = newPerc + "%";
		this.divFiller.innerHTML = newPerc + "%";
		if (this.text != this.activeText) {
			this.divText.innerHTML = this.text;
			this.activeText = this.text;
		}
		if (this.hideWhenFinished && newNr >= finNr) this.hide();
	}

	this.show = function() {
		this.divShell.style.display = "";
		this.setOpacity(this.origOpacity);
	}

	this.hide = function() {
		if (this.hideByFading) {
			if (this.opacity > 0) {
				this.opacity -= this.fadeStep;
				this.setOpacity();
				if (this.opacity > 0) setTimeout(this.id + "_globVar.hide()", this.fadeDelay);
			}
		} else this.divShell.style.display = "none";
	}
}
