/*
    jQuery.imageOver.js v1.0
	Copyright (C) 2011 Daniel Harrison
	Date: Fri Feb 11
	
	Requires:
		- jQuery 1.4.x

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

(function($){  
	$.fn.imageOver = function(options) {

		return this.each(function() { 

			// Internal vars
			var opts = $.extend({}, $.fn.imageOver.defaults, options); 

			
			//Add generic css attributes to parent object
			$(this).css('background-repeat', 'no-repeat');
			$(this).css('background-position', 'top left');
			$(this).css('overflow', 'hidden');
			$(this).css('display', 'inline-block');
			$(this).css('cursor', 'pointer');

			
			
			//Get background image url and css attributes from parent object
			img = extractUrl($(this).css('background-image'));
			height = $(this).css('height');
			width = $(this).css('width');
			overflow = $(this).css('overflow');
			display = $(this).css('display');
			
			
			//Append child object to parent div with opacity of 0
			$(this).append('<div class="imageOver_rollover" style="background-image:url('+img+'); height:'+height+'; width:'+width+'; overflow:'+overflow+'; display:'+display+'; background-repeat:no-repeat; background-position:bottom left;"></div>');

			
			//Set rollover image opacity initially to 0
			$(this).children('.imageOver_rollover').css('opacity', '0');
			
			
			//Hover function
			$(this).hover(function(){
				$(this).children('.imageOver_rollover').stop().animate({"opacity" : "1"}, opts.speed);
				}, function(){$(this).children('.imageOver_rollover').stop().animate({"opacity" : "0"}, opts.speed);
			}); 
			
			
			//Return the URL from a css background image 
			function extractUrl(input){
				return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
			}
		});  
	};  
	
	// plugin defaults - added as a property on our plugin function
	$.fn.imageOver.defaults = {
		speed: 300
	};
})(jQuery);
