// JavaScript Document

// Define an array to hold the folder "names/numbers" we will use to 
// get pictures, and a variable of how many we've found in this round.
var deptShown = new Array();
var dFIndex = 0;
var numFoldersFound=0;

// These variables are used in the creation of the path name to the
// picture that will be displayed, and in figuring out what picture
// in a folder to print next.
var folderIndex = 0;
var picN = 0;
var picName = "";
var pictureIndex = 0;

// Initialize deptArray and the number of tries we've used to get pictures
// from different folders.
initPicArrays();

// We want 4 pictures for the border. Change this number if the number of
// pictures desired changes.

// We will come out of this loop with the indicies of 4 picture folders
// stored in the first 4 positions of the deptShown array.
for (numFoldersFound = 0; numFoldersFound < 4; numFoldersFound++) {

	dFIndex = pickNewFolder();
	deptShown [numFoldersFound] = dFIndex;
}

// Now we have 4 folders from which to pull pictures for the border. We use
// the indicies stored in the deptShown array to index into the deptFolderNames
// array to get the actual folder names. Since each folder has a bunch of 
// pictures in it, we also have to choose which picture to use. We do this 
// using the second element of the deptArray array. This element has the index
// of the picture that we used last. 
// Note that if we want more than 4 pictures, and have gotten names for more
// than 4 directories, we need to change the number here too.
for (picN = 0; picN < 4; picN++) {

	// First we create short-cut variables so these value don't have to be 
	// "calculated" every time we need them.
	folderIndex = deptShown [picN];
	
	// We need to add 1 to the index into the picture folder since the names are of
	// the form 1_150x113.jpg to X_150x113.jpg where X is the number of pictures in
	// the folder. Since deptArray[folderIndex][1] is 0-based, we have to add 1 to it
	// in order to get the right picture name.
	pictureIndex = picArray[folderIndex] + 1;
	
	picName = "images/departments/" 
			+ deptFolderNames[folderIndex] + "/small/" + pictureIndex + "_150x113.jpg";
	
	// Now increment the index into the folder that will pick the next picture to
	// be used. If we have reached the last picture in the folder, then go back
	// to the first picture in the folder.
	picArray[folderIndex]++;
	if (picArray[folderIndex] == deptFolderLen[folderIndex])
		picArray[folderIndex] = 0;
	
	document.writeln(("<img name=borderNum" + picN + " src=" + picName + " width=150 height=113><p>"));
}

// Start a timeout for the pictures to be rotated out. The timeout
// has to be started in the function if we want to continue rotating
// pictures. The changeAPic function just swaps one of the pictures.
self.setTimeout ("changeAPic();", 3000);
