/**
 * jQuery imageFDHover plugin
 * This jQuery plugin was inspired on several projects executed for Larmit Grafisch Bureau (http://www.larmit.nl/)
 *
 * @name jquery-imageFDHover-0.1.js
 * @author Jeroen Blox - http://www.jeroenblox.nl
 * @version 0.1
 * @date September 28, 2010
 * @category jQuery plugin
 * @copyright (c) 2010 Jeroen Blox - http://www.jeroenblox.nl
 * @license CCAttribution-ShareAlike 2.5 Brazil - http://creativecommons.org/licenses/by-sa/2.5/br/deed.nl
 * @example Visit http://www.futuredezign.nl/scripts/imageFDHover/ for an example
 */
$.fn.imageFDHover = function(options) {  
	// Default variables
	var defaults = {
		defaultImg: '-01',
		hoverImg: '-02',
		activeImg: '-03',
		activeClass: 'activeImg',
		preload: true
		};
	var options = $.extend(defaults, options);

	// Mouse enter function
	function _mouse_enter(img)
	{
		// Unbind moueenter, till mouseleave has been executed
		img.unbind('mouseenter');
		
		// Get hover image location
		current_src = img.attr('src');
		hover_src = current_src.replace(options.defaultImg, options.hoverImg);
		
		// Bind mouseleave and change image
		img.bind('mouseleave', function() { _mouse_leave($(this));});
		img.attr('src', hover_src);
	}

	// Mouse leave function
	function _mouse_leave(img)
	{	
		// Unbind mouseleave, till mouseenter has been executed
		img.unbind('mouseleave');
		
		// Get default image location
		current_src = img.attr('src');
		default_src = current_src.replace(options.hoverImg, options.defaultImg);
		
		// Bind mouseenter and change image
		img.bind('mouseenter', function() {  _mouse_enter($(this)) } );
		img.attr('src', default_src);
	}
	
	return this.each(function() {  
		obj = $(this);
		
		// Change cursor to pointer
		obj.css('cursor','pointer');
		
		// Active images don't need an hover effect
		if(obj.hasClass(options.activeClass))
		{
			// Get active image location
			current_src = obj.attr('src');
			active_src = current_src.replace(options.defaultImg, options.activeImg);
			
			// Change default image to active image
			obj.attr('src', active_src);
		}
		else
		{
			// Bind mouseenter to image
			obj.bind('mouseenter', function() {  _mouse_enter($(this)) } );

			// Preload all hover images
			if(options.preload == true)
			{
				jQuery("<img>").attr("src", obj.attr('src').replace(options.defaultImg, options.hoverImg));
			}

		}
	});  
};
