;(function($) {

    $.fn.extend({
        retheme: function(options,callback) {
            options = $.extend( {}, $.Retheme.defaults, options );
            
            var preLoad = new Image();
            preLoad.src = options.loadingImg;
            
            this.each(function() {
                new $.Retheme(this,options,callback);
            });
            return this;
        }
    });
    
    $.Retheme = function( ctl, options, callback ) {
        if (options.styleSheet.match(/^\w/)) {
            options.styleSheet = '#' + options.styleSheet;
        }
        $(ctl).filter('select').change( function() {
            loadTheme( themeSelector(this), options );
            if (callback) callback.apply(this);
            return false;
        });
        $(ctl).filter(':not(select)').click( function() {
            loadTheme( themeSelector(this), options  );
            if (callback) callback.apply(this);
            return false;
        });
    };
    
    function themeSelector(ctl) {
        var $this = $(ctl);
        var theme = $this.attr('href');
        if (!theme) {
            theme = $this.val();
        }
        return theme;
    }
    
    function loadTheme( theme, options ) {
        var themeHref = options.baseUrl + '/' + theme;
        if (!themeHref.match(/\.css$/)) {
            themeHref += '.css';
        }
        
        var styleSheet = $(options.styleSheet);
        var counter = options.maxTries;
        var bg = options.bgColor;
        if (options.loadingImg) {
            bg += ' url(' + options.loadingImg + ') no-repeat center';
        }
        var speed = options.speed;
        var delay = options.delay;
        
        var $body = $('body');
        var overlay = $('<div id="' + options.overlayID + '"></div>').appendTo($body);
        $body.css( { height: '100%' } );
        overlay.css({
                display: 'none',
                position: 'absolute',
                top:0,
                left: 0,
                width: '1px',
                height: '1px',
                zIndex: options.zIndex,
                background: bg
        })
        .stop( true );
        
        if (options.loadingImg) {
            overlay.fadeIn( speed, function() {
                styleSheet.attr( 'href', themeHref );
                $.get( themeHref, function() {  // basically load it twice, but that will make sure it's been applied before we reveal
                    setTimeout( function() {
                        overlay.fadeOut( speed, function() {
                            $(this).remove();
                        });
                    }, delay );
                });
            });
        }
        else {
            overlay.show( speed, function() {
                styleSheet.attr( 'href', themeHref );
                $.get( themeHref, function() {  // basically load it twice, but that will make sure it's been applied before we reveal
                    setTimeout( function() {
                        overlay.slideUp( speed, function() {
                            $(this).remove();
                        });
                    }, delay );
                });
            });
        }
    };
      
    $.Retheme.defaults = {
        loadingImg: null,
        bgColor: 'black',
        overlayID: 'reThemeOverlay',
        baseUrl: 'modules/effets/css/',
        styleSheet: 'theme',
        zIndex: 32767,
        speed: 'slow',
        delay: 0
    };
    
})(jQuery);
