﻿/*  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.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; }";
	cssStr += ".ModalPopupChild { background-color:#FFFFFF; border-style:solid; border-width:1px; white-space:nowrap; }";
	
	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.className = "ModalPopupChild";
		
		var o = utilObj.getElById(this.idContent);
		oDivMain.appendChild(o);
		return oDivMain;
	}
	catch (e)
	{
		return null;
	}
};

ModalPopup.prototype.createContainer = function() {
	try {
		if (!this.oContainer) {
			this.createStyle();
			var oContainer = utilObj.createEl("DIV");
			oContainer.className = "ModalPopupContainer";
			oContainer.style.zIndex = 1000;
			this.oContainer = oContainer;
			this.oContent = this.createContent();
			this.oContent.style.zIndex = 1001;
			utilObj.addChildToBody(this.oContent);
			utilObj.addChildToBody(oContainer);
			this.hide();
		}
	}
	catch (e)
	{ }
};

ModalPopup.prototype.clear = function()
{
	try
	{
		if (this.oContainer != null && this.oContent != null)
		{
			document.body.removeChild(this.oContent);
			document.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 = "";
		this.oContent.style.display = "";
		var de = utilObj.getDocument();
		this.oContainer.style.height = de.clientHeight + "px";
		this.oContainer.style.width = de.clientWidth + "px";

		if (utilObj.isIE && utilObj.appVersion < 7) {
			this.oContainer.style.position = "absolute";
			this.oContainer.style.top = utilObj.getDocumentScroll().scrollTop + "px";

			this.oContent.style.position = "absolute";
			var pos = utilObj.getElPosCenterWebPage(this.oContent);
			this.oContent.style.top = pos.Y + "px";
			this.oContent.style.left = pos.X + "px";
		}
		else {
			this.oContainer.style.position = "fixed";
			this.oContainer.style.top =
			this.oContainer.style.left = "0px";

			this.oContent.style.position = "fixed";
			var elSize = utilObj.getElementSize(this.oContent);
			this.oContent.style.top = ((de.clientHeight - elSize.height) / 2) + "px";
			this.oContent.style.left = ((de.clientWidth - elSize.width) / 2) + "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;
		this.oContainer.style.top =
		this.oContainer.style.left = 
		this.oContent.style.top = 
		this.oContent.style.left = "-1000px";
	}
	catch (e)
	{ }
};