/** { [variable.window.tooltip]
* @dependency :: [object.window.Tooltip]
*/
window.tooltip = new ToolTip();
/** } [variable.window.tooltip] **/

/** { [object.window.Tooltip]
* @dependency :: [function.window.PositionElementToElement]
 * tooltips can be shown one at a time, and are added at the window level .
 * this is an automatically created item there should be no need to declair
 * the item.
 *

	10/4/2007:4:14 PM
	adding functionality to send an object instead of an argument list
	supports {
		text:[string]
		purge: [boolean] :: if tooltip window should be cleared before use... default true
		anchor:[object pointer] :: front end object to place tooltip in referrence to
		target: [object pointer] :: see anchor
		timeout:[int in milliseconds]
		onClose:[function] :: if present, will show close button
			close:[boolean] can be used to indicate default close function - is ignored if onClose is set
		onCancel:[function] :: if present will show cancel button
		onOK:[function] :: if present will show ok button
		onInit:[function] :: calls when tip window is opened
		onUnload: [function] :: calls when tip window is distroyed
	}

	* @edit :: 11/28/2007:9:45 AM - added timeout for close command
	* @edit :: 11/28/2007:9:45 AM - moved methods into
	* @edit :: Tuesday, January 27, 2009 - updated for compiling
*/

function ToolTip(){
	this.text = "";
	this.bgColor = "#FFFFCC";
	this.anchor = undefined;
	this.time = 4000;
	this.layer = undefined;
	this.intervalId = undefined;
	this.unload = undefined;

	this.show = function(cText, oAnchorItem, nTimeout, showClose, purge) {
		var target = this;
		var def = new Object();
		if (typeof (cText) != "object") {
			def.text = cText;
			def.purge = (purge != false);
			def.anchor = oAnchorItem;
			def.timeout = ((nTimeout != undefined) ? nTimeout : this.time);
			def.onClose = ((typeof (showClose) == "function") ? showClose : ((showClose == true) ? this.close : undefined));
		} else {
			def = cText;
			def.anchor = (def.anchor != undefined) ? def.anchor : (def.target != undefined) ? def.target : undefined;
			def.timeout = (def.timeout) ? def.timeout : this.time;
			def.purge = (def.purge != false);
			def.onClose = (typeof (def.onClose) == "function") ? def.onClose : ((def.close == true) ? this.close : undefined);
		};

		if (def.purge) {
			clearTimeout(this.intervalId);
			this.close();
		};

		if (def.timeout == -1) {
			clearTimeout(this.intervalId);
		};

		this.layer = document.getElementById("tooltipLayer");
		if (!this.layer) { this.layer = document.createElement("div"); };
		if (def.unique == true) {
			if (this.layer.innerHTML.indexOf(def.text) != -1) {
				return;
			};
		};

		if (this.layer.childNodes.length > 0) {
			this.layer.appendChild(document.createElement("br"));
		};

		def.text = def.text.replace(/\n/gi, "<br />");
		this.layer.innerHTML += def.text;


		if (def.onClose || def.onCancel || def.onOK) {
			this.layer.appendChild(document.createElement("br"));
		};

		if (def.onClose != undefined) {
			var closeButton = document.createElement("a");
			closeButton.id = closeButton.name = "ToolTipCloseLink";
			closeButton.href = "javascript:;";
			closeButton.innerText = "Close";
			closeButton.style.textAlign = "center";
			if (def.onClose != target.close) {
				closeButton.onclick = function() {
					def.onClose();
					target.close();
				};
			} else {
				closeButton.onclick = function() {
					target.close();
				};
			};
			this.layer.appendChild(document.createTextNode(" ["));
			this.layer.appendChild(closeButton);
			this.layer.appendChild(document.createTextNode("] "));
		} else {

			if (def.onOK != undefined) {
				var OKButton = document.createElement("a");
				OKButton.id = OKButton.name = "ToolTipOKLink";
				OKButton.href = "javascript:;";
				OKButton.appendChild(document.createTextNode("sOK"));
				OKButton.onclick = function() {
					def.onOK();
					target.close();
				};
				this.layer.appendChild(document.createTextNode(" ["));
				this.layer.appendChild(OKButton);
				this.layer.appendChild(document.createTextNode("] "));
			};

			if (def.onCancel != undefined) {
				var cancelButton = document.createElement("a");
				cancelButton.id = cancelButton.name = "ToolTipCancelLink";
				cancelButton.href = "javascript:;";
				cancelButton.innerText = "Cancel";
				cancelButton.style.float = "center";
				cancelButton.onclick = function() {
					def.onCancel();
					target.close();
				};
				this.layer.appendChild(document.createTextNode(" ["));
				this.layer.appendChild(cancelButton);
				this.layer.appendChild(document.createTextNode("] "));
			};
		};

		this.layer.id = this.layer.name = "tooltipLayer";
		this.layer.style.background = this.bgColor;
		this.layer.style.padding = "10px";
		this.layer.style.border = "1px solid #000000";
		this.layer.style.textAlign = "left";
		document.body.appendChild(this.layer);
		this.layer.style.position = "absolute";
		var x, y;
		if (def.anchor) {
			def.anchor.name = def.anchor.id = (
					(def.anchor.id != "" && def.anchor.id != undefined) ? def.anchor.id : (
						(def.anchor.name != "" && def.anchor.name != undefined) ? def.anchor.name : "TOOLTOP_OBJECT"
					)
				);
			PositionElementToElement(this.layer, def.anchor);
		} else {
			this.layer.style.top = document.body.scrollTop;
			this.layer.style.left = "0px";
		};
		if (def.timeout > -1) {
			this.intervalId = setTimeout("window.tooltip.close()", def.timeout);
		};
		if (def.init) {
			try { def.onInit(); } catch (e) { };
		};
		if (def.onUnload) {
			this.unload = def.onUnload;
		};
	};

	this.close = function(nTimeout) {
		clearTimeout(this.intervalId);
		this.intervalId = undefined;
		/* if there is a timeout delay then set countdown */
		if (nTimeout != undefined && nTimeout > 0) {
			this.intervalId = setTimeout("window.tooltip.close()", nTimeout);
			return;
		} else {
			try {
				/* make sure there is something to remove */
				if (this.layer) {
					/* remove layer */
					document.body.removeChild(this.layer);
					/* cleanup*/
					this.layer = undefined;
				};
			} catch (e) { };
			/* check to see if there is an onUnload Event */
			if (this.unload) {
				var temp = this.unload;
				this.unload = undefined;
				try { temp() } catch (e) { };
			};
		};
	};
};
/** } [object.window.Tooltip] **/
