// privatize context and provide $ shortcut 
(function($){
// prepare to use John's Metadata plugin 
if($.meta){
	$.meta.type = 'class';
	$.meta.single = 'metadata';
}

/**
 * Will cause all image tags or input[@type="image"] elements inside of the selected
 * elements to be made into rollovers if their source contains the string "_off" just
 * before the image extension.
 *
 * @example $('div.rollovers').rollover();
 * @before <div class="rollover"><img src="products_off.gif"/></div>
 * @after //when cursor is hovering over the image
 *        <div class="rollover"><img src="products_on.gif"/></div>
 * @desc Will cause all <img/> or <input type="image"/> tags with an src attrib which contains
 *       "_off" to have mouseover and mouseout handlers added that will change the image to
 *       replace "_off" with "_on" in the image name. The "_off" has to be right before the
 *       image extension.
 *
 * @param Object containing values to override defaults
 *
 * @type jQuery
 * @cat Plugins/Rollovers
 * @author Paul McLanahan <paul dot mclanahan at diginsite>
 */
$.fn.rollovers = function(opts){
	var settings = $.extend($.extend({},arguments.callee.defaults),opts || {});
	return this.each(function(){
		var root = this;
		root.opts = $.extend($.extend({},settings),root.metadata||{});
		$('img,input[@type="image"]',this).filter('[@src*="'+root.opts.off+'."]').each(function(){
			el = this;
			// using Image objects for both so IE will preload correctly.
			el.overObj = new Image();
			el.outObj = new Image();
			el.outObj.src = el.src;
			el.overObj.src = el.src.replace(new RegExp(root.opts.off+"\.([a-z]{3,4})$",'i'),root.opts.on+".$1");
		})
		.hover(
			function(){ // mouseover
				this.src = this.overObj.src;
			},
			function(){ // mouseout
				this.src = this.outObj.src;
			}
		);
	});
};

// default options
$.fn.rollovers.defaults = {
	off : '_off', // string to look for in the src attrib
	on : '_on' // string to replace the off setting with in the src attrib
}

/* uncomment to apply when the dom is ready */
$(function(){
	$('.rollover').rollovers();
});

})(jQuery);