jQuery.extend({
    BgImageTransitions: []
});

jQuery.fn.extend({
    BgImageTransition: function(src, options) {
        if( !src ){ return jQuery; }

        //copy css from the element to the helper element function
        function copyCSS( from, to ){
            jQuery(['']).each( function(i,v){
                jQuery(to).css( v, jQuery(from).css( v ) );
            });
        }

        //make sure there is a zIndex set - we will the helperElement to be *above* the original one
        if( !this.css('zIndex') ){
            this.css('zIndex',1);
        }

        //default plugin settings
        var settings = jQuery.extend({
            effect: {opacity: 'toggle'},
            duration: 'slow',
            easing: 'linear',
            callback: function(){},
            helperElementId: this.attr('id')+'2',
            zindex: parseInt(this.css('zIndex'),10)+1
        }, options);

        //check the bgImageTransition array and see whether there is already a helperElement for the original one. Generate one, if not
        var helperElement = null;
        if( !jQuery.BgImageTransitions[this.attr('id')] ){
            helperElement = this.clone();
            copyCSS( this, helperElement );
            helperElement.css('zIndex', settings.zindex);
            helperElement.css('display', 'none');
			helperElement.css('background-repeat', 'no-repeat');
			helperElement.css('background-position', 'center');
			helperElement.css('width', '100%');
			helperElement.css('height', '331');
			helperElement.css('position', 'absolute');
            helperElement.attr('id', settings.helperElementId );
            helperElement.insertAfter(this);
            jQuery.BgImageTransitions[this.attr('id')] = helperElement;
        }
        else{
            helperElement = jQuery.BgImageTransitions[this.attr('id')];
        }

        //load the image file into cache first, so that we get a nice and fast load. Make the transition when the image has been loaded in cache
        var tempImage = new Image();

        jQuery(tempImage).load( function(){
            var newImage = ( helperElement.css('display') == 'block' ) ? jQuery(this) : jQuery(helperElement);
            newImage.css('backgroundImage', 'url('+tempImage.src+')');
            helperElement.animate( settings.effect, settings.duration, settings.easing, settings.callback );
        });

        tempImage.src = src;

        return jQuery;
    }
});