// Scan webpage for images to cache
function cacheImages() {
	// Exectue function to return array with images for rollovers
	var imageNames = getName();
	var theImage = new Array();
	// Loop through the returned array of images
	for (count = 0; count < imageNames.length; count++) {
		// Filter out _x images
		if (false == (filter = imageNames[count].match(/_x$/))) {
			theImage[count] = new Image();
			theImage[count].src = imageNames[count];
		}
	}
}


// Grab names of images to cache and return an array
function getName() {
	// Set new array to hold image names
	var imageNames = new Array();
	// Set tracking variable, used to set array positions for valid entries only
	var track = 0;
	// Loop through all images in document, writing valid entries to the array imageNames
	for (count = 0; count < document.images.length; count++) {
		// If the image name is not blank
		var fileName = document.images[count].src;
		var tempName = document.images[count].name;
   		// Filter out names with nothing in the,
		if (tempName != "") {
			// Check for file format via extension...
			if (false != (ext = fileName.match(/(.jpg|.gif|.png)$/))) {
				// If match found then we know the format to use...
				if  (ext.length >= 1) {
					// Check for numeric extension and remove if needed (multiple rollovers)
					if (tempName.search(/_[0-9]{1,2}$/) != "-1") {
						tempName = tempName.replace(/_[0-9]{1,2}$/, "");
					}
					// Write name to array position for both up and over instances
					imageNames[track++] = ("/gfx/buttons/" + tempName + "-ov" + ext[0]);
					imageNames[track++] = ("/gfx/buttons/" + tempName + "-up" + ext[0]);
				}
			}
		}
	}
	// Send the array back to calling function
	return imageNames;
}


// Swap images on page
function itemSwap(opt_name, state) {
	// Set filename and target for testing...
	var fileName = document.images[opt_name].src;
	var targetFile = opt_name;
	// Check for file format via extension...
	if (false != (ext = fileName.match(/\.{1}(jpg|gif|png){1}$/))) {
		// If match found then we know the format to use...
		if  (ext.length >= 1) {
			// Check for numeric extension and remove if needed (multiple rollovers)
			if (targetFile.search(/_[0-9]{1,2}$/) != "-1") {
				targetFile = targetFile.replace(/_[0-9]{1,2}$/, "");
			}
			// Switch the image
			document [opt_name].src = ("/gfx/buttons/" + targetFile + "-" + state + ext[0]);
		}
	}
}