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

/**
 * Convert all images from pixels to ems and apply the transaprent PNG
 * fix to all PNG files when using IE5 or IE6.
 *
 * @param pixelToEms The multiplier used to convert pixels to ems
 * (0 if the conversion should not be made).
 * @param PNGFixNeeded True if the PNG fix should be applied.
 * @param blank The source path to a blank GIF (usually 1x1 pixels).
 */

function processAllImages(pixelToEms, PNGFixNeeded, blank)
{
    var images = $("img");
    /*var useEms = pixelToEms != 0;*/
    if (/*!useEms && */ !PNGFixNeeded) return;

    for (var i = 0; i < images.length; i++) {
	/*if (useEms) imageSizeToEms(images[i], pixelToEms);*/
	if (PNGFixNeeded) fixPNG(images[i], blank);
    }
}

/**
 * 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)
{
    var oSrc;
    if (!image.oSrc) {
	image.oSrc = image.src;
	if (image.isPNG) {
	    image.oSrc = image.realSrc;
	}
    }

    if (image.isPNG) {
	image.runtimeStyle.filter =
	    "progid:DXImageTransform.Microsoft.AlphaImageLoader(" +
	    "src='" + newSrc + "',sizingMethod='scale')";
    }
    else {
	image.src = newSrc;
    }
}

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

function swapImageRestore(id) {
    var image = $(imgName);
    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;
    if (image.isPNG) {
	image.runtimeStyle.filter =
	    "progid:DXImageTransform.Microsoft.AlphaImageLoader(" +
	    "src='" + image.oSrc + "',sizingMethod='scale')";
    }
    else {
	image.src = image.oSrc;
    }
}

/**
 * Convert an image from pixels to ems. This allows an image to be
 * scaled on all browsers.
 *
 * @param image The image whose height and width needs to be converted
 * to ems.
 * @param pixelToEms The multiplier used to convert pixels to ems.
 */

function imageSizeToEms(image, pixelsToEms) {
    if (pixelsToEms <= 0) return;
    image.style.width = image.width * pixelsToEms + "em";
    image.style.height = image.height * pixelsToEms + "em";
}

/**
 * Make a transparent PNG work properly in IE5 and IE6. This should
 * only be called for IE5 and 6.
 *
 * @param image The image object to make transparent.
 * @param blank The source path to a blank GIF (usually 1x1 pixels).
 */

function fixPNG(image, blank)
{
    // Don't apply the fix twice

    if (image.isPNG) return;
    if (image.src.search(/\.png$/i) == -1) return;
    image.isPNG = 1;

    // Save the original src path

    image.realSrc = image.src;

    // Use the AlphaImageLoader filter and blend the original image
    // with the blank image

    image.src = blank;
    image.runtimeStyle.filter =
	"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
	image.realSrc + "',sizingMethod='scale')";
}
