/*
originally written by paul sowden <paul@idontsmoke.co.uk> | http://idontsmoke.co.uk
modified and localized by alexander shurkayev <alshur@narod.ru> | http://htmlcoder.visions.ru (http://htmlcoder.visions.ru/JavaScript/?11)
modified by Pavel Golodoniuc <pavek3k@yandex.ru>

#tooltip{
background:#FFFFFF;
border:1px solid #666666;
color:#333333;
font:menu;
margin:0px;
padding:3px 5px;
position:absolute;
visibility:hidden
}
*/

//window.onerror = null;

tooltip = {

	// Settings
	attr_name:		"tooltip",							// The name of tooltip attribute
	blank_text:		"(a new window will be opened)",	// Tooltip text for the link with target="_blank"
	max_width:		300,
	delay:			0,
	parse_all:		false,								// title and alt attributes are recognized as tooltips

	// Event handlers
	OnBuilt:		null,
	OnShow:			null,
	OnHide:			null,

	// Class' members
	initialized:	false,								// Is the object initialized OnLoad
	hdr:			document.createElement("DIV"),		// Header container
	t:				document.createElement("DIV"),		// Body container
	c:				null,								// Timer
	g:				false,								// Tooltip visibility flag
	stop:			false,								// Is tooltip frozen or not
	hdr_h:			0,									// Header height
	w:				0,									// Width of the tooltip

	m: function(e)
	{
		if (tooltip.g && !tooltip.stop)
		{
			var oCanvas = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];
			tooltip.x = window.event ? event.clientX + oCanvas.scrollLeft : e.pageX;
			tooltip.y = window.event ? event.clientY + oCanvas.scrollTop : e.pageY;
			tooltip.a();
		}
	},

	d: function()
	{
		if (!tooltip.initialized)
		{
			tooltip.initialized = true;
			tooltip.hdr.style.position = "absolute";
			document.body.insertBefore(tooltip.hdr, document.body.firstChild);
			tooltip.t.setAttribute("id", "tooltip");
			tooltip.t.style.position = "absolute";
			document.body.insertBefore(tooltip.t, document.body.firstChild);

			tooltip.hdr.style.top = -1000;
			tooltip.t.style.top = -1000;
		}

		a = document.all && !window.opera ? document.all : document.getElementsByTagName("*"); // in opera 9 document.all produces type mismatch error
		aLength = a.length;
		for (var i = 0; i < aLength; i++)
		{
			//if (a[i].tagName == "A" || a[i].tagName == "BUTTON" || (a[i].tagName == "INPUT" && (a[i].type == "submit" || a[i].type == "button" || a[i].type == "reset"))) a[i].onclick = self.focus;

			if (!a[i] || a[i].getAttribute("tt_parsed")) continue;

			if (tooltip.parse_all)
			{
				tooltip_title = a[i].getAttribute("tooltip");
				if (!tooltip_title)
					tooltip_title = a[i].getAttribute("title"); // returns form object if IE & name="title"; then IE crashes; so...
				if (!tooltip_title)
					tooltip_title = a[i].getAttribute("alt");
				if (tooltip_title && typeof tooltip_title != "string")
					tooltip_title = "";

				tooltip_blank = a[i].getAttribute("target") && a[i].getAttribute("target") == "_blank" && tooltip.blank_text;
				if (tooltip_title || tooltip_blank)
				{
					a[i].setAttribute(tooltip.attr_name, tooltip_blank ? (tooltip_title ? tooltip_title + " " + tooltip.blank_text : tooltip.blank_text) : tooltip_title);
					if (a[i].getAttribute(tooltip.attr_name))
					{
						a[i].removeAttribute("title");
						if (/*tooltip_alt && */a[i].complete)
							a[i].removeAttribute("alt");
						if (a[i].getAttribute("tt_onclick") == null)
						{
							tooltip.l(a[i], "mouseover", tooltip.s);
							tooltip.l(a[i], "mouseout", tooltip.h);
							if (a[i].getAttribute("tt_clickstop") != null)
								tooltip.l(a[i], "click", tooltip.freeze);
						}
						else
							tooltip.l(a[i], "click", tooltip.s);
						a[i].setAttribute("tt_parsed", 1);
					}
				}
			}
			else
			{
				if (a[i].getAttribute(tooltip.attr_name) != null)
				{
					if (a[i].getAttribute("tt_onclick") == null)
					{
						tooltip.l(a[i], "mouseover", tooltip.s);
						tooltip.l(a[i], "mouseout", tooltip.h);
						if (a[i].getAttribute("tt_clickstop") != null)
							tooltip.l(a[i], "click", tooltip.freeze);
					}
					else
						tooltip.l(a[i], "click", tooltip.s);
					a[i].setAttribute("tt_parsed", 1);
				}
			}
		}
		document.onclick = tooltip.closetip;
		document.onmousemove = tooltip.m;
		window.onscroll = tooltip.h;

		if (tooltip.OnBuilt)
			tooltip.OnBuilt();
	},

	s: function(e)
	{
		var d = window.event ? window.event.srcElement : e.target;
		var s = d.getAttribute(tooltip.attr_name);
		if (s == null)
			return;

		// Close opened tooltip
		tooltip.stop = false;
		if (tooltip.g)
			tooltip.h();

		var tt_holder = d.getAttribute("tt_holder");
		if (tt_holder)
			tooltip.tc = document.getElementById(tt_holder);
		else
		{
			var tt_css = d.getAttribute("tt_css");
			if (tt_css != null)
			{
				tooltip.t.id = tooltip.attr_name + "NoStyle";
				tooltip.t.className = tt_css;
			}
			else
				tooltip.t.id = tooltip.attr_name;
			tooltip.t.innerHTML = s;
			tooltip.tc = tooltip.t;
		}

		var hdr = d.getAttribute("tt_header");
		if (hdr)
		{
			tooltip.hdr.className = d.getAttribute("tt_csshdr");
			tooltip.hdr.innerHTML = "<b>" + hdr + "</b>";
			tooltip.hdr_h = 1;
		}
		else
			tooltip.hdr_h = 0;

		tooltip.w = d.getAttribute("tt_width");

		var oCanvas = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];
		tooltip.x = window.event ? event.clientX + oCanvas.scrollLeft : e.pageX;
		tooltip.y = window.event ? event.clientY + oCanvas.scrollTop : e.pageY;

		if (d.getAttribute("tt_onclick") == null)
		{
			if (tooltip.delay)
				tooltip.c = setTimeout(tooltip.show, tooltip.delay);
			else
				tooltip.show();
		}
		else
		{
			// Immediately show and freeze tooltip
			tooltip.show();
			tooltip.freeze();
		}
	},

	show: function()
	{
		if (tooltip.OnShow)
			tooltip.OnShow();
		if (tooltip.hdr_h)
		{
			tooltip.hdr.style.display = "";
			tooltip.hdr_h = tooltip.hdr.offsetHeight;
		}
		tooltip.tc.style.display = "";
		tooltip.g = true;
		tooltip.a();
	},

	h: function(e)
	{
		if (tooltip.stop)
			return;
		clearTimeout(tooltip.c);
		if (!tooltip.g)
			return;
		if (tooltip.OnHide)
			tooltip.OnHide();
		if (tooltip.hdr_h)
			tooltip.hdr.style.display = "none";
		tooltip.tc.style.display = "none";
		tooltip.g = false;
		tooltip.tc = null;
	},

	l: function(o, e, a)
	{
		if (o.addEventListener)
			o.addEventListener(e, a, false); // was true--Opera 7b workaround!
		else if (o.attachEvent)
			o.attachEvent("on" + e, a);
		else
			return null;
	},

	a: function()
	{
		var oCanvas = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];

		var w_width = oCanvas.clientWidth ? oCanvas.clientWidth + oCanvas.scrollLeft : window.innerWidth + window.pageXOffset;
		var w_height = window.innerHeight ? window.innerHeight + window.pageYOffset : oCanvas.clientHeight + oCanvas.scrollTop; // should be vice verca since Opera 7 is crazy!

		if (tooltip.t == tooltip.tc)
			tooltip.t.style.width = (tooltip.max_width && tooltip.t.offsetWidth > tooltip.max_width) ? tooltip.max_width + "px" : (tooltip.w ? tooltip.w + "px" : "auto");

		var t_width = tooltip.tc.offsetWidth;
		var t_height = tooltip.tc.offsetHeight;

		if (tooltip.hdr_h)
		{
			// Equalize header and body width
			if (t_width > tooltip.hdr.offsetWidth)
				tooltip.hdr.style.width = t_width;
			else
				tooltip.tc.style.width = tooltip.hdr.offsetWidth;
			tooltip.hdr.style.top = tooltip.y + 8 + "px";
			tooltip.hdr_h = tooltip.hdr.offsetHeight;
		}
		tooltip.tc.style.left = tooltip.x + 8 + "px";
		tooltip.tc.style.top = tooltip.y + 8 + tooltip.hdr_h + "px";

		if (tooltip.x + t_width > w_width) tooltip.tc.style.left = w_width - t_width + "px";
		if (tooltip.y + t_height > w_height) tooltip.tc.style.top = w_height - t_height + "px";
		tooltip.hdr.style.left = tooltip.tc.style.left;
	},

	freeze: function(e)
	{
		if (tooltip.g)
			tooltip.stop = true;
		(e ? e : event || {}).cancelBubble = true;
	},

	closetip: function(e)
	{
		var evt = (e ? e : event);
		if (tooltip.stop && !tooltip.isChild(evt.target ? evt.target : evt.srcElement, tooltip.tc))
		{
			tooltip.stop = false;
			tooltip.h();
		}
	},

	isChild: function(s, d)
	{
		while (s)
		{
			if (s == d) 
				return true;
			s = s.parentNode;
		}
		return false;
	}
}

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root)
{
	if (root.addEventListener)
		root.addEventListener("load", tooltip.d, false);
	else if (root.attachEvent)
		root.attachEvent("onload", tooltip.d);
}
