/**
 *
 * File: 		jquery.rollover.js
 *
 * Abstract:	Binds event handlers to images for rollover effects.
 *				Preloads all images.
 *
 * Notes:		Should be called on a jQuery wrapped set of elements.
 *				Supports the following elements:
 *					- an IMG node,
 *					- an INPUT node of type 'image',
 *					- an A or DIV node containing either of the above.
 *
 *				Image names must end with _over.* and _off.* to qualify for swapping,
 *				(ex. "btn-home_off.gif" triggers "btn-home_over.gif" on mouseover).
 *
 * TODO:		Add an options hash parameter, allow configurable suffixes.
 *
 *
**/
(function ($) {
	$.fn.Rollover = function () {

		return this.each(function () {
		
			// Change image(s) on mouseover
			$(this).bind("mouseover", function () {
				var elems;
				var overSrc;

				// find images that need to swapped
				if (this.tagName === "A" || this.tagName === "DIV") {
					elems = $(this).find("img[src*='_off.']"); // for A tags look for suitable child images
				} else {
					elems = $(this);
				}
		
				// do the swap
				elems.each(function () {
					overSrc = this.src.replace(/\_off\./, "_on.");
					this.src = overSrc;
				});
			});
			
			// Reset image(s) on mouseout
			$(this).bind("mouseout", function () {
				var elems;
				var overSrc;
				
				// find images that need to swapped
				if (this.tagName === "A" || this.tagName === "DIV") {
					elems = $(this).find("img[src*='_on.']"); // for A tags look for suitable child images
				} else {
					elems = $(this);
				}
				
				// do the swap
				elems.each(function () {
					overSrc = this.src.replace(/\_on\./, "_off.");
					this.src = overSrc;
				});
			});
			
			// Preload the images
			if (this.tagName === "A" || this.tagName === "DIV") {
				elems = $(this).find("img[src*='_off.']"); // for A tags look for suitable child images
			} else {
				elems = $(this);
			}
			elems.each(function () {
				preloadSrc = this.src.replace(/\_off\./, '_on.');
				preloadObj = new Image();
				preloadObj.src = preloadSrc;
			});
		});
	};
})(jQuery);