﻿/*  ModalPopup.Javascript
    Developer: Lam Vi Banh(Brian Lam)
    Email: banhthidiem@yahoo.com
    --------------------------------------------------------
    Ex: var NameObject = new ModalPopup("idDisplay", "NameObject");
    NameObject.display(); // Call Modal Popup
    NameObject.hide(); // Hide Modal Popup
*/
function ModalPopup(idContent, myName)
{
	this.disNone = "none";
	this.oContent = null;
	var o = utilObj.getElById(idContent);
	if (o) o.style.display = this.disNone;
	this.myName = myName;
	this.oContainer = null;
	this.idContent = idContent;
	this.parent = null;
	this.isShow = false;
	var self = this;
	utilObj.addEvent(utilObj.wd, "resize", function(e) { self.setCenter(e); });
	utilObj.addEvent(null, "scroll", function (e) { self.setCenter(e); });
	this.createContainer();
};

ModalPopup.prototype.createStyle = function()
{
	var cssStr = ".ModalPopupContainer { background-color:#FFFFFF; filter:alpha(opacity=50); -moz-opacity:.50; opacity:.50; left:0px; position:absolute; z-index:10000; }";
	cssStr += ".ModalPopupChild { background-color:#FFFFFF; border-style:solid; border-width:1px; position:absolute; white-space:nowrap; left:-1000px; top:-1000px; }";
	
	try
	{
		var eStyle = utilObj.getElById("idStyleModalPopup");
		if (eStyle == null)
		{
			eStyle = utilObj.createEl("STYLE");
			eStyle.id = "idStyleModalPopup";
			eStyle.setAttribute("type", "text/css");
			utilObj.d.getElementsByTagName('head')[0].appendChild(eStyle);
			if(eStyle.styleSheet)
			{ 
				eStyle.styleSheet.cssText = cssStr;
			}
			else 
			{
				// w3c
				var cssText = utilObj.createElText(cssStr);
				eStyle.appendChild(cssText);
			}
		}
	}
	catch (e)
	{ }
	
};

ModalPopup.prototype.createContent = function()
{
	try
	{
		var oDivMain = utilObj.createEl("DIV");
		oDivMain.id = this.myName + "ChildContent";
		oDivMain.className = "ModalPopupChild";
		
		var o = utilObj.getElById(this.idContent);
		oDivMain.appendChild(o);
		return oDivMain;
	}
	catch (e)
	{
		return null;
	}
};
ModalPopup.prototype.getParent = function() {
	if (this.parent == null) {
		var o = utilObj.getElById(this.idContent);
		this.parent = o.parentNode;
	}
	return this.parent;
}
ModalPopup.prototype.createContainer = function() {
	try {
		if (!this.oContainer) {
			this.createStyle();
			var oContainer = utilObj.createEl("DIV");
			oContainer.id = this.myName + "Container";
			oContainer.className = "ModalPopupContainer";
			oContainer.style.zIndex = 1000;
			this.oContent = this.createContent();
			var zIndex = oContainer.style.zIndex;
			zIndex = parseInt(zIndex) + 1;
			this.oContent.style.zIndex = zIndex;
			var parent = this.getParent();
			parent.appendChild(this.oContent);
			parent.appendChild(oContainer);
			this.oContainer = oContainer;
		}
	}
	catch (e)
	{ }
};

ModalPopup.prototype.clear = function()
{
	try
	{
		if (this.oContainer != null && this.oContent != null)
		{
			utilObj.d.body.removeChild(this.oContent);
			utilObj.d.body.removeChild(this.oContainer);
		}
	}
	catch (e) 
	{ }
};

ModalPopup.prototype.setCenter = function()
{
	if (this.isShow && this.oContainer && this.oContent)
	{
		utilObj.getElById(this.idContent).style.display = "";
		this.oContainer.style.display = "";
		var de = utilObj.getDocument();
		this.oContainer.style.height = de.clientHeight + "px"; 
		this.oContainer.style.width = de.clientWidth + "px";
		this.oContainer.style.top = utilObj.getDocumentScroll().scrollTop + "px";
		this.oContent.style.display = "";
		var pos = utilObj.getElPosCenterWebPage(this.oContent);
		this.oContent.style.top = pos.Y + "px";
		this.oContent.style.left = pos.X + "px";
		
	}
};

ModalPopup.prototype.display = function()
{
	this.isShow = true;
	this.setCenter();
};

ModalPopup.prototype.hide = function()
{
	this.isShow = false;
	try
	{
		this.oContainer.style.display = this.disNone;
		this.oContent.style.display = this.disNone;
	}
	catch (e)
	{ }
};
