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/styleswitcher/img/loading.gif) no-repeat center'
			})
			.fadeIn(500, function () {


			    //obj = <a id="style_switcher_link" href="#only_text">

			    var cssFileName = $(obj).attr('href').replace("#", "");
			    if (cssFileName == "only_text") {
			        $(obj).attr('href', "#graphics");
			        $(obj).text("wersja graficzna");
			    }
			    else {
			        $(obj).attr('href', "#only_text");
			        $(obj).text("wersja tekstowa");
			    }

			    $.cookie('css_file_name', cssFileName, { path: '/', expires: 7 });

			    var oldPath = $('#stylesheet').attr('href');

			    var exploded = oldPath.split('/');
			    var newPath = oldPath.replace(exploded[exploded.length - 1], cssFileName + ".css");
			    $('#stylesheet').attr("href", newPath);

			    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();
}
