(function($) {
	$.blockUI = function(msg, css) { $.blockUI.impl.install(window, msg, css); };
	$.unblockUI = function() { $.blockUI.impl.remove(window); };

	$.fn.block = function(msg, css) {
    return this.each(function() {
		if (!this.$pos_checked) {
            if ($.css(this,"position") == 'static')
                this.style.position = 'relative';
            this.$pos_checked = 1;
        }
        $.blockUI.impl.install(this, msg, css);
    });
	};
	$.fn.unblock = function() {
	    return this.each(function() {
	        $.blockUI.impl.remove(this);
	    });
	};
	$.blockUI.defaults = {
	    pageMessage:    '<h1>Proszę czekać ...</h1>',
	    elementMessage: '', // none
	    overlayCSS:  { backgroundColor: '#bbb', opacity: '0.5' },
	    pageMessageCSS:    { width:'250px', margin:'-50px 0 0 -125px', top:'50%', left:'50%', textAlign:'center', color:'#000', backgroundColor:'#fff', border:'1px solid #000' },
	    elementMessageCSS: { width:'250px', padding:'0px', textAlign:'center', backgroundColor:'#fff'},
	    ie6Stretch: 1
	};

	$.blockUI.impl = {
	    pageBlock: null,
	    op8: window.opera && window.opera.version() < 9,
	    ffLinux: $.browser.mozilla && /Linux/.test(navigator.platform),
	    ie6: $.browser.msie && typeof XMLHttpRequest == 'function',
	    install: function(el, msg, css) {
	        var full = (el == window), noalpha = this.op8 || this.ffLinux;
	        if (full && this.pageBlock) this.remove(window);
	        // check to see if we were only passed the css object (a literal)
	        if (msg && typeof msg == 'object' && !msg.jquery && !msg.nodeType) {
	            css = msg;
	            msg = null;
	        }
	        msg = msg ? (msg.nodeType ? $(msg) : msg) : full ? $.blockUI.defaults.pageMessage : $.blockUI.defaults.elementMessage;
	        var basecss = jQuery.extend({}, full ? $.blockUI.defaults.pageMessageCSS : $.blockUI.defaults.elementMessageCSS);
	        css = jQuery.extend(basecss, css || {});
	        var f = ($.browser.msie) ? $('<iframe class="blockUI" style="z-index:1000;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;document.write(\'\');"></iframe>')
	                           : $('<div class="blockUI" style="display:none"></div>');
	        var w = $('<div class="blockUI" style="z-index:1001;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
	        var m = full ? $('<div class="blockUI blockMsg" style="z-index:1002;padding:0;position:fixed"></div>')
	                     : $('<div class="blockUI" style="display:none;z-index:1002;;position:absolute"></div>');
	        w.css('position', full ? 'fixed' : 'absolute');
	        if (msg) m.css(css);
	        if (!noalpha) w.css($.blockUI.defaults.overlayCSS);
	        if (this.op8) w.css({ width:''+el.clientWidth,height:''+el.clientHeight }); // lame
	        if ($.browser.msie) f.css('opacity','0.0');

	        $([f[0],w[0],m[0]]).appendTo(full ? 'body' : el);
	        if (full) this.pageBlock = m[0];

	        // ie7 needs to use absolute positioning to account for activex issues (when scrolling)
	        var activex = $.browser.msie && $('object,embed', full ? null : el).length > 0
	        if (this.ie6 || activex) {
	            // stretch content area if it's short
	            if (full && $.blockUI.defaults.ie6Stretch && $.boxModel)
	                $('html,body').css('height','100%');

	            // simulate fixed position
	            $.each([f,w,m], function(i) {
	                var s = this[0].style;
	                s.position = 'absolute';
	                if (i < 2) {
	                    full ? s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"') : s.setExpression('height','this.parentNode.offsetHeight + "px"');
	//                    full ? s.setExpression('height','document.body.scrollHeight + "px"') : s.setExpression('height','this.parentNode.offsetHeight + "px"');
	                    full ? s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
	                         : s.setExpression('width','this.parentNode.offsetWidth + "px"');
	                }
	                else {
	                    full ? s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"')
	                         : s.setExpression('top','this.parentNode.top');
	                    s.marginTop = 0;
	                }
	            });
	        }
	        this.bind(1, el);
	        m.append(msg).show();
	        if (msg.jquery) msg.show();
	        full ? setTimeout(this.focus, 200): this.center(m[0]);
	        if (this.op8) this.simulate(true,el);
	    },
	    remove: function(el) {
	        this.bind(0, el);
	        var full = el == window;
	        if (full) {
	            $('body').children().filter('.blockUI').remove();
	            this.pageBlock = null;
	        }
	        else $('.blockUI', el).remove();
	        if (this.op8) this.simulate(false,el);
	    },
	    // event handler to suppress keyboard/mouse events when blocking
	    handler: function(e) {
	        if (e.keyCode && e.keyCode == 9) return true;
	        if ($(e.target).parents('div.blockMsg').length > 0)
	            return true;
	        return $(e.target).parents().children().filter('div.blockUI').length == 0;
	    },
	    // bind/unbind the handler
	    bind: function(b, el) {
	        var full = el == window;
	        // don't bother unbinding if there is nothing to unbind
	        if (!b && (full && !this.pageBlock || !full && !el.$blocked)) return;
	        if (!full) el.$blocked = b;
	        var $e = full ? $() : $(el).find('a,:input');
	        $.each(['mousedown','mouseup','keydown','keypress','keyup','click'], function(i,o) {
	            $e[b?'bind':'unbind'](o, $.blockUI.impl.handler);
	        });
	    },
	    // simulate blocking in opera8
	    simulate: function(dis, el) {
	        var full = el == window;
	        $(':input', full ? 'body' : el).each(function() {
	            if (full && $(this).parents('div.blockMsg').length > 0) return;
	            if (this.$orig_disabled == undefined)
	                this.$orig_disabled = this.disabled;
	            var d = dis || this.$orig_disabled;
	            if (d) this.$orig_disabled = this.disabled;
	            this.disabled = d;
	        });
	    },
	    focus: function() {
	        if (!$.blockUI.impl.pageBlock) return;
	        var v = $(':input:visible:enabled', $.blockUI.impl.pageBlock)[0];
	        if (v) v.focus();
	    },
	    center: function(el) {
			var p = el.parentNode, s = el.style;
	        var l = (this.sz(p,1) - this.sz(el,1))/2, t = (this.sz(p,0) - this.sz(el,0))/2;
	        s.left = l > 0 ? (l+'px') : '0';
	        s.top  = t > 0 ? (t+'px') : '0';
	    },
	    sz: function(el, w) { return parseInt($.css(el,(w?"width":"height"))); }
	};

})(jQuery);