﻿/*--
标题：弹出框
说明：弹出框
设计：王集鹄
博客：http://blog.csdn.net/zswang
日期：2008年11月9日
--*/

if (!Common.scripts["PopupBox"]) {
	loadCss("/Scripts/PopupBox.css?version=2010040501");
	Common.scripts["PopupBox"] = true;
}

/// <summary>构造弹出框</summary>
/// <param name="parent">容器</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
function PopupBox(parent, width, height) {
	this.div_box = document.createElement("div");
	this.div_box.className = "PopupBox";
	this.div_box.style.display = "none";
	this.div_box.style.position = "absolute";
	if (width) this.div_box.style.width = width + "px";
	if (height) this.div_box.style.height = height + "px";
	this.visible = false;
	this.parent = typeof parent == "object" ? parent : document.body;
	this.width = width;
	this.height = height;
	this.engine = new PopupEngine(this.div_box);
	var self = this;
	this.engineClose = function () {
		self.visible = false;
		if (typeof self.onclose == "function") self.onclose(self);
	}
	this.engine.onclose = this.engineClose;
	this.parent.appendChild(this.div_box);
}

PopupBox.prototype.close = function() {
	if (!this.visible) return;
	this.visible = false;
	this.engine.close();
	if (typeof this.onclose == "function") this.onclose(this);
}

PopupBox.prototype.popup = function(x, y, element, anchor) {
	if (this.visible) return;
	this.visible = true;
	if (typeof element == "object") {
		var point = absolutePoint(element);
		x += point.x + ((/right/i).test(anchor) ? element.offsetWidth : 0)
		y += point.y + ((/bottom/i).test(anchor) ? element.offsetHeight : 0);
	}
	this.engine.popup(x, y);
	if (typeof this.onpopup == "function") this.onpopup(this);
}
	
/// <summary>
/// 释放弹出框
/// </summary>
PopupBox.prototype.dispose = function() {
	this.disposed = true;
	for (var i in this) {
		if (this[i].disposed) continue;
		if (typeof this[i].dispose == "function") this[i].dispose();
		if (typeof this[i].parentNode == "object")
			this[i].parentNode.removeChild(this[i]);
		delete this[i];
	}
}