// findPos() finds the position of an element on the page.
// this function was taken from here: http://www.quirksmode.org/js/findpos.html
// and is used in the tooltip function below.

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}


// $().ToolTip() is a jquery plugin that creates a nice tooltip.
// this plugin was taken from here: http://sam.collett.googlepages.com/tooltip.html
// and was changed to fit my needs.
// usage:
//	$(window).load(function() {
//		$("a").ToolTip();
//	});

$.fn.ToolTip = function() {
	this.mouseover(function(e) {
		if ((!this.title && !this.alt) && !this.tooltipset) return;
			// if there is no sibling after this one, or the next siblings className is not tooltip
			if (!this.nextSibling || this.nextSibling.className != "tooltip") {
				// create a div
				var div = document.createElement("div");
				// add the title/alt attribute to it
				$(div).html((this.title || this.alt)).addClass("tooltip");
				this.title = "";
				this.alt = "";
				if (this.nextSibling) {
					this.parentNode.insertBefore(div, this.nextSibling);
				} else {
					this.parentNode.appendChild(div);
				}
				this.tooltipset = true;
			}
			
			// the position of the tooltip
			xy = findPos(this);
			x = xy[0] + this.width - 10;
			y = xy[1] + this.height - 10;
			// the tooltip
			$(this.nextSibling).show().css({left:x+"px", top:y+"px"});
		}
	).mouseout(function() {
		if (this.nextSibling && this.nextSibling.className == "tooltip"){
			$(this.nextSibling).hide();
		}
	});
	return this;
}
