// ---------------------------
// Legal
// ---------------------------

// ---------------------------
// Constructor
// ---------------------------
// BusyBox class constructor
// Arguments:
//   ImgID - id of the Image tag to use.
//   varName - name of the variable this instance of the MainPic is assigned to.
//   imageCount - number of image in the animation sequence.
//   imageNamePrefix - name prefix for each image.
//   imageNameSuffix - name suffix for each image.
//   imageDelay - number of milliseconds to display each image.
//
//   This example uses the default busy box layout defined internally (in the javascript).
//		MainPic("MainPicIFrame", "mainPicture", 4, "../resources/images/homepage/localhost/MainPic_", ".jpg", 3000, 280, 255);


function MainPic(id, varName, imageCount, imageNamePrefix, imageNameSuffix, imageDelay, width, height)
{
	// Initialize object
	this.id = id;
	this.ImageCount = imageCount;
	this.CurrentImageIndex = 0;
	this.ImageNamePrefix = imageNamePrefix;
	this.ImageNameSuffix = imageNameSuffix;
	this.ImageDelay = imageDelay;
	this.DivID = "MainPicDiv";
	this.ImgID = "MainPic1";
	this.Enabled = true;
	this.Width = width;
	this.Height = height;



	// Retain the name of the instantiated object variable so that we can animate 
	// using the setTimeout statement
	this.VarName = varName;
	// Allows us to stop the animation with clearTimeout(), should we ever want to
	this.timeout_id = null;

	// Cache (pre-load) images
	this.CacheImages();
	// Get reference to the IFrame object
	this.IFrame = document.getElementById(this.id);
	
	// Hide the busy box
	this.Hide();
	// Load the busy box contents using the internally defined layout.
	this.RenderContent();
	
	// If this browser does not support IFRAME tags then disable this control.  The
	// next version will implement the use of a DIV instead of the IFRAME tag; 
	// even though there are a couple minor issues with using DIV tags.
	if( !frames[this.id] )
		this.Enabled = false;
	
	this.Show();
}

// --------------------------------
// Instance Methods
// --------------------------------


// CacheImages:
// Pre-loads the images from the server and stores a reference to each
// image.  This allows the images to be presented to the user quickly
// for smooth image animation.
MainPic.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.ImageNamePrefix + i + this.ImageNameSuffix;
	}
}

// RenderContent:
// This method is used when the default busy box layout is used; not a custom 
// layout.  This method is called when the url argument for the constructor is null.
MainPic.prototype.RenderContent = function()
{

	// Get the IFrame document object
	var doc = this.GetIFrameDocument();

	//var style = " style='BORDER: navy 3px solid; POSITION: absolute;' ";
	
	doc.open();
	doc.writeln("<body ondragstart='return false;' style='Margin: 0px; Background-Color: white'>");
	doc.writeln("   <div id='" + this.DivID + "' align=center>");
	doc.writeln("      <img id='" + this.ImgID + "' name='" + this.ImgID + "' STYLE='FILTER: progid:DXImageTransform.Microsoft.fade(duration=0.9);'>");
	doc.writeln("      <br><h3></h3>");
	doc.writeln("   </div>");
	doc.writeln("</body>");
	doc.close();
}

// GetIFrameDocument:
// Returns a reference to the document object in the IFrame.
MainPic.prototype.GetIFrameDocument = function()
{
	var doc;

	if( this.IFrame.contentDocument )
	{
		// For NS6
		doc = this.IFrame.contentDocument; 
	}
	else if( this.IFrame.contentWindow ) 
	{
		// For IE5.5 and IE6
		doc = this.IFrame.contentWindow.document;
	}
	else if( this.IFrame.document )
	{
		// For IE5
		doc = this.IFrame.document;
	}
	else
	{
// TODO: Confirm this should be the default
		doc = this.IFrame.document;
	}
	
	return doc;
}

// Resize:
// Resizes the busy box IFrame by setting its width and height attributes
// to the size of its contents.
MainPic.prototype.Resize = function()
{
	// Set the width to the value specified.
	this.IFrame.style.width = this.Width;
	this.IFrame.style.height = this.Height;
}


// IsAnimating:
// Returns a boolean value representing the state of the animation.
MainPic.prototype.IsAnimating = function()
{
	if( this.timeout_id == null)
		return false;
	else
		return true;
}


// Animate:
// Performs the animation process.  This is accomplished by showing the "current" 
// image in the animation sequence process; and then submitting a timed statement
// to execute in x number of milliseconds.
MainPic.prototype.Animate = function()
{
	// Assign the current image sequence to display
	if( frames[this.id] )
	{
		// browser supports frames
		var img = frames[this.id].document.getElementById(this.ImgID);
		img.filters[0].Duration=2.5;
		img.filters[0].Apply();
		img.src = this.Images[this.CurrentImageIndex].src;
		img.filters[0].Play(0.8);
	}
	else
		// browser does not support frames
		document.getElementById(this.ImgID).src = this.Images[this.CurrentImageIndex].src;

	// Increment the current image index
	this.CurrentImageIndex = (this.CurrentImageIndex + 1)%this.ImageCount;
	
	// Display the next image in (imageDelay value) milliseconds (i.e. 125)
	this.timeout_id = window.setTimeout(this.VarName + ".Animate();", this.ImageDelay);
}

// StartAnimation:
// Starts the animation process.
MainPic.prototype.StartAnimate = function()
{
	if( this.IsAnimating() )
		return;
	
	this.Animate();
}

// StopAnimation:
// Stops the animation process.
MainPic.prototype.StopAnimate = function()
{
	clearTimeout(this.timeout_id);
	this.timeout_id = null;
}

// Hide:
// Hides the busy box making it invisible to the user.
MainPic.prototype.Hide = function()
{	
	this.StopAnimate();
}

// Show:
// This function displays the busy box to the user.  This function centers the 
// busy dialog box, makes it visible, and starts the animation.  This function 
// will typically be called by the body event.
//
// Example:
//		<body onbeforeunload="busyBox.Show();" >
MainPic.prototype.Show = function()
{
	if( !this.Enabled )
		return;

	if( this.IsAnimating())
		return;	
	this.Resize();
	//this.Center();
	
	// Set the busy box to be visible and make sure it is on top of all other controls.	
	this.IFrame.style.visibility = "visible";
	this.IFrame.style.zIndex = "999999";
	
	// Start the animation
	this.StartAnimate();
}

// --------------------------------
// Class Methods
// --------------------------------

// IsBrowserIE:
// Returns true if the executing browser it a Microsoft Internet Explorer browser.
MainPic.IsBrowserIE = function()
{
	try
	{ return (window.navigator.userAgent.indexOf("MSIE ") > 0); }
	catch(x)
	{ return false; }
}

// IsBrowserNS:
// Returns true if the executing browser it a Netscape browser.
MainPic.IsBrowserNS = function()
{
	try
	{ return (window.navigator.userAgent.indexOf("Netscape") > 0); }
	catch(x)
	{ return false; }
}

// IsBrowserFirefox:
// Returns true if the executing browser it a Firefox browser.
MainPic.IsBrowserFirefox = function()
{
	try
	{ return (window.navigator.userAgent.indexOf("Firefox") > 0); }
	catch(x)
	{ return false; }
}
