﻿var g_mb;
function ComitMagicBox() {
	this.refMagicBox = null;
	this.refBlackBox = null;
	this.refInnerBlackBox = null;
	this.active = false;
	this.currentOwner = false;
	this.popupMagicBox = function(showThis, owner) {
		if (this.active) { this.removeMagicBox() }
		if (!showThis) {
			return false;
		}
		if (!this.refBlackBox) {
			var targetForm = mGetNodes(new oGetNodes("form", 0, 0, 0, 0))[0];
			this.refBlackBox = this.createBlackBox(targetForm);
		}
		if (!this.refMagicBox) { this.refMagicBox = this.createMagicBox(this.refBlackBox, showThis); };
		this.currentOwner = owner;
		this.active = true;
		return true;
	}
	this.removeMagicBox = function(owner) {
		if (this.active == false || owner != this.currentOwner) { return };
		this.active = false;
		this.refBlackBox.parentNode.removeChild(this.refBlackBox);
		document.body.style.overflow = "";
		this.refBlackBox = null;
		this.refInnerBlackBox = null;
		this.refMagicBox = null;
	}
	this.createBlackBox = function(targetHTML) {
		var blackBox = document.createElement('div');
		blackBox.className = "blackBox";
		var innerBlackBox = document.createElement('div');
		innerBlackBox.className = "innerBlackBox";
		blackBox.appendChild(innerBlackBox);
		this.refInnerBlackBox = innerBlackBox;
		targetHTML.appendChild(blackBox);
		return blackBox;
	};
	this.createMagicBox = function(targetHTML, showThis) {
		var callBack = this;
		var magicBoxOuter = document.createElement('div');
		document.body.style.overflow = "hidden";
		magicBoxOuter.className = "magicBoxOuter";
		var magicBoxInner = document.createElement('div');
		magicBoxInner.className = "magicBoxInner";
		magicBoxOuter.appendChild(magicBoxInner);
		/// Top
		var top = document.createElement('div');
		top.className = "top";
		var tlc = document.createElement('div');
		tlc.className = "tlc";
		var trc = document.createElement('div');
		trc.className = "trc";
		top.appendChild(tlc);
		top.appendChild(trc);
		magicBoxInner.appendChild(top);


		///////////////
		/// Content ///
		///////////////
		var content = document.createElement('div');
		content.className = "content";
		if (typeof showThis == "object" && showThis.nodeType && typeof showThis.nodeType == "number") {
			// This fix is done so that the closebutton can be appended without losing the onclick function
			// IE could not append showThis directly.
			if (showThis.ownerDocument != document) {
				var appendContent = document.createElement("div");
				appendContent.innerHTML = showThis.innerHTML;
			}
			showThis.style.display = "block";
			content.appendChild((appendContent) ? appendContent : showThis);
		}
		else {
			alert("magicbox currently accepts: nodes");
		}
		magicBoxInner.appendChild(content);
		var closeButton = document.createElement('img');
		closeButton.src = 'images/magicBoxCloseButton.png';
		closeButton.className = "closeButton";
		content.insertBefore(closeButton, content.firstChild);
		closeButton.onclick = function() {
			callBack.removeMagicBox(callBack.currentOwner);
		};


		/// Bottom
		var bottom = document.createElement('div');
		bottom.className = "bottom";
		var blc = document.createElement('div');
		blc.className = "blc";
		var brc = document.createElement('div');
		brc.className = "brc";

		bottom.appendChild(blc);
		bottom.appendChild(brc);
		magicBoxInner.appendChild(bottom);
		/// attach to body
		this.refInnerBlackBox.appendChild(magicBoxOuter);
		var screen = extScreen();
		var xpos = Math.floor(targetHTML.offsetWidth / 2) - Math.floor(magicBoxOuter.offsetWidth / 2) - 10;
		var ypos = Math.floor(targetHTML.offsetHeight / 2) - Math.floor(magicBoxOuter.offsetHeight / 2) - 20;
		this.refInnerBlackBox.style.top = ypos + 'px';
		this.refInnerBlackBox.style.left = xpos + 'px';
		this.refBlackBox.style.top = screen.scrollTop * 1 + "px";
		return magicBoxOuter;
	};
}
addOnload(function() {
	g_mb = new ComitMagicBox();
	var catchArr = [];
	var fileDialog = mGetNodes(new oGetNodes(0, 0, 0, "fileDialog", 0));
	if (fileDialog.length) {
		catchArr[catchArr.length] = fileDialog;
	}
	for (var i in catchArr) {
		var classArr = catchArr[i];
		for (var j in classArr) {
			//debugger;
			var currentTag = classArr[j];
			g_mb.popupMagicBox(currentTag, "cssChecker");
		}
	}
});