(function($) {
	$.blockui = function(msg, css, opts) {
		$.blockui.impl.install(window, msg, css, opts);
	};
	$.blockui.version = 1.33;
	$.unblockui = function(opts) {
		$.blockui.impl.remove(window, opts);
	};
	$.fn.block = function(msg, css, opts) {
		return this.each(function() {
			if (!this.$pos_checked) {
				if ($.css(this, "position") == 'static')
					this.style.position = 'relative';
				if ($.browser.msie)
					this.style.zoom = 1;
				this.$pos_checked = 1;
			}
			$.blockui.impl.install(this, msg, css, opts);
		});
	};
	$.fn.unblock = function(opts) {
		return this.each(function() {
			$.blockui.impl.remove(this, opts);
		});
	};
	$.fn.displayBox = function(css, fn, isFlash) {
		var msg = this[0];
		if (!msg)
			return;
		var $msg = $(msg);
		css = css || {};
		var w = $msg.width() || $msg.attr('width') || css.width
				|| $.blockui.defaults.displayBoxCSS.width;
		var h = $msg.height() || $msg.attr('height') || css.height
				|| $.blockui.defaults.displayBoxCSS.height;
		if (w[w.length - 1] == '%') {
			var ww = document.documentElement.clientWidth
					|| document.body.clientWidth;
			w = parseInt(w) || 100;
			w = (w * ww) / 100;
		}
		if (h[h.length - 1] == '%') {
			var hh = document.documentElement.clientHeight
					|| document.body.clientHeight;
			h = parseInt(h) || 100;
			h = (h * hh) / 100;
		}
		var ml = '-' + parseInt(w) / 2 + 'px';
		var mt = '-' + parseInt(h) / 2 + 'px';
		var ua = navigator.userAgent.toLowerCase();
		var opts = {
			displayMode : fn || 1,
			noalpha : isFlash && /mac/.test(ua) && /firefox/.test(ua)
		};
		$.blockui.impl.install(window, msg, {
			width : w,
			height : h,
			marginTop : mt,
			marginLeft : ml
		}, opts);
	};
	$.blockui.defaults = {
		pageMessage : '<h1>Please wait...</h1>',
		elementMessage : '',
		overlayCSS : {
			backgroundColor : '#fff',
			opacity : '0.5'
		},
		pageMessageCSS : {
			width : '250px',
			margin : '-50px 0 0 -125px',
			top : '50%',
			left : '50%',
			textAlign : 'center',
			color : '#000',
			backgroundColor : '#fff',
			border : '3px solid #aaa',
			'min-height':'80px'
		},
		elementMessageCSS : {
			width : '250px',
			padding : '10px',
			textAlign : 'center',
			backgroundColor : '#fff'
		},
		displayBoxCSS : {
			width : '400px',
			height : '400px',
			top : '50%',
			left : '50%'
		},
		ie6Stretch : 1,
		allowTabToLeave : 0,
		closeMessage : 'Click to close',
		fadeOut : 1,
		fadeTime : 400
	};
	$.blockui.impl = {
		box : null,
		boxCallback : null,
		pageBlock : null,
		pageBlockEls : [],
		op8 : window.opera && window.opera.version() < 9,
		ie6 : $.browser.msie && /MSIE 6.0/.test(navigator.userAgent),
		install : function(el, msg, css, opts) {
			opts = opts || {};
			this.boxCallback = typeof opts.displayMode == 'function' ? opts.displayMode
					: null;
			this.box = opts.displayMode ? msg : null;
			var full = (el == window);
			var noalpha = this.op8 || $.browser.mozilla
					&& /Linux/.test(navigator.platform);
			if (typeof opts.alphaOverride != 'undefined')
				noalpha = opts.alphaOverride == 0 ? 1 : 0;
			if (full && this.pageBlock)
				this.remove(window, {
					fadeOut : 0
				});
			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;
			if (opts.displayMode)
				var basecss = jQuery.extend({},
						$.blockui.defaults.displayBoxCSS);
			else
				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;"></iframe>')
					: $('<div class="blockui" style="display:none"></div>');
			var w = $('<div class="blockui" style="z-index:1001;cursor:wait;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
			var m = full ? $('<div class="blockui blockMsg" id="poping" style="z-index:1002;cursor:wait;padding:0;position:fixed"></div>')
					: $('<div class="blockui" id="poping" style="display:none;z-index:1002;cursor:wait;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
				});
			if ($.browser.msie)
				f.css('opacity', '0.0');
			//$([ f[0], w[0], m[0] ]).appendTo(full ? 'body' : el);
			//var $container = (full ? 'body' : el)
			$(full ? 'body' : el).append(f[0]).append(w[0]).append(m[0]);
			var expr = $.browser.msie
					&& (!$.boxModel || $('object,embed', full ? null : el).length > 0);
			if (this.ie6 || expr) {
				if (full && $.blockui.defaults.ie6Stretch && $.boxModel)
					$('html,body').css('height', '100%');
				if ((this.ie6 || !$.boxModel) && !full) {
					var t = this.sz(el, 'borderTopWidth'), l = this.sz(el,
							'borderLeftWidth');
					var fixT = t ? '(0 - ' + t + ')' : 0;
					var fixL = l ? '(0 - ' + l + ')' : 0;
				}
				$
						.each(
								[ f, w, m ],
								function(i, o) {
									var s = o[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(
														'width',
														'jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"')
												: s
														.setExpression('width',
																'this.parentNode.offsetWidth + "px"');
										if (fixL)
											s.setExpression('left', fixL);
										if (fixT)
											s.setExpression('top', fixT);
									} else {
										if (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.marginTop = 0;
									}
								});
			}
			if (opts.displayMode) {
				w.css('cursor', 'default').attr('title',
						$.blockui.defaults.closeMessage);
				m.css('cursor', 'default');
				$([ f[0], w[0], m[0] ]).removeClass('blockui').addClass(
						'displayBox');
				$().click($.blockui.impl.boxHandler).bind('keypress',
						$.blockui.impl.boxHandler);
			} else
				this.bind(1, el);
			m.append(msg).show();
			if (msg.jquery)
				msg.show();
			if (opts.displayMode)
				return;
			if (full) {
				this.pageBlock = m[0];
				this.pageBlockEls = $(':input:enabled:visible', this.pageBlock);
				this.abscenter(m[0]);
				setTimeout(this.focus, 20);
			} else
				this.center(m[0]);
		},
		remove : function(el, opts) {
			var o = $.extend({}, $.blockui.defaults, opts);
			this.bind(0, el);
			var full = el == window;
			var els = full ? $('.blockui') : $(
					'.blockui', el);
			if(!els) els=$('.blockui');
			if (full)
				this.pageBlock = this.pageBlockEls = null;
			if (o.fadeOut) {
				els.fadeOut(o.fadeTime, function() {
					if (this.parentNode)
						this.parentNode.removeChild(this);
					else els.remove();
				});
			} else
				els.remove();
		},
		boxRemove : function(el) {
			$().unbind('click', $.blockui.impl.boxHandler).unbind('keypress',
					$.blockui.impl.boxHandler);
			if (this.boxCallback)
				this.boxCallback(this.box);
			$('body .displayBox').hide().remove();
		},
		handler : function(e) {
			if (e.keyCode && e.keyCode == 9) {
				if ($.blockui.impl.pageBlock
						&& !$.blockui.defaults.allowTabToLeave) {
					var els = $.blockui.impl.pageBlockEls;
					var fwd = !e.shiftKey && e.target == els[els.length - 1];
					var back = e.shiftKey && e.target == els[0];
					if (fwd || back) {
						setTimeout(function() {
							$.blockui.impl.focus(back)
						}, 10);
						return false;
					}
				}
			}
			if ($(e.target).parents('div.blockMsg').length > 0)
				return true;
			return $(e.target).parents().children().filter('div.blockui').length == 0;
		},
		boxHandler : function(e) {
			if ((e.keyCode && e.keyCode == 27)
					|| (e.type == 'click' && $(e.target)
							.parents('div.blockMsg').length == 0))
				$.blockui.impl.boxRemove();
			return true;
		},
		bind : function(b, el) {
			var full = el == window;
			if (!b && (full && !this.pageBlock || !full && !el.$blocked))
				return;
			if (!full)
				el.$blocked = b;
			var $e = $(el).find('a,:input');
			$.each([ 'mousedown', 'mouseup', 'keydown', 'keypress', 'click' ],
					function(i, o) {
						$e[b ? 'bind' : 'unbind'](o, $.blockui.impl.handler);
					});
		},
		focus : function(back) {
			if (!$.blockui.impl.pageBlockEls)
				return;
			var e = $.blockui.impl.pageBlockEls[back === true ? $.blockui.impl.pageBlockEls.length - 1
					: 0];
			if (e)
				e.focus();
		},
		center : function(el) {
			var p = el.parentNode, s = el.style;
			var l = ((p.offsetWidth - el.offsetWidth) / 2)
					- this.sz(p, 'borderLeftWidth');
			var t = ((p.offsetHeight - el.offsetHeight) / 2)
					- this.sz(p, 'borderTopWidth');
			s.left = l > 0 ? (l + 'px') : '0';
			s.top = t > 0 ? (t + 'px') : '0';
		},
		abscenter : function(el) {
			$("#blockuititle",el).width($.boxModel?"100%":"99%");
			var doc;
			try{doc = top.document || document;}catch(e){doc = document;}
			var emscrollHeight = doc.body.scrollHeight;
			var emclientHeight = doc.body.clientHeight;
			var emclientWidth = doc.body.clientWidth;
			var emscrollTop = doc.body.scrollTop ? doc.body.scrollTop
					: doc.documentElement.scrollTop;

			//var emtop = (emclientHeight - el.offsetHeight) / 2 + emscrollTop - 54;
			var emtop = (window.screen.height - el.offsetHeight) / 2 + emscrollTop - 100;
			var emleft = (emclientWidth - el.offsetWidth) / 2;

			el.style.left = emleft > 0 ? (emleft + 'px') : '0';
			el.style.top = emtop > 0 ? (emtop + 'px') : '0';
			el.style["margin-top"] = 0;
			el.style["margin-left"] = 0;
			$("#popmaininfo").height(el.offsetHeight>80 ? el.offsetHeight-40:45);
		},
		sz : function(el, p) {
			return parseInt($.css(el, p)) || 0;
		}
	};
})(jQuery);
