$.extend({
	dialog : function (options){
		$('#dialog').dialog('close');
		$('.ui-dialog').remove();
		if(typeof options == 'string') options = {prepend: options};
		param = $.extend({url: null, o: null, prepend: null, append: null, cb: function (){},speed: 300}, options || {});
		if(param.modal && /msie 6/i.test(navigator.userAgent)) {
			param.overlay = null;
		}
		if(!$('#dialog').length) $('body').append('<div id="dialog" style="display:none;"><div></div></div>');
		if(param.url) {
			$.loading();
			return $('#dialog>div').load(param.url, $.show);
		}
		if(param.o) {
			$('#dialog>div').html(param.o.html());
		}
		$.show();
	},
	show : function () {
		if(param.prepend) $('#dialog>div').prepend(param.prepend);
		if(param.append) $('#dialog>div').append(param.append);
		$('#dialog').dialog(param).show();
		$('.jqMsg').remove();
		param.cb.call();
		setTimeout($.autoSize, 10);
		$(window).bind('resize', $('#dialog').parent().parent(), $.autoCenter);
	},
	pageSize : function (){
		var xScroll,yScroll;if(window.innerHeight&&window.scrollMaxY){
			xScroll=window.innerWidth+window.scrollMaxX;
			yScroll=window.innerHeight+window.scrollMaxY;
		}else if(document.body.scrollHeight>document.body.offsetHeight){
			xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;
		}else{
			xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;
		}
		var windowWidth,windowHeight;
		if(self.innerHeight){
			if(document.documentElement.clientWidth){
				windowWidth=document.documentElement.clientWidth;
			}else{
				windowWidth=self.innerWidth;
			}
			windowHeight=self.innerHeight;
		}else if(document.documentElement&&document.documentElement.clientHeight){
			windowWidth=document.documentElement.clientWidth;
			windowHeight=document.documentElement.clientHeight;
		}else if(document.body){
			windowWidth=document.body.clientWidth;
			windowHeight=document.body.clientHeight;
		}
		if(yScroll<windowHeight){
			pageHeight=windowHeight;
		}else{
			pageHeight=yScroll;
		}
		if(xScroll<windowWidth){
			pageWidth=xScroll;
		}else{
			pageWidth=windowWidth;
		}
		return {X:pageWidth, Y:pageHeight, x:windowWidth, y:windowHeight};
	},
	scroll :  function (){
		var xScroll,yScroll;
		if(self.pageYOffset){
			yScroll=self.pageYOffset;xScroll=self.pageXOffset;
		}else if(document.documentElement&&document.documentElement.scrollTop){
			yScroll=document.documentElement.scrollTop;xScroll=document.documentElement.scrollLeft;
		}else if(document.body){
			yScroll=document.body.scrollTop;
			xScroll=document.body.scrollLeft;
		}
		arrayPageScroll=new Array(xScroll,yScroll);
		return {x:xScroll, y:yScroll};
	},
	loading : function (){
		$.tip('loading...', 999999);	
		//esc to close
		$(document).one('keydown', $.esc);
	},
	esc : function (e){
		if (e.keyCode == 27) {
			if(e.data) {
				location.href = e.data;
			}
			$('.jqMsg').remove();
			return false;
		}
	},
	tip : function(str, time, url){
		if(typeof ti != 'undefined') {
			clearInterval(ti);
		}
		if(!$('.jqMsg').length) {
			$('body').append('<div class="jqMsg" style="z-index: 1500;padding: 20px 40px;position: fixed;text-align:center;background: #F8FFE8;line-height: 20px;color: #E00;display: none;border: 1px solid #5AB500"></div>');
		}
		if(typeof time != 'number') time = 2000;
		var pageSize = $.pageSize();
		var scroll = $.scroll();
		
		var leng = 270 + (str.length - 100) / 1;
		if(leng > pageSize.x * 0.8) leng = pageSize.x * 0.8;
		$('.jqMsg').width(leng).html(str).show();
		if($('.jqMsg').height() > 20) $('.jqMsg').css('text-align', 'left')
		
		var top = (pageSize.y - $('.jqMsg').height()-40)/2;
		var left = (pageSize.x - $('.jqMsg').width() - 70)/2;
		if(/msie 6/i.test(navigator.userAgent)) {
			$('.jqMsg').css('position', 'absolute');
			top += scroll.y;
			left += scroll.x;
		}
		$('.jqMsg').css('top', top);
		$('.jqMsg').css('left', left);
		var endTime = +new Date + time;
		if(time != 0) ti = setInterval("$.timing("+endTime+")", 1000);
		if(url && time > 0) setTimeout("location.href='"+url+"'", time);
		$(document).one('keydown', url, $.esc);
		$(window).bind('resize', $('.jqMsg'), $.autoCenter);
	},
	autoCenter : function (e){
		var dialog = e.data;
		if(!dialog.width()) return;
		var pageSize = $.pageSize();
		var scroll = $.scroll(); 
		var left = (pageSize.x - dialog.width()) /2 + scroll.x - 35;
		var top = (pageSize.y - dialog.height()) /2 + scroll.y - 20;
		if(left < scroll.x) left = scroll.x;
		if(top < scroll.y) top = scroll.y;
		dialog.css('left', left);
		dialog.css('top', top);
	},
	//dialog autosize
	autoSize : function ()
	{
		if(param.width && param.height) return;
		var son = false;
		if($('#dialog>div').length) {
			var son = $('#dialog>div');
		}
		var scroll = $.scroll();
		var p = $('#dialog').parent().parent();
		var size = {};
		if(!param.width) {
			var w = p.width();
			var W = son.width() + 50;
			var left = parseInt(p.css('left')) - (W - w) / 2;
			if(left < scroll.x) left = scroll.x;
			size.width = W;
			size.left = left;
		}
		if(!param.height) {
			var h = p.height();
			var H = son.height() + 70;
			var top = parseInt(p.css('top')) - (H - h) / 2;
			if(top < scroll.y) top = scroll.y;
			size.height = H;
			size.top = top;
		}
		p.animate(size, param.speed);
	},
	timing : function (endTime){
		if(+new Date > endTime) {
			$('.jqMsg').remove();
			clearInterval(ti);
		}
	}
});