
/*
 * jquery.rolloverify.js
 *
 * Created: 21.03.2011
 * Author: DS @ Chrometoaster
 * Description: 
 * Originally for: WOTS
 * Note: JS Lint shows this error: 'Move the invocation into the parens that contain the function.' 
 * Note: image filename must contain '-off'
 * Usage: $('img[src*=-off.]).rolloverify();
 * Usage: $('.el img').rolloverify(); 
 * Usage: $('.el .submit input').rolloverify();  
 * 
*/

	(function($) 
	{
		$.fn.rolloverify = function(options) 
		{
			// PLUGIN DEFAULTS
			$.fn.rolloverify.defaults = 
			{
				parent: false // parent element || false
			};									
			
			options = $.extend($.fn.rolloverify.defaults,options);			
						
			return this.each(function() {			
				
				// check if already enhanced
				if ( $(this).data('rolloverified') )
				{
					return;
				}			
				
				// function
				$(this)
				.data( 'offSrc', $(this).attr('src') )
				.data( 'overSrc', $(this).attr('src').replace('-off', '-over') );
				
				if ( options.parent )
				{
					$(this).parents( options.parent + ':first')				
					.bind( 'mouseenter focus', 
						function() 
						{ 
							$.img = $(this).find('img:first');						
							$.img.attr( 'src', $.img.data('overSrc') ); 
						}
					)
					.bind( 'mouseleave blur', 
						function() 
						{ 
							$.img = $(this).find('img:first');					
							$.img.attr( 'src', $.img.data('offSrc') ); 
						}
					);
				}
				else
				{
					$(this)				
					.bind( 'mouseenter focus', 
						function() 
						{ 
							$.img = $(this);						
							$.img.attr( 'src', $.img.data('overSrc') ); 
						}
					)
					.bind( 'mouseleave blur', 
						function() 
						{ 
							$.img = $(this);					
							$.img.attr( 'src', $.img.data('offSrc') ); 
						}
					);
				}				

				// preload overstate
				$('<img/>').attr({ src : $(this).data('overSrc') });							
																							
				// mark as done
				$(this).data('rolloverified', true);					

			});			
		};
	})(jQuery);	
