/**
 * @author Ry Racherbaumer
 */

//
// create closure
//
(function($) {
	//
	// private variable to hold preloaded images
	//
	var preloadImages = [];
	//
	// plugin definition
	//
 	$.fn.rollover = function(options) {
		// build main options before element iteration
		var opts = $.extend({}, $.fn.rollover.defaults, options);
		// iterate and reformat each matched element
		return this.each(function() {
			// get the jQuery object of the image
			$this = $(this);
			// TODO: implement style changes
			// bind mouseover event to the image
			$this.bind('mouseover', function() {
				// make sure the image doesn't contain the suffix
				if($(this).attr("src").indexOf(opts.suffix) == -1) {
					// create new image source string
					var newSrc = $(this).attr("src").replace(opts.extension, opts.suffix+opts.extension);
					// replace the src attribute of the image
					$(this).attr("src", newSrc);
				}				
			});
			// bind mouseout event to the image
			$this.bind('mouseout', function() {
				// make sure the image's source contains the suffix
				if($(this).attr("src").indexOf(opts.suffix+opts.extension) != -1) {
					// create new image source string
					var oldSrc = $(this).attr("src").replace(opts.suffix+opts.extension, opts.extension);
					// replace the src attribute of the image
					$(this).attr("src", oldSrc);
				}
			});	
			// if the preload option is true, preload the image
			if (opts.preload === true) {
				var current = preloadImages.length;
				preloadImages[current] = $('<img>');
				preloadImages[current].attr('src', $this.attr('src').replace(opts.extension, opts.suffix+opts.extension));
			}
		});
	};
	//
	// plugin defaults
	//
	$.fn.rollover.defaults = {
	 	// file extension to use for images
		extension: '.gif',
		// image name suffix for rollover state
		// i.e. link.gif > link_over.gif
		suffix: '_over',
		// preload rollover image?
		preload: true,
		// CSS to apply on rollover
		rolloverCSS: {},
		// CSS to apply on rollout
		rolloutCSS: {}
	};
//
// end of closure
//
})(jQuery);