(function ($) {
    $.fn.GSDImgCrossFade = function (options) {
		var defaults = {
			speed: 250,
			captionSpeedUp: 250,
			captionSpeedDown: 500
		};
		var options = $.extend(defaults, options);
		return this.each(function(i){
			
			//Setup image cross fade
			var theLink = $(this);
			var targetImage = theLink.find(":first-child").css('backgroundImage').replace(/^url|[\(\)"']/g, '');
			theLink.prepend('<img>');
			if(!jQuery.browser.msie){
				var thecss = {'position':'absolute','opacity':0}
			}else{
				var thecss = {'position' : 'absolute','visibility':'hidden'}
			}
			theLink.find(":first-child")
			.attr('src', targetImage)
			.css(thecss);
			
			//Setup captions only if they are present
			if(theLink.prev().attr("class")=="cf_desc"){
				var caption = theLink.prev();
				caption.show();
				caption.capHeight = caption.find(":first-child").height() + 10;
			}
			
			//Mouse events
			theLink.bind('mouseenter', function(){
				if(!jQuery.browser.msie){
					$(this).find(":first-child")
					.stop().animate({
						opacity: 1
					}, options.speed);
				}else{
					$(this).find(":first-child").css({'visibility':'visible'});
				}
				//If there are captions, show them
				if(theLink.prev().attr("class")=="cf_desc"){
					caption.show().stop().animate({
						height: caption.capHeight,
						marginTop: -caption.capHeight
					}, options.captionSpeedUp);
				}
			});
			theLink.bind('mouseleave', function(){
				if(!jQuery.browser.msie){
					$(this).find(":first-child")
					.stop().animate({
						opacity: 0
					}, options.speed);
				}else{
					$(this).find(":first-child").css({'visibility':'hidden'});
				}
				//If there are captions, hide them
				if(theLink.prev().attr("class")=="cf_desc"){
					caption.stop().animate({
						height: 20,
						marginTop: 0
					}, options.captionSpeedDown, "", function(){caption.hide();} );
				}
			});					  
		});
    };
})(jQuery);