var Modalbox = {
	BUTTON_CLOSE: {text: "关闭", onclick: function() {Modalbox.close()}},

	initialize: function() {
		this.W = document.createElement("div");
		this.W.id = "MB_Container";
		this.W.style.display = "none";
		this.T = document.createElement("div");
		this.T.id = "MB_Title";
		this.W.appendChild($(this.T));
		this.F = document.createElement("div");
		this.F.id = "MB_Frame";
		this.W.appendChild($(this.F));
		this.I = document.createElement("div");
		this.I.id = "MB_Icon";
		this.F.appendChild($(this.I));
		this.M = document.createElement("div");
		this.M.id = "MB_Message";
		this.F.appendChild($(this.M));
		this.B = document.createElement("div");
		this.B.id = "MB_Buttons";
		this.F.appendChild($(this.B));
		this.insertDom($(this.W));

		this.S = document.createElement("div");
		this.S.id = "MB_Spinner";
		this.S.style.display = "none";
		this.S.innerHTML = "<img src='/images/spinner.gif'/>";
		this.insertDom($(this.S));

		this.O = document.createElement("div");
		this.O.id = "MB_Overlay";
		this.O.style.display = "none";
		this.insertDom($(this.O));
	},

	insertDom: function(d) {
		document.body.firstChild ? document.body.insertBefore(d, document.body.firstChild) : document.body.appendChild(d);
	},

	show: function(title, icon, message, buttons) {
		if (this.O.style.display != "") {
			this.O.show();
			new Effect.Opacity(this.O, { from: 0.0, to: 0.5, duration: 0.2 });
		} else {
			this.S.hide();
		}
		this.W.appear({ duration: 0.2, queue: {position: "end", scope: "Modalbox.W"}, afterSetup: function() {
			this.T.update(title);
			if (icon) {
				this.I.show();
				this.I.update(icon);
			} else {
				this.I.hide();
				this.I.update("");
			}
			if (message) {
				this.M.show();
				this.M.update(message);
			} else {
				this.M.hide();
				this.M.update("");
			}
			this.B.update("");
			if (buttons.length > 0) {
				this.B.show();
				var bc = "";
				$A(buttons).each(function(opt) {
					var i = document.createElement("input");
					i.type = "button";
					i.value = opt.text;
					i.onclick = opt.onclick;
					Modalbox.B.appendChild(i);
				});
			} else {
				this.B.hide();
			}

			this.O.style.width = document.documentElement.scrollWidth + "px";
			this.O.style.height = document.documentElement.scrollHeight + "px";

			var d = this.W.getDimensions();
			this.W.style.left = ((document.documentElement.clientWidth - d.width) / 2) + "px";
			this.W.style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight - d.height) / 2) + "px";
		}.bind(this) });
	},

	showOk: function(title, message, buttons) {
		this.show(title, "<img src='/images/gou.gif'/>", message, buttons);
	},

	showNotice: function(title, message, buttons) {
		this.show(title, "<img src='/images/deng.gif'/>", message, buttons);
	},

	showError: function(title, message, buttons) {
		this.show(title, "<img src='/images/cha.gif'/>", message, buttons);
	},

	showConfirm: function(title, message, buttons) {
		this.show(title, "<img src='/images/wen.gif'/>", message, buttons);
	},

	showWaiting: function() {
		this.W.fade({ duration: 0.2, queue: {position: "end", scope: "Modalbox.W"} });

		this.O.style.width = document.documentElement.scrollWidth + "px";
		this.O.style.height = document.documentElement.scrollHeight + "px";

		var d = this.S.getDimensions();
		this.S.style.left = ((document.documentElement.clientWidth - d.width) / 2) + "px";
		this.S.style.top = (document.documentElement.scrollTop + (document.documentElement.clientHeight - d.height) / 2) + "px";
		this.S.show();

		if (this.O.style.display != "") {
			this.O.show();
			new Effect.Opacity(this.O, { from: 0.0, to: 0.5, duration: 0.2 });
		}
	},

	close: function() {
		this.S.hide();
		this.W.fade({duration: 0.2});
		new Effect.Opacity(this.O, { to: 0.0, duration: 0.2, afterFinish: this.O.hide.bind(this.O) });
	}
};

Event.observe(window, "load", function() {
	Modalbox.initialize();
});
