// JavaScript Document

/* To use this file, add this line to the html page, in the <head> section,
	after the jsHeadPictVars.js script, and any other variable declaration
	scripts (e.g. jsHeadMenuVars.js).
<script type='text/javascript' src="javascript/jsHeadPictFuncs.js"></script>
*/

/* The functions in this file are used to choose pictures from random folders
	in the images/departments folder. 
	
	This file is included via the page templates. It is a separate js file so that
	the code can be changed without the template having to be changed.
*/

// This function intializes the deptArray array, that is 
// used to keep track of which pictures in which folders have
// be displayed on the page border. This is a the value is
// essentially boolean - if the folder hasn't been used, its
// value is 0, and if the folder has been used the value is 1.
// It also initializes the array containing the "index" into
// the pictures in the folder. This will be a random number
// corresponding to a picture.
function initPicArrays () {
	var m;
	var pIndex;
	for (m = 0; m < numDepts; m++) {
		deptArray [m] = 0;
		
		// deptFolderLen is an array with the number of pictures
		// in each of the picture folders. So, to get a starting
		// point for choosing pictures out of the folder, get
		// a random number between 0 and the number of pictures
		// in the folder.
		pIndex = Math.round (Math.random() * deptFolderLen [m]);
			
		// If we do get a number that is the total number of pictures
		// in the folder, this will be a problem for a 0-based array
		// indexing, so reset the number to 0.
		if (pIndex == deptFolderLen [m]) 
			pIndex = 0;
		
		
		// This holds the place we will start in each picture folder.
		// This is an "index" into which pictures in the folder have
		// been used. It is simply incremented as a picture is used, then
		// started over when the number of pictures have been exhausted.
		// We start each folder from a different spot by using the random number.
		picArray [m] = pIndex;
	}
}

// This function resets the folders array so that any folder
// can be chosen again.
function clearDeptArray () {
	var m;
	for (m = 0; m < numDepts; m++)
		deptArray [m] = 0;
}

// This function chooses one of the pictures on the border
// to swap out.
function pickPicToChange () {
	var num;
	var idName;
	
	num = (Math.round (Math.random() * 4)) -1;
	if (num < 0)
		num = 0;
		
	idName = "borderNum" + num;
	return idName;
}

// This function picks a random folder to pull a picture from. It
// attempts to get different folders every time it is called until
// pictures have been pulled from all possible folders. If it takes
// a really long time to come up with a folder that hasn't been used,
// the deptArray is cleared so the next random pick will be OK.
function pickNewFolder () {
	var dIndex =0;
	var numFound = 0;
	
	while (numFound < 1) {
	
		// First, get a potential picture folder; get a random number and multiply
		// by the number of potential folders and round it to a whole number to get
		// a number between 0 and the number of potential folders. Note that this
		// is inclusive; we could get the number of potiential folders, which is a
		// problem if we are trying to use this number as an index since all arrays
		// use 0-based indexing. We fix that problem by making the number 0 if we
		// get the maximum instead.
		dIndex = Math.round (Math.random() * numDepts);
		if (dIndex == numDepts)
			dIndex = 0;
		
		// Increment the number of tries. 
		numTries ++;
		
		// If we haven't used this folder before, we'll know because the first
		// element of the array associated with the folder will be 0. If this is
		// the case, this is a good "hit", so we set the value of the first
		// element to 1, and remember this folder index in the array we are using 
		// to keep track of which folders we will pull pictures from for this set 
		// of pictures. Then we increment the number of "good" folders found.
		if (deptArray [dIndex] == 0) {
			deptArray [dIndex] = 1;
			numFound ++;
		}
		
		// If we already got a picture from this folder, then we check to see
		// how many times we've tried to get a unique folder. If this is 3 times
		// the number of potential folders, we clear the deptArray array so that 
		// the changes are much higher that we get folders that haven't been used.
		else {
			if (numTries > (3 * numDepts))
				clearDeptArray ();
		}
	}
	return dIndex;
}

// This function picks a new picture from a random folder. It simply goes
// through all the pictures in a folder sequentially, starting from some
// random picture (see initPicArrays()). It cycles through the pictures.
// This function has a timeout in it so that it will continue swapping out
// random pictures.
function changeAPic () {
	var picIDName = "";
	var picName = "";
	var pictureIndex = 0;
	var evalString = "";
	
	picIDName = pickPicToChange();
	newFolderIndex = pickNewFolder ();
	
	pictureIndex = picArray[newFolderIndex] + 1;
	picName = "images/departments/"
		+ deptFolderNames [newFolderIndex] + "/small/" + pictureIndex + "_150x113.jpg";
	
	picArray[newFolderIndex]++;
	if (picArray[newFolderIndex] == deptFolderLen[newFolderIndex])
		picArray[newFolderIndex] = 0;
	
	evalString = "document." + picIDName + ".src = \"" + picName + "\";";
	eval(evalString);

	// Set a timeout for another picture to be swapped out.
	self.setTimeout ("changeAPic();", (1 + (Math.random() * 2) * 2000));
}

