/** { [object.window.lock]
* @purpose :: creates a global variable for locking the window
* @dependency :: [object.window.LockWindow]
*/
	window.lock = new LockWindow();
/** } [object.window.lock] **/


/** { [object.window.LockWindow]
* @purpose ::
* @param ::
* @return ::
* @creation ::
* @dependency :: [object.window.lock]
* @dependency :: [function.window.Catch]
* @dependency :: [function.window.AddListener]
* @dependency :: [function.window.RemoveListener]
* @dependency :: [function.window.numOnly]
* @dependency :: [function.window.width]
* @dependency :: [function.window.height]
* @dependency :: [function.window._hscroll]
* @dependency :: [function.window._vscroll]
* @dependency :: [function.window.DOM.setElem]
* @edit :: Friday, April 10, 2009 - added callback as an args value in .show - will fire when close is called
* @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]
* ///
*/
function LockWindow(message){
	this.typename = "object.window.LockWindow";
	this.ECHO_WindowLockOutputLayer = undefined;
	this.ECHO_WindowLockOutputLayerText = undefined;
	this.ECHO_WindowLockOutputLayerTextFluid = undefined;
	this.ECHO_WindowLockOutputLayerTextStatic = undefined;
	this.callbackOnClose = undefined;

	this.active = function() {
		try {
			return (this.ECHO_WindowLockOutputLayer != undefined);
		} catch (e) { Catch(this.typename + ".active", e, true); }
	};

	this.show = function(message, nTimeout, args) {
		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); };


		var target = this;
		if (!message) { this.close(); return; };
		try {
			if (this.ECHO_WindowLockOutputLayer == undefined) {
				var docBody = document.body;
				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", this.resize);
					AddListener(window, "scroll", this.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.onclick = function() {
							if (typeof (args.okButton) == "function") {
								args.okButton();
							};
							window.lock.close();
							return (false);
						};
						buttonLayer.appendChild(a);
					};
				} catch (e) { };

				try {
					if (args.cancelButton != undefined) {
						var a = setElem("a");
						a.href = "javascript:;";
						a.innerHTML = "[Cancel]";
						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) { };
			};

			this.resize();
			if (nTimeout != undefined && typeof (nTimeout) == "number" && nTimeout > 0) {
				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;
				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;
				this.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;
				this.resize();
			} else {
				this.show(msg);
			};
		} catch (e) { Catch(this.typename + ".update(1)", e, true); };
	};

	this.close = function() {
		try {
			RemoveListener(window, "resize", this.resize);
			RemoveListener(window, "scroll", this.resize);
			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.resize = function() {
		try {
			if (!this.ECHO_WindowLockOutputLayer) {
				return;
			};
			if (window.width() + window._hscroll() > (numOnly(this.ECHO_WindowLockOutputLayer.style.width) / 1)) {
				this.ECHO_WindowLockOutputLayer.style.width = window.width() + window._hscroll() + "px";
			};
			if (window.height() + window._vscroll() > (numOnly(this.ECHO_WindowLockOutputLayer.style.height) / 1)) {
				this.ECHO_WindowLockOutputLayer.style.height = window.height() + window._vscroll() + "px";
			};
			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";

			if(ECHO_WindowLockOutputLayerText.offsetHeight>=window.height()){
				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";
				this.resize();
			};
		} catch (e) { Catch(this.typename + ".resize(0)", e, true); };
	};
};
/** } [object.window.LockWindow] **/
