(function($)
{
	$.fn.rollovers = function(options) 
	{
		options = $.extend({overImageSuffix:"-over", disabledImageSuffix: "-disabled", downImageSuffix:"-down", blur:true, down:false}, options || {});
	
		var overSuffix	= options.overImageSuffix;
		var downSuffix	= options.downImageSuffix;
		var disabledSuffix = options.disabledImageSuffix;
		var isBlur		= options.blur;
		var isDown		= options.down;

		// Cache the over- and down- state images.
		this.each(function(i) 
		{
			var overStateImages = [];
			var downStateImages = [];
			
			var imageSource = $(this).attr("src");
			var filetype = imageSource.substring(imageSource.lastIndexOf("."));
	
			overStateImages[i] = new Image();
			if (imageSource.indexOf(disabledSuffix) != -1)
			{
				$(overStateImages[i]).attr("src", imageSource.replace(disabledSuffix + filetype, overSuffix + filetype));
				
				if (isDown) 
				{
					// Preload the image by adding it to the down-state images array and extend it.
					downStateImages[i] = new Image();
					$(overStateImages[i]).attr("src", imageSource.replace(disabledSuffix + filetype, downSuffix + filetype));
				}
			}
			else 
			{
			
				$(overStateImages[i]).attr("src", imageSource.replace(filetype, overSuffix + filetype));
				
				if (isDown) 
				{
					// Preload the image by adding it to the down-state images array and extend it.
					downStateImages[i] = new Image();
					$(overStateImages[i]).attr("src", imageSource.replace(filetype, downSuffix + filetype));
				}
			}
	    });
	
		// If blur is on, remove the rectangle from around each image.
		this.parent().each(function()
		{
			if (isBlur)
			{
				this.onfocus = function()
				{
					if (this.blur) this.blur();	
				}
			}
		});
	
		// On mouseover, replace the current image with the over-state image.
		this.mouseover(function()
		{
			var imageSource = $(this).attr("src");
			var filetype = imageSource.substring(imageSource.lastIndexOf("."));
	
			if (imageSource.indexOf("-disabled") != -1) return;
			
			$(this).attr("src", imageSource.replace(filetype, overSuffix + filetype));
		});
	
		// On mouseout, replace the current image with the default.
		this.mouseout(function()
		{
			var imageSource = $(this).attr("src");
			var filetype = imageSource.substring(imageSource.lastIndexOf("."));
	
			if (imageSource.indexOf(downSuffix) != -1)
			{
				$(this).attr("src", imageSource.replace(downSuffix + filetype, filetype));
			}
			else
			{
				$(this).attr("src", imageSource.replace(overSuffix + filetype, filetype));
			}
		});
	
		// On mousedown, if the isDown option is true, replace the image with the down-state image.
		this.mousedown(function()
		{
			if (isDown)
			{
				var imageSource = $(this).attr("src");
				var filetype = imageSource.substring(imageSource.lastIndexOf("."));
				$(this).attr("src", imageSource.replace(overSuffix + filetype, downSuffix + filetype));
			}
		});
	
		// On mouse up, if isDown is true, replace the down-state with the over-state image.
		this.mouseup(function()
		{
			if (isDown)
			{
				var imageSource = $(this).attr("src");
				var filetype = imageSource.substring(imageSource.lastIndexOf("."));
				$(this).attr("src", imageSource.replace(downSuffix + filetype, overSuffix + filetype));
			}
		});
		return this
	};
})(jQuery)


