
/**
 *
 */
var bIsIE = document.all ? true : false;

/**
 *
 */
var Tooltip = function () {
	var sIdentifier = 'tooltip';
	var sCurrentClassName = '';
	var nTopOffset = 3;
	var nLeftOffset = 3;
	var nMaxWidth = 300;
	var nSpeed = 10;
	var nTimer = 20;
	var nEndAlpha = 95;
	var nAlpha = 0;
	
	var oTooltip = null;
	var oTopDiv = null;
	var oContentDiv = null;
	var oBottomDiv = null;
	
	var nHeight = 0;
	
	var fOldDocumentMouseMoveHandler = null;
	
	var createTooltipDiv = function () {
		oTooltip = document.createElement ('div');
		oTooltip.setAttribute ('id', sIdentifier);
		oTooltip.style.position = 'absolute';
		oTopDiv = document.createElement ('div');
		oTopDiv.setAttribute ('id', sIdentifier + '_top');
		oContentDiv = document.createElement ('div');
		oContentDiv.setAttribute ('id', sIdentifier + '_content');
		oBottomDiv = document.createElement ('div');
		oBottomDiv.setAttribute ('id', sIdentifier + '_bottom');
		
		oTooltip.appendChild (oTopDiv);
		oTooltip.appendChild (oContentDiv);
		oTooltip.appendChild (oBottomDiv);
		document.body.appendChild (oTooltip);
		oTooltip.style.opacity = 0;
		oTooltip.style.filter = 'alpha(opacity=0)';
		
	}
	
	return {
		show: function (sContentHTML, sClassName, nWidth) {
			if (sClassName === undefined || sClassName === null) {
				sClassName = '';
			}
			if (nWidth === undefined) {
				nWidth = null;
				// nWidth = nMaxWidth;
			}
			
			if (oTooltip === null) {
				createTooltipDiv (sClassName);
			}
			if (sClassName != sCurrentClassName) {
				oTooltip.className = sClassName;
				oTopDiv.className = sClassName + 'Top';
				oContentDiv.className = sClassName + 'Content';
				oBottomDiv.className = sClassName + 'Bottom';
				sCurrentClassName = sClassName;
			}
			
			fOldDocumentMouseMoveHandler = document.onmousemove;
			document.onmousemove = this.position;
			
			oTooltip.style.display = 'block';
			oContentDiv.innerHTML = sContentHTML;
			oTooltip.style.width = nWidth ? nWidth + 'px' : 'auto';
			
			if (!nWidth && bIsIE) {
				oTopDiv.style.display = 'none';
				oBottomDiv.style.display = 'none';
				oTooltip.style.width = oTooltip.offsetWidth;
				oTopDiv.style.display = 'block';
				oBottomDiv.style.display = 'block';
			}
			
			if (oTooltip.offsetWidth > nMaxWidth) {
				oTooltip.style.width = nMaxWidth + 'px';
			}
			
			nHeight = parseInt (oTooltip.offsetHeight) + nTopOffset;
			clearInterval (oTooltip.timer);
			oTooltip.timer = setInterval (function () { Tooltip.fade (1); }, nTimer);
			
		},
		
		position: function (oEvent) {
			var _t = bIsIE ? event.clientY + document.documentElement.scrollTop : oEvent.pageY;
			var l = bIsIE ? event.clientX + document.documentElement.scrollLeft : oEvent.pageX;
			
			oTooltip.style.top = (((_t - nHeight) >= 0) ? (_t - nHeight) : _t) + 'px';
			oTooltip.style.left = (l + nLeftOffset) + 'px';
			
		},
		
		fade: function (nDirection) {
			var nA = nAlpha;
			
			if ((nA != nEndAlpha && nDirection == 1) || (nA != 0 && nDirection == -1)) {
				var i = nSpeed;
				
				if (nEndAlpha - nA < nSpeed && nDirection == 1) {
					i = nEndAlpha - nA;
				} else if (nAlpha < nSpeed && nDirection == -1) {
					i = nA;
				}
				
				nAlpha = nA + (i * nDirection);
				oTooltip.style.opacity = nAlpha * 0.01;
				oTooltip.style.filter = 'alpha(opacity=' + nAlpha + ')';
				
			} else {
				clearInterval (oTooltip.timer);
				
				if (nDirection == -1) {
					oTooltip.style.display = 'none';
				}
			}
		},
		
		hide: function () {
			clearInterval (oTooltip.timer);
			oTooltip.timer = setInterval (function () { Tooltip.fade (-1); }, nTimer);
			document.onmousemove = fOldDocumentMouseMoveHandler;
		}
		
	};
} ();
