//
// Tooltip Replacer by the DtTvB
//
(function() {

	//
	// From DtJS - http://dttvb.yi.org/dtjs/
	//
	function l(el) {
		var tmp = el.offsetLeft;
		el = el.offsetParent;
		while (el) {
			tmp += el.offsetLeft;
			el = el.offsetParent;
		}
		return tmp;
	}
	function t(el) {
		var tmp = el.offsetTop;
		var el2 = el;
		while (el2.nodeName != 'BODY' && el2.nodeName != 'HTML') {
			if (el2.scrollTop)
				tmp -= el2.scrollTop;
			el2 = el2.parentNode;
		}
		el = el.offsetParent;
		while (el) {
			tmp += el.offsetTop;
			el = el.offsetParent;
		}
		return tmp;
	}
	function sz() {
		if (window.innerHeight)
			return [window.innerWidth, window.innerHeight];
		if (document.documentElement && document.documentElement.clientHeight)
			return [document.documentElement.clientWidth,
				document.documentElement.clientHeight];
		return [document.body.clientWidth, document.body.clientHeight];
	}
	
	//
	// On mouse over any element
	//
	function mo(e) {
		//
		// Get the element and event object.
		//
		e = e ? e : window.event;
		var s = e.target ? e.target : e.srcElement;
		var f = 0;
		
		//
		// Get the element with a title.
		//
		while (s.nodeName != 'HTML' && s.nodeName != 'BODY') {
			if ((s.getAttribute('title') != null && s.getAttribute('title') != '') || (typeof s.bcktitle != 'undefined')) {
				f = 1;
				break;
			}
			s = s.parentNode;
		}
		if (!f)
			return true;
		
		if (typeof s.bcktitle == 'undefined') {
			s.bcktitle = s.getAttribute('title');
			s.title = '';
		}
		
		//
		// The on mouse out function, first use to cancel the timer.
		//
		var omo;
		var tid = 0;
		omo = function() {
			clearTimeout (tid);
		};
		
		//
		// Show the tooltip.
		//
		function showTooltip() {
		
			//
			// Create and set style.
			//
			var d  = document.createElement('div');
			var dc = document.createElement('div');
			dc.style.position = 'absolute';
			dc.className = 'tooltip';
			dc.style.padding = '0';
			dc.style.visibility = 'hidden';
			dc.style.overflow = 'hidden';
			d.style.padding = '3px 1em';
			d.className = 'titletip';
			d.innerHTML = s.bcktitle;
			
			//
			// Add to hierarchy.
			//
			dc.appendChild (d);
			document.body.appendChild (dc);
			
			//
			// Get the size of the viewport, and calculate the position of the tooltip.
			//
			var csz = sz();
			var ls = l(s);
			if (ls + dc.offsetWidth > csz[0] - 32) {
				ls = (csz[0] - dc.offsetWidth) - 32;
			}
			var ts = (t(s) + s.offsetHeight + 1);
			dc.style.left = ls + 'px';
			dc.style.top = ts + 'px';
			var anipos = 1;
			var mxw = d.offsetHeight;
			function ani() {
				anipos -= 0.05;
				if (anipos < 0)
					anipos = 0;
				dc.style.top = (ts + (anipos * anipos * (d.offsetHeight / 2))) + 'px';
				dc.style.height = ((1 - (anipos * anipos)) * d.offsetHeight) + 'px';
				if (anipos > 0) {
					setTimeout (ani, 5);
				} else {
					dc.style.height = '';
				}
			}
			ani ();
			
			//
			// Show it.
			//
			dc.style.visibility = 'visible';
			
			//
			// Change the mouse out function.
			//
			omo = function() {
				omo = function() {};
				var anipos = 1;
				function ani() {
					anipos -= 0.1;
					if (anipos < 0)
						anipos = 0;
					dc.style.MozOpacity = anipos;
					dc.style.opacity = anipos;
					dc.style.filter = 'alpha(opacity=' + Math.round(anipos * 100) + ')';
					if (anipos > 0) {
						setTimeout (ani, 5);
					} else {
						dc.style.display = 'none';
						document.body.removeChild (dc);
					}
				}
				ani ();
			};
			dc.onmouseover = mt;
		}
		
		//
		// Set timeout
		//
		tid = setTimeout(showTooltip, 100);
		
		//
		// On mouse out...
		//
		function mt() {
			omo ();
			//
			// Remove event on mouse out.
			//
			if (document.removeEventListener)
				s.removeEventListener ('mouseout', mt, false);
			else if (document.detachEvent)
				s.detachEvent ('onmouseout', mt);
		}
		
		//
		// Add event on mouse out.
		//
		if (document.addEventListener)
			s.addEventListener ('mouseout', mt, false);
		else if (document.attachEvent)
			s.attachEvent ('onmouseout', mt);
	}
	
	//
	// Add event on mouse over.
	//
	if (document.addEventListener)
		document.addEventListener ('mouseover', mo, true);
	else if (document.attachEvent)
		document.attachEvent ('onmouseover', mo);
})();