// =====================================================
// Global variable declarations For Animation
// The global definitions are in _Global.js
// =====================================================
// ========
// Global declarations For Animation Routines
// ========
var vCurArea = 1;		// Current area to display
var vMaxAreas = 1;		// Maximum display areas
// ===============================================
// Global variable declarations For Animation Routines
// ===============================================
var swIsImgFirstTime = "yes";	// Is this the first time - display all boxes
var whichImg = 0;		// Holds the index number of the next image to display
var nImages = -1;	// MUST be -1 for the following to work
var aImages = new Array();	// Holds the images to preload
var swDoPreload = "Yes";	// Should the preload of images be done?
var vTimeOut ;			// holds setTimeout control
var dispArea="picDisp" + vCurArea;	// area to display in
var descArea="picDesc" + vCurArea;	// area for description


// =====================================================
// For animation of images on a web page
//   THREE Javascipt modules must be included in a web page
//
//	1. _Global.js  This should already be in every web page
//		It contains the global variables for the process
//	2. _fAnimation.js  This is the main processing routines
//		and can be used for any animation
//	3. _fAnimation-xxxxxxx.js  This contains the preload function
//		for the images to be displayed.
//		It also loads the preload array and set the variable
//		that has the total images loaded.
//		Replace the xxxxxxx with what ever name you gave it.
//		IE: _fAnimation-FamousMasons.js
//			Loads the table of Famous Mason Images.
//
//	NOTE:
//		There can be only 1 animation active on a web page.
//		To have more than 1 - major re-work of these routines 
//		will need to be done.
// =====================================================
// =====================================================
// To activate the animations, you need the following
//  in your web page.
//
// EITHER: - <BODY onLoad="animate()">
//   - or -
//	<script type="text/javascript">animate()</script>
//		somewhere EARLY in the web page.
//
// You will then need to insert the following where you want
//	the animation to appear
//		<IMG NAME="picDisp" SRC="#" title=" ">
//		<br><br>
//		<div id="picDesc"></div>
// =====================================================

// =====================================================
// This function swaps the current image
// for the incoming parameter newImage;
// then it calls itself every second,
// passing itself a different newImage
// each time.  The result is a simple
// on/off animation.
// =====================================================

function animate() {

var tDelay = 5347;	// Delay time for animation
    tDelay = 11359;	// Delay time for animation

if (swIsImgFirstTime == "yes") { fDoFirstTime(); }

clearTimeout(vTimeOut);		// Clear any residual timeouts

fGetImageIndex()

// Change the picture and description
fswap(dispArea, aImages[whichImg].URL, descArea, aImages[whichImg].Desc);

fSetNextDisplayArea()

vTimeOut = setTimeout("animate()", tDelay)
}

// =====================================================
// 
// Routine to display the first set of images
//
// =====================================================
function fDoFirstTime() {

while ( vCurArea != vMaxAreas )
{
fGetImageIndex()
fswap(dispArea, aImages[whichImg].URL, descArea, aImages[whichImg].Desc);
fSetNextDisplayArea()
}
swIsImgFirstTime = "no"
return
}

// =====================================================
//
// The following function gets the image index to display
//
// =====================================================

function fGetImageIndex() 
{

var i
var j

if(swDoPreload == "Yes") {fPreloadImages()}

fHasDisplayed()

for (i=0; i<=nImages; i++)
	{
	j=fGetRandom(nImages)
	if(aImages[j].HasDisplayed == "n") { break }
	}

if (aImages[j].HasDisplayed == "n") { whichImg = j }
	else { fDoSeqLookup() }


aImages[whichImg].HasDisplayed = "y"

return
}

// =====================================================
//
// The following function gets the image index to display
//
// =====================================================

function fDoSeqLookup() 
{

var i

for (i=0; i<=nImages; i++)
{
	if(aImages[i].HasDisplayed == "n")
	{
	whichImg = i;
	break;
	}
}
return
}

// =====================================================
// This function checks to see if all images have been displayed
// if so, reset the HasDisplayed switch for all images
// =====================================================

function fHasDisplayed() {

var allDisplayed = "y"

for (i=0; i<=nImages; i++)
	{
	if (aImages[i].HasDisplayed == "n") {allDisplayed = "n"; break;}
	}

if (allDisplayed == "y")
	{
	for (i=0; i<=nImages; i++)
		{
		aImages[i].HasDisplayed = "n"
		}
	}
return
}

// =====================================================
// The swap() function replaces the image
// associated with the first input
// parameter (id) with the image specified
// for the second input parameter (newSrc)
// =====================================================

function fswap(pImgArea, pURL, pDescArea, pDesc) {

var theImage = findImage(document, pImgArea);

if (theImage)
	{
	theImage.src = pURL;		// set the Img URL to display
//	theImage.title = pDesc;		// set the Title= parm same as the URL
	var x = document.getElementById(pDescArea)	// set Description area to URL
	x.innerHTML=pDesc;				//....
	}
}

// =====================================================
// 
// Routine get the next display area
//
// =====================================================
function fSetNextDisplayArea() {

vCurArea += 1
if( vCurArea > vMaxAreas ){ vCurArea = 1 }

dispArea = "picDisp" + vCurArea
descArea = "picDesc" + vCurArea

return
}

// =====================================================
// The following function returns a "random number
//   From 0 ot max nbr specified
// 
// NOTE: This implies the caller sets the max nbr
//       Relative to 0 (zero)
// =====================================================

function fGetRandom(pMaxNbr) 
{
return Math.round((Math.random()*1000)%pMaxNbr)
}

// =====================================================
// The findImage() function locates and
// returns an Image objectUsing a document,
// the name of an image to find, and an index
// (the three input parameters).
// =====================================================

function findImage(pDoc, pImgArea) {

    // Declare a variable to hold the image
    // (if/when we find it)
    var theImage = false;

    // The document object's images property contains a
    // collection of all the images in a document.

    if (pDoc.images) {
        theImage = pDoc.images[pImgArea];
    }

    // Return the image if we've already found it
    if (theImage) {
        return theImage;
    }

    // Otherwise, return an error
    return (false);
}
