/*
 * ImageSwap.js
 * 
 * preloads and swaps images one for another
 */


/* Container for preloaded images */
var images = new Array();

/*
 * preloadImage() - loads an image into an array to be swapped with another
 * image later on
 * 
 * src - URL where the image file can be found
 * 
 * returns index of new image
 */
function preloadImage(src) {

	var image = new Image();
	
	image.src = src;
	images.push(image);
	return image.length - 1;
}

/*
 * swapImage() - swaps one image source for another
 * 
 * e - HTML <img> element to be swapped on
 * i - index of preloaded image to be swapped in
 * 
 * returns success
 */
function swapImage(e, i) {
	/* IE 6 Bug Fix */
	if (navigator.userAgent.indexOf("MSIE 6.0")) {
		for (var j = 0; j < images.length; j++) {
			var im = new Image();
			im.src = images[j].src;
			images[j] = im;
		}
	}
	
	if (i < images.length) {
		e.src = images[i].src;
		return true;
	}
	
	return false;
}


/*
 * swapImageById() - swaps one image source for another; finds the image by id
 * attribute in HTML
 * 
 * id - id attribute of <img> element to be swapped on
 * i - index of preloaded image to be swapped in
 * 
 * returns success
 */
function swapImageById(id, i) {
	
	var e;
	
	if (document) {
		e = document.getElementById(id);
	}
	
	if (e && e.src) {
		return swapImage(e, i);
	}
	
	return false;
}
