/** { [lib.objects.window.LockWindow]
* @purpose :: creates a global variable for locking the window
* @dependency :: [lib.functions.window.Catch]
* @dependency :: [lib.functions.window.AddListener]
* @dependency :: [lib.functions.window.RemoveListener]
* @dependency :: [lib.functions.window.numOnly]
* @dependency :: [lib.functions.window.width]
* @dependency :: [lib.functions.window.height]
* @dependency :: [lib.functions.window._hscroll]
* @dependency :: [lib.functions.window._vscroll]
* @dependency :: [lib.functions.dom.setElem]
* @dependency :: [lib.variables.window.keyCode]
* @edit :: Friday, April 10, 2009 - added callback as an args value in .show - will fire when close is called
* @edit :: Tuesday, March 31, 2009 - removes floating elements from visibility when open (flash movies will no longer attempt to float over)
* @edit :: Thursday, January 29, 2009 - added auto height resize if larger then the window size
* @edit :: Wednesday, January 28, 2009 - added support for customButtons
* @edit :: Tuesday, January 27, 2009 - formatting for compile
* ///
* @author :: Scott McDonald;
* @license :: Lowen Corporation Use Only [Proprietary Script]
* ///
*/
window.lock = new LockWindow();
function LockWindow(message) {
	var target = this;
	this.timeoutObject = undefined;
	this.typename = "lib.objects.window.LockWindow";
	this.ECHO_WindowLockOutputLayer = undefined;
	this.ECHO_WindowLockOutputLayerText = undefined;
	this.ECHO_WindowLockOutputLayerTextFluid = undefined;
	this.ECHO_WindowLockOutputLayerTextStatic = undefined;

	this._onOk = function() {
		/* run whatever actions need to be done with the onOK click */
		this.close();
		if (this.onOk) { this.onOk(); };
	}; this.onOk = function() { };
	this._onCancel = function() {
		this.close();
		/* run whatever action needs to be done with the cancel click */
		if (this.onCancel) { this.onCancel(); };
	}; this.onCancel = function() { };

	this.active = function() {
		try {
			return (this.ECHO_WindowLockOutputLayer != undefined);
		} catch (e) { Catch(this.typename + ".active", e, true); }
	};


	this.hiddenlist = new Array();
	this.hideFloatingElemenets = function() {
		return;
		this.hiddenlist = new Array();
		var elements = document.getElementsByTagName("object");
		var i = 0;
		var l = elements.length;
		var me;
		for (i; i < l; i++) {
			trace("here");
			me = elements[i];
			me.style.position = "absolute";
			me.style.left = -1000;
			me.style.top = -1000;

			this.hiddenlist[this.hiddenlist.length] = me;
		};
	};

	this.showFloatingElements = function() {
		var i = 0;
		var l = this.hiddenlist.length;
		var me;
		for (i; i < l; i++) {
			me = this.hiddenlist[i];
			me.style.position = "relative";
			me.style.left = null;
			me.style.top = null;
		};
		this.hiddenlist = new Array();
	};

	this.show = function(message, nTimeout, args) {
		this.hideFloatingElemenets();
		var target = this;

		try {
			if (!args) { args = {} };
		} catch (e) { Catch(this.typename + ".show(2)", e, true); };

		try {
			if (args.purge && this.ECHO_WindowLockOutputLayer) {
				this.close();
			};
		} catch (e) { Catch(this.typename + ".show(1)", e, true); };
		try {
			if (args.callback) {
				this.callbackOnClose = args.callback;
			};
		} catch (e) { Catch(this.typename + ".show(2)", e, true); };

		if (!message) { this.close(); return; };
		try {
			if (this.ECHO_WindowLockOutputLayer == undefined) {
				var docBody = document.getElementsByTagName("body")[0];
				if (docBody == null) {
					throw ({ message: "body element must be established before window.lock element can be used" });
				} else {
					docBody.style.margin = "0";
					this.ECHO_WindowLockOutputLayer = document.createElement("div");
					this.ECHO_WindowLockOutputLayer.name = this.ECHO_WindowLockOutputLayer.id = "ECHO_WindowLockOutputLayer";
					this.ECHO_WindowLockOutputLayer.innerHTML = "&nbsp;";
					this.ECHO_WindowLockOutputLayer.style.position = "absolute";
					this.ECHO_WindowLockOutputLayer.className = "ECHO_WindowLockOutputLayerBackground";
					this.ECHO_WindowLockOutputLayer.style.top = "0px";
					this.ECHO_WindowLockOutputLayer.style.left = "0px";
					this.ECHO_WindowLockOutputLayer.zIndex = 9998;
					this.ECHO_WindowLockOutputLayer.style.backgroundColor = "#000000";
					this.ECHO_WindowLockOutputLayer.style.filter = "alpha(opacity=40)";
					/** this.ECHO_WindowLockOutputLayer.style.-khtml-opacity: 0.4; */
					/** this.ECHO_WindowLockOutputLayer.style.moz-opacity: 0.4; */
					this.ECHO_WindowLockOutputLayer.style.opacity = 0.4;
					docBody.appendChild(this.ECHO_WindowLockOutputLayer);
					if (message != "" && message != undefined) {
						this.ECHO_WindowLockOutputLayerText = document.createElement("div");
						this.ECHO_WindowLockOutputLayerText.id = "ECHO_WindowLockOutputLayerText";
						this.ECHO_WindowLockOutputLayerText.style.fontSize = "20px";
						this.ECHO_WindowLockOutputLayerText.style.backgroundColor = "#FFFFFF";
						this.ECHO_WindowLockOutputLayerText.style.border = "3px double #CCCCCC";
						this.ECHO_WindowLockOutputLayerText.style.padding = "10px";
						docBody.appendChild(this.ECHO_WindowLockOutputLayerText);

						this.ECHO_WindowLockOutputLayerTextFluid = document.createElement("div");
						this.ECHO_WindowLockOutputLayerTextFluid.id = "ECHO_WindowLockOutputLayerTextFluid";
						this.ECHO_WindowLockOutputLayerTextFluid.style.fontSize = "20px";
						this.ECHO_WindowLockOutputLayerText.appendChild(this.ECHO_WindowLockOutputLayerTextFluid);

						this.ECHO_WindowLockOutputLayerTextStatic = document.createElement("div");
						this.ECHO_WindowLockOutputLayerTextStatic.id = "ECHO_WindowLockOutputLayerTextStatic";
						this.ECHO_WindowLockOutputLayerTextStatic.style.fontSize = "20px";
						this.ECHO_WindowLockOutputLayerText.appendChild(this.ECHO_WindowLockOutputLayerTextStatic);
					};
					AddListener(window, "resize", window.lock.resize);
					AddListener(window, "scroll", window.lock.resize);
				};
			};

			if (message != "" && message != undefined) {
				this.ECHO_WindowLockOutputLayerTextFluid.innerHTML = message;
			} else if (this.ECHO_WindowLockOutputLayerText) {
				docBody.removeChild(this.ECHO_WindowLockOutputLayerText);
				this.ECHO_WindowLockOutputLayerText = undefined;
			};


			if (args.okButton || args.cancelButton || args.customButton) {
				var buttonLayer = setElem("div");
				buttonLayer.style.textAlign = "center";
				buttonLayer.style.marginTop = "10px";
				this.ECHO_WindowLockOutputLayerTextStatic.appendChild(buttonLayer);



				try {
					if (args.okButton != undefined) {
						var a = setElem("a");
						a.href = "javascript:;";
						a.innerHTML = "[OK]";
						a.align = "center";
						a.style.color = "#000000";
						a.style.fontSize = "14px";
						a.style.fontFamily = "Arial, Helvetica";
						a.style.margin = "5px";

						if (typeof (args.okButton) == "function") {
							target.onOk = function() {
								args.okButton();
							};
						};
						a.onclick = function() {
							target._onOk();
							return (false);
						};
						buttonLayer.appendChild(a);
						AddListener(document, "keydown", target.keypress);

					};
				} catch (e) { };



				try {
					if (args.cancelButton != undefined) {
						var a = setElem("a");
						a.href = "javascript:;";
						a.style.color = "#000000";
						a.style.fontSize = "14px";
						a.style.fontFamily = "Arial, Helvetica";
						a.innerHTML = "[Cancel]";
						a.style.margin = "5px";
						a.onclick = function() {
							if (typeof (args.cancelButton) == "string") {
								eval(args.cancelButton);
							} else {
								args.cancelButton();
							};
							return (false);
						};
						buttonLayer.appendChild(a);
					};
				} catch (e) { };

				try {
					if (args.customButton != undefined) {
						var c = args.customButton;
						if (c.length) {
							var me;
							var i = 0;
							var l = c.length;
							for (i; i < l; i++) {
								me = c[i];
								this.appendCustomButton(me, buttonLayer);
							};
						} else {
							Catch(this.typename + ".show(155)", { message: "customButton must be sent as an array of objects defining text and callback" }, true);
						};
					};
				} catch (e) { };
			};
			window.lock.resize();
			if (nTimeout != undefined && typeof (nTimeout) == "number" && nTimeout > 0) {
				target.timeoutObject = setTimeout("window.lock.close()", nTimeout);
			} else {
				clearTimeout();
			}
		} catch (e) { Catch(this.typename + ".show(0)", e, true); };
		return (true);
	};

	this.appendCustomButton = function(me, buttonLayer) {
		try {
			var target = this;
			if (!me.text || !me.callback || typeof (me.callback) != "function") {
				Catch(this.typename + ".show(159)", { message: "customButton must be sent as an object defining text and callback" }, true);
			} else {
				var a = setElem("a");
				a.href = "javascript:;";
				a.innerHTML = me.text;
				a.style.color = "#000000";
				a.style.fontSize = "14px";
				a.style.fontFamily = "Arial, Helvetica";
				a.style.margin = "5px";
				if (me.style) {
					if (me.style.length) {
						var i = 0;
						var l = me.style.length;
						for (i; i < l; i++) {
							a.style[me.style[i].name] = me.style[i].value;
						};
					} else {
						a.style[me.style.name] = me.style.value;
					};
				};
				if (me.classname) {
					a.className = me.classname;
				};
				a.onclick = function() {
					me.callback();
					return (false);
				};
				buttonLayer.appendChild(a);
				if (me.breakafter) {
					buttonLayer.appendChild(setElem("br"));
				};
			};
		} catch (e) { Catch(this.typename + "appendCustomButton(base)", e, true); };
	};

	this.addText = function(msg) {
		try {
			if (this.ECHO_WindowLockOutputLayerTextFluid) {
				this.ECHO_WindowLockOutputLayerTextFluid.innerHTML = this.ECHO_WindowLockOutputLayerTextFluid.innerHTML + msg;
				window.lock.resize();
			} else {
				this.show(msg);
			};
		} catch (e) { Catch("window.lock.addText(base)", e, true); };
	};

	this.update = function(msg) {
		try {
			if (this.ECHO_WindowLockOutputLayerTextFluid) {
				this.ECHO_WindowLockOutputLayerTextFluid.innerHTML = msg;
				window.lock.resize();
			} else {
				this.show(msg);
			};
		} catch (e) { Catch(this.typename + ".update(1)", e, true); };
	};

	this.close = function() {
		try {
			clearTimeout(target.timeoutObject);
			target.timeoutObject = undefined;
			this.showFloatingElements();
			RemoveListener(window, "resize", window.lock.resize);
			RemoveListener(window, "scroll", window.lock.resize);
			RemoveListener(document, "keydown", target.keypress);
			if (this.ECHO_WindowLockOutputLayer) {
				this.ECHO_WindowLockOutputLayerTextFluid.parentNode.removeChild(this.ECHO_WindowLockOutputLayerTextFluid);
				this.ECHO_WindowLockOutputLayerTextStatic.parentNode.removeChild(this.ECHO_WindowLockOutputLayerTextStatic);

				this.ECHO_WindowLockOutputLayerTextFluid = undefined;
				this.ECHO_WindowLockOutputLayerTextStatic = undefined;

				this.ECHO_WindowLockOutputLayer.parentNode.removeChild(this.ECHO_WindowLockOutputLayerText);
				this.ECHO_WindowLockOutputLayer.parentNode.removeChild(this.ECHO_WindowLockOutputLayer);

				this.ECHO_WindowLockOutputLayer = undefined;
				this.ECHO_WindowLockOutputLayerText = undefined;
			};
			if (this.callbackOnClose) {
				this.callbackOnClose();
			}
		} catch (e) { Catch(this.typename + ".close(0)", e, true); };
	};

	this.keypress = function(e, pushed) {
		if (target.typename != this.typename) { /* if we're in the listener object rather then the lock object, re-address */
			if (pushed) { /* to prevent an unending loop - if we've already tried to re-address to the proper level, then just die out */
				return;
			};
			target.keypress(e);
			return;
		};
		/*trace(e.keyCode);*/
		if (e.keyCode == 27) {
			this._onCancel();
		};
		if (e.keyCode == 13) {
			this._onOk();
		};
	};

	this.resize = function() {
		try {
			try {
				if (!this.ECHO_WindowLockOutputLayer) {
					return;
				};
			} catch (e) { Catch(this.typename + ".resize(5)", e, true); };
			try {
				if (window.width() + window._hscroll() > (numOnly(this.ECHO_WindowLockOutputLayer.style.width) / 1)) {
					this.ECHO_WindowLockOutputLayer.style.width = window.width() + window._hscroll() + "px";
				};
			} catch (e) { Catch(this.typename + ".resize(4)", e, true); };

			try {
				if (window.height() + window._vscroll() > (numOnly(this.ECHO_WindowLockOutputLayer.style.height) / 1)) {
					this.ECHO_WindowLockOutputLayer.style.height = window.height() + window._vscroll() + "px";
				};
			} catch (e) { Catch(this.typename + ".resize(3)", e, true); };

			try {
				this.ECHO_WindowLockOutputLayerText.style.position = "absolute";
				this.ECHO_WindowLockOutputLayerText.className = "ECHO_WindowLockOutputLayerText";
				this.ECHO_WindowLockOutputLayerText.style.zIndex = 9999;
				this.ECHO_WindowLockOutputLayerText.style.top = (window.height() * .5) + window._vscroll() - (this.ECHO_WindowLockOutputLayerText.offsetHeight * .5) + "px";
				this.ECHO_WindowLockOutputLayerText.style.left = (window.width() * .5) + window._hscroll() - (this.ECHO_WindowLockOutputLayerText.offsetWidth * .5) + "px";
			} catch (e) { Catch(this.typename + ".resize(2)", e, true); };

			try {
				if (ECHO_WindowLockOutputLayerText.offsetHeight >= window.height()) {
					try {
						var _width = ECHO_WindowLockOutputLayerText.offsetWidth;
						ECHO_WindowLockOutputLayerText.style.height = (window.height() * .8) + "px";
						ECHO_WindowLockOutputLayerText.style.overflow = "auto";
						ECHO_WindowLockOutputLayerText.style.width = (numOnly(_width) + 16) + "px";
					} catch (e) { Catch(this.typename + ".resize(1.1)", e, true); };
					try {
						window.lock.resize();
					} catch (e) { Catch(this.typename + ".resize(1.2)", e, true); };
				};
			} catch (e) { Catch(this.typename + ".resize(1)", e, true); };
		} catch (e) { Catch(this.typename + ".resize(0)", e, true); };
	};
};
/** } [lib.objects.window.LockWindow] **/
