/****************************************************************************
Provides the core functions to be used on most, if not all, of the pages
within the application.
*****************************************************************************/

/*
 * Locates and returns a reference to the document object by the given ID.
 *
 * Parameters
 * ==========
 *	id	the string name of the id to locate
 */
function findObject(id){
	var ele;
	
	if (typeof(id) != "string"){
	    return id;
	}
	
	if (document.getElementById){
		ele = document.getElementById(id);
	} else {
		ele = document.all.images[id];
	}

	return ele;
}

/*
 * Swaps the existing source image for the new specified image, and 
 * stored the previous in another reference, ready for restoring.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and swap
 *	newImage	the string path and filename of the rollover image
 */
function swapImage(imgId, newImage) {
    var ele = findObject(imgId);
    if (ele){
        if (ele.altSrc)
        {
            newImage = ele.altSrc;
        }
        ele.oldSrc = ele.src;
        ele.src = newImage;
    }
}

/*
 * Wraps the swapImage() function to specify the rollover/hover image
 * based on the rule that there is an xxx_a.xxx version and an xxx_b.xxx
 * version.  This function works out the xxx_b.xxx name and passes it to
 * the swapImage() function for operation.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and swap
 */
function swapHoverImage(imgId)
{
	var img = findObject(imgId);
	if (img)
	{
		var currSrc = img.src;
		swapImage(imgId, currSrc.replace("_off","_on"));
	}
}


/*
 * Restores an image's original source image from its held reference.
 *
 * Parameters
 * ==========
 *	imgId		the string id of the image to locate and restore
 */
function restoreImage(imgId){
    var ele = findObject(imgId);
    if (ele){
        ele.src = ele.oldSrc;
    }
}

/*
 * Loads a series of images into the cache for quicker response time
 * when user's trigger rollovers.
 */
var imgArr = new Array();
function preloadImages() {
	for (i=0;i<preloadImages.arguments.length;i++) {
		imgArr[i]=new Image()
		imgArr[i].src=preloadImages.arguments[i]
	}
}
