//********************************************************************
//* Image utility functions
//********************************************************************

/**
 * Preload a sequence of images
 *
 * The arguments consist of a variable length list of image source
 * paths.
 */

function preloadImages()
{
    var d = document;

    // Create the pre-load list if it doesn't exist

    if (d.images && !d.preLoadList) d.preLoadList = new Array();
    var nextImg = d.preLoadList.length;

    // We're going to load the images passed in and add them to the
    // pre-load list.

    var args = preloadImages.arguments;

    for (var i = 0; i < args.length; i++) {
	d.preLoadList[nextImg] = new Image;
	d.preLoadList[nextImg++].src = args[i];
    }
}

/**
 * Swap one image with another. When we want to restore the original
 * image, call swapImageRestore. This method applies the PNG fix where
 * needed.
 *
 * @param id The id of the image to swap.
 * @param newSrc The source path to the replacement image.
 */

function swapImage(id, newSrc)
{
    var image = $(id);
    if (image) swapImageObj(image, newSrc);
}

/**
 * Swap one image with another. When we want to restore the original
 * image, call swapImageRestoreObj. This method applies the PNG fix
 * where needed.
 *
 * @param image The image object to swap.
 * @param newSrc The source path to the replacement image.
 */

function swapImageObj(image, newSrc)
{
    if (!image.oSrc) {

	// This is needed to deal with ie6png.htc file, which destroys
	// our original src file

	if (image.style.filter) {
	    m = image.style.filter.match(/http:.*\.png/);
	    if (m != null) image.oSrc = m[0];
	}
	else {
	    image.oSrc = image.src;
	}
    }
    image.src = newSrc;
}

/**
 * Restore the image swapped out by swapImage().
 *
 * @param id The id of the image to restore.
 */

function swapImageRestore(id) {
    var image = $(id);
    if (image) swapImageObjRestore(image);
}

/**
 * Restore the image swapped out by swapImageObj().
 *
 * @param image The image object to restore.
 */

function swapImageObjRestore(image)
{
    if (!image.oSrc) return;
    image.src = image.oSrc;
    image.oSrc = null;
}
