/*
 * jquery.swapimage.js
 *
 * $LastChangedRevision$
 *
 * $LastChangedDate$
 *
 * (C) 2007-2008 Quartocanale
 */

/**
 * Preload the images provided as parameters.
 */
jQuery.preloadImages = function() {
	for (var i = 0; i < arguments.length; i++) {
		jQuery('<img>').attr('src', arguments[i]);
	}
}

/**
 * Swap the image on mouse down/mouse up.
 *
 * @param originalImage the path used to load the original image
 * @param swappedImage the path used to load the swapped image 
 */
jQuery.fn.swapImages = function(originalImage, swappedImage) {
	if (swappedImage == undefined) {
		swappedImage = originalImage;
		originalImage = $(this).attr('src');
	}
	// Preload the images in order to avoid flickering
	jQuery.preloadImages(originalImage, swappedImage);
	this.mousedown(function(evt) {
		$(this).attr('src', swappedImage);
	}).mouseup(function(evt) {
		$(this).attr('src', originalImage);
	}).mouseout(function(evt) {
		$(this).attr('src', originalImage);
	});
}

jQuery.fn.swapImagesOnClick = jQuery.fn.swapImages;

/**
 * Swap the image on mouse over/mouse out.
 *
 * @param originalImage the path used to load the original image
 * @param swappedImage the path used to load the swapped image 
 */
jQuery.fn.swapImagesOnOver = function(originalImage, swappedImage) {
	if (swappedImage == undefined) {
		swappedImage = originalImage;
		originalImage = $(this).attr('src');
	}
	// Preload the images in order to avoid flickering
	jQuery.preloadImages(originalImage, swappedImage);
	this.mouseover(function(evt) {
		$(this).attr('src', swappedImage);
	}).mouseout(function(evt) {
		$(this).attr('src', originalImage);
	});
}