
// Define the BusyBox class (function)
function BusyBox(instanceVarName, imageCount, imagePrefix, imageSuffix, imageDelay)
{
	// Initialize object
	this.ImageCount = imageCount;
	this.CurrentFrame = 0;
	this.ImageWidth = 0;
	this.ImageHeight = 0;
	this.ImagePrefix = imagePrefix;
	this.ImageSuffix = imageSuffix;
	this.ImageDelay = imageDelay;			// milliseconds to display each frame
	this.Enabled = true;
	this.DivID = "BusyBoxDiv";
	this.ImgID = "BusyBoxImg";
	
	// Retain the name of the instantiated object variable so that we can animate using the setTimeout statement
	this.instanceVarName = instanceVarName;
	
	// Initialize to null.  Allows us to stop the animation with clearTimeout(), should we ever want to
	this.timeout_id = null;
	
	// Cache (pre-load) images
	this.CacheImages();
}

BusyBox.prototype.CacheImages = function()
{
	// Instantiate the array to store the image references
	this.Images = new Array(this.ImageCount);
	
	// Load all the images to cache into the aniframes array
	for(var i = 0; i < this.ImageCount; i++)
	{
		this.Images[i] = new Image();
		this.Images[i].src = this.ImagePrefix + i + this.ImageSuffix;
	}
}

BusyBox.prototype.Animate = function()
{
	// Display the current frame
	document.getElementById(this.ImgID).src = this.Images[this.CurrentFrame].src;
	// Increment the frame counter
	this.CurrentFrame = (this.CurrentFrame + 1)%this.ImageCount;
	// Display the next frame in imageDelay milliseconds
	this.timeout_id = setTimeout(this.instanceVarName + ".Animate();", this.ImageDelay);
}

BusyBox.prototype.StopAnimate = function()
{
	this.timeout_id = null;
}

BusyBox.prototype.Hide = function()
{
	div = document.getElementById(this.DivID);
  if(document.getElementById(this.DivID)){div.style.visibility = "hidden";}
}

// This function displays the busy dialox box to the user.  This function centers the 
// busy dialog box, makes it visible, and starts the animation.
BusyBox.prototype.Show = function()
{
	if (this.Enabled) 
	{
		// Find the BusyBox div tag
		div = document.getElementById(this.DivID);
	  if(document.getElementById(this.DivID))
    {	
		  // Center the BusyBox in the window regardless of the scroll positions
		  myLeft = ((document.body.clientWidth - div.clientWidth) / 3) + document.body.scrollLeft;
		  myTop = ((document.body.clientHeight - div.clientHeight) / 3) + document.body.scrollTop;
		
		  // Position and show the busy box
		  div = document.getElementById(this.DivID);
		  div.style.top = myTop;
		  div.style.left = myLeft;
		  div.style.visibility = "visible";
		
		  // Start the animation
		  this.Animate();
    }
  }  
}
