// lib_progressbar.js - Displays a progress bar on screen

// Requires: lib_core.js
if (!LIB_CORE_LOADED) {
  // Here is where you _could_ do something to alert the user to a problem, but we don't want to do that.
}

var repeatBar = false; // For optional 3rd param, repeats the bar indefinitely.
var removeBar = false; // Removes the progress bar once it hits 100% (Default: false = stop at 99%).
var progressTimer = null;

// startWait(barTime,barText[,repeatBar]) - Starts the progress bar with given options
function startWait(barTime,barText) {
	ProgressCreate(barTime,barText);
	window.setTimeout("doTick()", 200);
	if (arguments.length == 3 && arguments[2] != "") { repeatBar=true; } else { repeatBar=false; }
	return true;
}

function stopWait() {
	ProgressDestroy();
	return false;
}

var _progressWidth = 200;	// Display pixel width of progress bar (default)
var _progressEnd = 10;    // Default duration of progress bar (in seconds)
var _progressAt = 0;      // Start counting at 0
var updText = "Loading";  // Default text, can be overridden with barText

// Create and display the progress dialog.
// end: The number of steps to completion
function ProgressCreate(end,newText) {
	// Initialize state variables
	_progressEnd = end * 8; // Increase the end time, because we update 8x per second
	_progressAt = 0;
	if (newText != "") {
	  updText = newText;
	  _progressWidth = (updText.length + 100);
  }

	// Move layer to center of window to show
	document.getElementById("progress").style.display = "block";
	document.getElementById("progress").focus = true;
	document.getElementById("progress").scrollIntoView(true);
	document.getElementById("bartext").innerHTML = updText + "...";
	document.body.style.cursor = "wait";
	ProgressUpdate();	// Initialize bar
}

// Hide the progress layer
function ProgressDestroy() {
	// Move off screen to hide
	document.getElementById("progress").style.display = "none";
	document.body.style.cursor = "auto";
	_progressAt = _progressEnd;
}

// Increment the progress dialog one step
function ProgressStepIt() {
	_progressAt++;
	if (_progressAt >= _progressEnd && repeatBar) {
	    _progressAt = _progressAt % _progressEnd;
  }
	ProgressUpdate();
}

// Update the progress dialog with the current state
function ProgressUpdate() {
	var n = (_progressWidth / _progressEnd) * _progressAt;
	n = parseInt(n);
	if (n > 99 && !repeatBar && removeBar) {
	  ProgressDestroy();
		return;
	} else if (n > 99 && !repeatBar && !removeBar) {
    // We reached the end, but we're not repeating, and we're not removing the bar
	  _progressAt = _progressEnd - 1; // Never let the counter reach the end (just loop)
	  clearTimeout(progressTimer); // Try to kill off the timeout for incrementing the bar
	}
	if (n > 99) {
	  n = 99; // Never show a value greater than 99%
	}
  var pbar = document.getElementById("pbar");
  var bpercent = document.getElementById("barpercent");
	pbar.style.width = (99 - n) + "%"; // We're actually shrinking the div, in order to reveal the animated gif below it
	bpercent.innerHTML = n + "%";
}

function doTick() {
	if(_progressAt >= _progressEnd) {
		ProgressDestroy();
		return;
	}
	ProgressStepIt();
	progressTimer = window.setTimeout("doTick()", 125); // Updating 8x per second creates smaller steps
}

// Create layer for the progress dialog
document.write("<div name=\"progress\" id=\"progress\" class=\"progressBox\" style=\"width:" + (_progressWidth + 20) + "px;\">");
	document.write("<table class=\"progressTable\">");
	document.write("<tr><td align=\"center\">");
	document.write("<div name=\"bartext\" id=\"bartext\" class=\"progressStatus\" style=\"float:left;\">" + updText + "</div>");
	document.write("<div name=\"barpercent\" id=\"barpercent\" class=\"progressStatus\" style=\"float:right;\">0%</div>");
	document.write("</td></tr>");
	document.write("<tr><td align=\"left\" class=\"progressBar\" style=\"background-color:#336699;\">");
	document.write("<div style=\"background:url(/images/progress_bar.gif);width:100%;\">");
	document.write("<div name=\"pbar\" id=\"pbar\" style=\"width:0%;\">&nbsp;</div>");
	document.write("</div>");
	document.write("</td></tr>");
	document.write("</table>");
document.write("</div>");

// Setup the standard "Loading..." progress bar
function doLoadWait(obj) {
  startWait(60,'Loading');
}

// Set a global flag that indicates this library has been loaded by the browser.
var LIB_PROGRESSBAR_LOADED = true;

// End library
