$.ajaxSetup({
	global: true,
	cache:false,
	beforeSend:function(){
		$.Waiting.waiting();
	},
	success: function(){
		$.Waiting.destroy();
	},
	complete:function(){
		$.Waiting.destroy();
	},
	error:function() {
		//$.Waiting.destroy();
	}
});

(function($){
$.Waiting = {
//$.extend($.Waiting,{
	waiting:function(){
		if(this.waitdlg){
			return;
		}
		this.waitdlg = $("<div><div class='please'>请稍候</div></div>")
						.addClass("wait")
						.css({
							display:"none",
							position: 'absolute',
							zIndex:"1000"
						})
						.appendTo("body");
			
//		this.modaldlg = $('<div></div>').appendTo(document.body)
//			.addClass('ui-widget-overlay')
//			.css({
//				width: this.width(),
//				height: this.height(),
//				zIndex:"999",
//				display:"none"
//			});
		this.show();
//		$(window).bind('resize.dialog-overlay', $.Waiting.resize);
	},
	
	show:function(){
		this._position('center');
		this.waitdlg.css("display","");
//		this.modaldlg.css("display","");
//		this.waitdlg.css("z-index","4444")
	},
	
	destroy:function(){
		if(this.waitdlg){
			this.waitdlg.remove();
			this.waitdlg=undefined;
		}
//		this.modaldlg.remove();
	},
	
	_position: function(pos) {
		var wnd = $(window), doc = $(document),
			pTop = doc.scrollTop(), pLeft = doc.scrollLeft(),
			minTop = pTop;

		if ($.inArray(pos, ['center','top','right','bottom','left']) >= 0) {
			pos = [
				pos == 'right' || pos == 'left' ? pos : 'center',
				pos == 'top' || pos == 'bottom' ? pos : 'middle'
			];
		}
		if (pos.constructor != Array) {
			pos = ['center', 'middle'];
		}
		if (pos[0].constructor == Number) {
			pLeft += pos[0];
		} else {
			switch (pos[0]) {
				case 'left':
					pLeft += 0;
					break;
				case 'right':
					pLeft += wnd.width() - this.waitdlg.outerWidth();
					break;
				default:
				case 'center':
					pLeft += (wnd.width() - this.waitdlg.outerWidth()) / 2;
			}
		}
		if (pos[1].constructor == Number) {
			pTop += pos[1];
		} else {
			switch (pos[1]) {
				case 'top':
					pTop += 0;
					break;
				case 'bottom':
					pTop += wnd.height() - this.waitdlg.outerHeight();
					break;
				default:
				case 'middle':
					pTop += (wnd.height() - this.waitdlg.outerHeight()) / 2;
			}
		}

		// prevent the dialog from being too high (make sure the titlebar
		// is accessible)
		pTop = Math.max(pTop, minTop);
		this.waitdlg.css({top: pTop, left: pLeft});
	},
	
	height: function() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollHeight = Math.max(
				document.documentElement.scrollHeight,
				document.body.scrollHeight
			);
			var offsetHeight = Math.max(
				document.documentElement.offsetHeight,
				document.body.offsetHeight
			);

			if (scrollHeight < offsetHeight) {
				return $(window).height() + 'px';
			} else {
				return scrollHeight + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).height() + 'px';
		}
	},

	width: function() {
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
			var offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

			if (scrollWidth < offsetWidth) {
				return $(window).width() + 'px';
			} else {
				return scrollWidth + 'px';
			}
		// handle "good" browsers
		} else {
			return $(document).width() + 'px';
		}
	},
	
	resize: function() {
		var that = $.Waiting;
		that.modaldlg.css({
			width: 0,
			height: 0
		}).css({
			width: that.width(),
			height: that.height()
		});
	}
}
})(jQuery);
