jQuery.fn.styleSwitcher = function(){
    $(this).click(function(){
        loadStyleSheet(this);
        return false;
    });
    function loadStyleSheet(obj) {
        $('body').append('<div id="overlay" />');
        $('body').css({height:'100%'});
        $('#overlay')
            .css({
                display: 'none',
                position: 'absolute',
                top:0,
                left: 0,
                width: '100%',
                height: '100%',
                zIndex: 1000,
                background: 'black url(images/loading.gif) no-repeat center'
            })
            .fadeIn(500,function(){
                // ajax request to style-switcher.php file. '&js' GET-Var is set to identify call from jquery. sw-file echos $style which is passed to callbackfunction as 'data'
                $.get( obj.href+'&js',function(data){
                    $('#stylesheet').attr('href','css/base_' + data + '.css');
                    // 'id' of active menu li-item is grabbed, because every site has it's own background image 
                    var idName = $('a.active').attr('id');
                    // set background image, url made of 'data' (styletheme) and 'idName' of the active element                 
                    $('body').css('background','black url(images/background/bg_' + data + '_' + idName + '.jpg) top left no-repeat');
                    $('#wrapper').css('background','transparent url(images/background/bg_' + data + '_' + idName + '.jpg) top left no-repeat');
                    cssDummy.check(function(){
                        $('#overlay').fadeOut(500,function(){
                            $(this).remove(); 
                        }); 
                    });
                });
                
            });
    }
    var cssDummy = {
        init: function(){
            $('<div id="dummy-element" style="display:none" />').appendTo('body');
        },
        check: function(callback) {
            if ($('#dummy-element').width()==2) callback();
            else setTimeout(function(){cssDummy.check(callback)}, 200);
        }
    }
    cssDummy.init();
}