var showURL = true;
var certainCSSOnly = false;
var triggerClasses = false;
var triggerElements = ['a', 'abbr', 'acronym', 'img'];
var displayTimeout = 350;
var fadeDuration = 25;
var endOpac = 80;
var posx = posy = 0;
var xsToolTips = {
	obj: {},
	tip: {},
	init: function() {
		if (!document.getElementById || !document.createElement || !document.getElementsByTagName) {
			return;
		}
		var i, j, k;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		this.tip.className = 'xsToolTip';

		this.tipImage = document.createElement('img');
		this.tipImage.src = 'templates/xhtmlsuite/images/tooltip.gif';
		this.tipImage.id = 'toolTipImg';

		document.getElementsByTagName('body')[0].appendChild(this.tipImage);
		document.getElementsByTagName('body')[0].appendChild(this.tip);

		this.tipImage.width = '15';
		this.tipImage.height = '15';
		this.tipImage.style.position = 'absolute';
		this.tipImage.style.display = 'none';
		this.tipImage.style.top = '0';
		this.tipImage.style.visibility = 'hidden';

		this.tip.style.display = 'none';
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = triggerElements.length;

		for (i=0; i<tipLen; i++) {
			var current = document.getElementsByTagName(triggerElements[i]);
			var curLen = current.length;
			for (j=0; j<curLen; j++) {
				if (certainCSSOnly && typeof triggerClasses != 'undefined' && triggerClasses !== '') {
					if (!current[j].className.match(triggerClasses)) {
						continue;
					}
				}
				addEvent(current[j], 'mouseover', this.tipOver);
				addEvent(current[j], 'mouseout', this.tipOut);
				current[j].setAttribute('tip', current[j].title);
				current[j].removeAttribute('title');
				current[j].removeAttribute('alt');
			}
		}
	},
	tipOut: function() {
		if (window.tID) {
			clearTimeout(tID);
		}

		if (window.opacityID) {
			clearTimeout(opacityID);
		}
		xsToolTips.tipImage.style.visibility = xsToolTips.tip.style.visibility = 'hidden';
	},
	checkNode: function() {
		var trueObj = this.obj;

		if (triggerElements.inArray(trueObj.nodeName.toLowerCase())) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver: function(e) {
		var ev = e;
		xsToolTips.obj = this;
		tID = window.setTimeout(function() {
			xsToolTips.tipShow(ev);
		}, displayTimeout);
	},
	tipShow: function() {
		var scrX = posx;
		var scrY = posy;
		var tp = parseInt(scrY + 20);
		var lt = parseInt(scrX + 10);
		var anch = this.checkNode();
		var addy = '';
		var access = '';

		if (showURL) {
			if (anch.nodeName.toLowerCase() == 'a') {
				addy = (anch.href.length > 25 ? anch.href.toString().substring(0, 25) + "..." : anch.href);
				var access = (anch.accessKey ? ' <span>[' + anch.accessKey + ']</span> ' : '');
			}
		}

		this.tipImage.style.display = '';
		this.tip.style.display = '';

		actualToolTip = anch.getAttribute('tip');
		actualToolTip = actualToolTip.replace(/\[n\]/g, '<br />');
		this.tip.innerHTML = '<p>' + actualToolTip + '<em>' + access + addy + '</em></p>';

		/*if (parseInt(document.documentElement.clientWidth + document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth + lt)) {
			this.tipImage.style.left = parseInt(lt - (this.tip.offsetWidth - 5)) + 'px';
			this.tip.style.left = parseInt(lt - (this.tip.offsetWidth + 10)) + 'px';
		} else {*/
			this.tipImage.style.left = lt + 15 + 'px';
			this.tip.style.left = lt + 'px';
		//}

		/*if (parseInt(document.documentElement.clientHeight + document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight + tp)) {
			this.tipImage.style.top = (tp - 50) + 'px';
			this.tip.style.top = parseInt(tp - (this.tip.offsetHeight + 10)) + 'px';
		} else {*/
			this.tipImage.style.top = (tp - 15) + 'px';
			this.tip.style.top = tp + 'px';
		//}

		this.tipImage.style.visibility = this.tip.style.visibility = 'visible';
		this.tipImage.style.opacity = this.tip.style.opacity = '.1';
		this.tipFade(10);
	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed + 10);

		if (newOpac < endOpac) {
			this.tip.style.opacity = this.tipImage.style.opacity = '.' + newOpac;
			this.tip.style.filter = this.tipImage.style.filter = "alpha(opacity:" + newOpac + ")";
			opacityID = window.setTimeout("xsToolTips.tipFade('" + newOpac + "')", fadeDuration);
		} else {
			this.tip.style.opacity = this.tipImage.style.opacity = '.' + endOpac;
			this.tip.style.filter = this.tipImage.style.filter = "alpha(opacity:" + endOpac + ")";
		}
	}
};

function mouseXY(e) {
	if (!e)
		var e = window.event;

	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		try {
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		} catch (ex) {}
	}
};

addEvent(document, 'mousemove', mouseXY);
addEvent(document, 'mousedown', xsToolTips.tipOut);
addEvent(window, 'load', function() {
	xsToolTips.init();
});