﻿/// <reference path="../Script/Lib.js" />

var ToolTip = new Class();
ToolTip.prototype =
{
	init: function()
	{
		var cons =
		[
			["DIV", { ID: "PIXEL", style: { position: "fixed", top: "0px", left: "0px", width: "1px",
				height: "1px"
			}
			}, [
				["DIV", { ID: "WIN", className: "CILA_TOOLTIP", style: { display: "none" },
					onmouseover: this._cancelHide.handle(this),
					onmouseout: this.hideToolTip.handle(this)
				}, [
					["TABLE", { cellSpacing: 0, cellPadding: 0 }, [
						["TR", {}, [
							["TD", { className: "CILA_TOOLTIP_TopLeft"}],
							["TD", { className: "CILA_TOOLTIP_TopCenter"}],
							["TD", { className: "CILA_TOOLTIP_TopRight"}]
						]],
						["TR", {}, [
							["TD", { className: "CILA_TOOLTIP_CenterLeft"}],
							["TD", { ID: "CENTER", className: "CILA_TOOLTIP_Center" }, [
								["DIV", { ID: "CONTENT", className: "CILA_TOOLTIP_CenterText"}]
							]],
							["TD", { className: "CILA_TOOLTIP_CenterRight"}]
						]],
						["TR", {}, [
							["TD", {}, [["DIV", { className: "CILA_TOOLTIP_BottomLeft"}]]],
							["TD", {}, [["DIV", { className: "CILA_TOOLTIP_BottomCenter"}]]],
							["TD", {}, [["DIV", { className: "CILA_TOOLTIP_BottomRight"}]]]
						]]
					]],
					["DIV", { ID: "R1", className: "CILA_TOOLTIP_ArrowTopLeft", style: { display: "none"}}],
					["DIV", { ID: "R2", className: "CILA_TOOLTIP_ArrowTopRight", style: { display: "none"}}],
					["DIV", { ID: "R3", className: "CILA_TOOLTIP_ArrowBottomLeft", style: { display: "none"}}],
					["DIV", { ID: "R4", className: "CILA_TOOLTIP_ArrowBottomRight", style: { display: "none"}}]
				]]
			]],
			["DIV", { ID: "ZONE", style: { position: "absolute", top: "0px", left: "0px", width: "100%",
				height: "100%", visibility: "hidden", zIndex: -100}}]
		];
		this.ctrl = create(cons, document.body);
		document.onmousemove = this._docMouseMove.eventHandle(this, document.onmousemove);
	},

	_docMouseMove: function(e, func)
	{
		if (func) func(e);

		if (this.timer)
		{
			window.clearTimeout(this.timer);
			this.timer = null;
		}
		if (!this.isOnToolTip)
			this.hideToolTip();
		var elem = Event.element(e);
		if (elem != null && this.element != elem)
		{
			var tooltip = Element.getAttribute(elem, "tooltip"),
				tooltipid = Element.getAttribute(elem, "tooltipid"),
				html = null;
			if (tooltip)
				html = "<pre>" + tooltip + "</pre>";
			else
			{
				tooltipid = get(tooltipid);
				if (tooltipid) html = tooltipid.innerHTML;
			}
			if (html && html != "")
			{
				this.timer = window.setTimeout(this._showToolTip.handle(this, html, e.clientX, e.clientY), 500);
			}
		}
	},

	_cancelHide: function()
	{
		if (this.hideTimer)
		{
			window.clearTimeout(this.hideTimer);
			this.hideTimer = null;
			this.isShow = this.isOnToolTip = true;
		}
	},

	hideToolTip: function()
	{
		if (this.isShow)
		{
			this.isShow = this.isOnToolTip = false;
			this.hideTimer = window.setTimeout(this._hideToolTip.handle(this), 500);
		}
	},

	_hideToolTip: function()
	{
		Element.hide(this.ctrl.WIN);
	},

	_showToolTip: function(html, x, y)
	{
		this.isShow = true;
		this.ctrl.CONTENT.innerHTML = html;

		this.ctrl.WIN.style.visibility = 'hidden';
		Element.show(this.ctrl.WIN);
		var zone = Element.getSize(this.ctrl.ZONE);
		var size = Element.getSize(this.ctrl.WIN);
		var pos = 1;
		if (x + size[0] > zone[0]) { x -= size[0] - 20; pos += 1 };
		if (y + size[1] > zone[1]) { y -= size[1] + 20; pos += 2 };
		if (!Browser.IE6)
		{
			for (var i = 1; i <= 4; i++)
			{
				if (i == pos)
					Element.show(this.ctrl["R" + i]);
				else
					Element.hide(this.ctrl["R" + i]);
			}
		}
		Element.setPosition
		(
			this.ctrl.WIN,
			x + (Browser.IE6 ? document.documentElement.scrollLeft : 0),
			y + 15 + (Browser.IE6 ? document.documentElement.scrollTop : 0)
		);
		this.ctrl.WIN.style.visibility = 'visible';
	}
}
var __tooltip = new ToolTip();