if (window.top != window && top.Dialog) {
	Dialog = window.top.Dialog;
}else{
	function Dialog(option) {

		var defaultOpt = {
			modal: true
		};

		this.ele = $(Dialog.TEMPLATES.DIALOG);
		this.dialogBg = $(Dialog.TEMPLATES.DIALOGBG);
		this.opt = $.extend(defaultOpt, option);

		this.init();

		this.show();
	}

	Dialog.prototype = {
		init: function() {
			this._initDOM();
			this._resize();	
			this._addEvents();   
		},

		_initDOM: function() {
			if(this.opt.msg) this.ele.find('.msg').text(this.opt.msg);
			this.ele.appendTo($(document.body));
		},
		// 插入覆盖页面的背景
		_insertBg: function() {
			this.ele.before(this.dialogBg.show());
		},
		// 移除覆盖页面的背景
		_removeBg: function() {
			this.dialogBg.hide();
		},
		_resize: function() {
			var width = document.documentElement.clientWidth;
			var height = document.documentElement.clientHeight;

			var ele = this.ele;

			var eleWidth = ele.width();
			var eleHeight = ele.height();

			// 弹出层居中 
			ele.css('left', (width - eleWidth)/2 + 'px')
			   .css('top', (height - eleHeight)/2 + 'px');
		},
		_addEvents: function() {

			var ele = this.ele;

			ele.find('.close, .cancel').click($.proxy(function() {
				this.hide();
				this.opt.callback && this.opt.callback(false);
				// if(this.opt.callback.cancel) this.opt.callback.cancel.call(this);
			}, this));

			ele.find('.confirm').click($.proxy( function() {
				this.hide();
				this.opt.callback && this.opt.callback(true);
				// if(this.opt.callback.confirm) this.opt.callback.confirm.call(this);
			}, this));

		},

		show: function() {
			this.ele.show();
			if(this.opt.modal) this._insertBg();
			return this;
		},
		hide: function() {
			this.ele.hide();
			if(this.opt.modal) this._removeBg();
			return this;
		}
	};

	Dialog.TEMPLATES = {
		DIALOG: '<div class="dialog">'+
			'<div class="header">'+
				'<a class="close" href="javascript:;">X</a>'+
			'</div>'+
			'<div class="content">'+
				'<p>'+
					'<span class="content-tip"></span>'+
					'<span class="msg"></span>'+
				'</p>'+
			'</div>'+
			'<div class="footer">'+
				'<input type="button" value="取消" class="cancel">'+
				'<input type="button" value="确定" class="confirm">'+
			'</div>'+
		'</div>',
		DIALOGBG: '<div class="dialog-bg"></div>'
	}

}
window.alert = function(str){
	new Dialog({
		msg: str
	});
};
window.confirm = function(str,cb){
	new Dialog({
		msg: str,
		callback: cb
	});
};