// Scan webpage for images to cache
function cacheImages(upState) {
	// Exectue function to return array with images for rollovers
	var imageNames = getName(upState);
	var theImage = new Array();
	var imgLen = imageNames.length;
	// Loop through the returned array of images
	for (var i = 0; i < imgLen; i++) {
		theImage[i] = new Image();
		theImage[i].src = imageNames[i];
	}
}


// Grab names of images to cache and return an array
function getName(upState) {
	// Set new array to hold image names
	var imageNames = new Array();
	// Set variable
	var track = 0;
	var fileName = null;
	var tempName = null;
	var imgLen = document.images.length;
	var reg = null;
	var ext = null;
	// Loop through all images in document, writing valid entries to the array imageNames
	for (var i = 0; i < imgLen; i++) {
		// If the image name is not blank
		fileName = document.images[i].src;
		// RegEx
		reg = new RegExp(upState + '\\.{1}(jpg|gif|png)$');
		// Check for file format via extension...
		if (null != (ext = reg.exec(fileName))) {
			// Write name to array position for both up and over instances
			imageNames[track++] = fileName;
			reg = new RegExp(upState + ext[0]);
			imageNames[track++] = (fileName.replace(reg, '-ov' + 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.getElementById(opt_name).childNodes[0].src;
	var reg = null;
	// Check for file format via extension...
	if (false != (ext = fileName.match(/\.{1}(jpg|gif|png){1}$/))) {
		// Switch the image
		if (state == 'ov') {
			reg = new RegExp('-up' + ext[0]);
			var newFile = fileName.replace(reg, '-ov' + ext[0]);

		} else if (state == 'up') {
			reg = new RegExp('-ov' + ext[0]);
			var newFile = fileName.replace(reg, '-up' + ext[0]);
		}
		document.getElementById(opt_name).childNodes[0].src = newFile;
	}
}