var showOptional = true;	//hide anything that isnt a ToolTip.

var tt = new ToolTip();

function ToolTip(){
	this.OFFSETX=10;
	this.OFFSETY=5;

	this.div = false;
	this.oldClassName = false;
	this.element = false;
	this.killCall = false;

	var me = this;

	this.show = function(element, isOptional){
		this.div = get('ToolTip');
		// if its optional and we hide optional, or if the div isnt loaded yet
		// or were already showing, quit.
		if (isOptional && !showOptional || !me.div || me.element) return;
		
		//set up our element by setting innerHTML and class
		me.element = element;
		me.div.innerHTML = element.getAttribute("ToolTip");
		me.oldClassName = element.className;
		element.className = me.oldClassName + " helpOn";

		//show it by attaching handler to document and fading in.
		me.div.style.display = "block";
		addEvent(document, 'mousemove', me.position);
		me.showCall = setTimeout(me.doShow, 800);
		//me.killCall = setTimeout(me.kill, 2000);	//kill it in 2 seconds!
	}
	this.doShow = function() { Fade(me.div, .3, 1); }

	this.position = function(e){
		var IS_IE = document.all;
		var ietruebody = (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
		var curX = (IS_IE) ? event.clientX + ietruebody.scrollLeft : e.pageX;
		var curY = (IS_IE) ? event.clientY + ietruebody.scrollTop : e.pageY;

		var rightEdge = (IS_IE && !window.opera) ? ietruebody.clientWidth + ietruebody.scrollLeft : window.innerWidth + window.pageXOffset - 25;
		var bottomEdge = (IS_IE && !window.opera) ? ietruebody.clientHeight + ietruebody.scrollTop : window.innerHeight + window.pageYOffset - 25;

		// Check if near bottom
		if (curY + me.OFFSETY + me.div.offsetHeight > bottomEdge) {
			curY = curY - me.div.offsetHeight - 2*me.OFFSETY;
		}else curY = curY + me.OFFSETY;

		// Right
		if (curX + me.OFFSETX + me.div.offsetWidth > rightEdge) {
			curX = rightEdge - me.div.offsetWidth - 2*me.OFFSETX;
		}else curX = curX + me.OFFSETX;

		me.div.style.left = curX  + "px";
		me.div.style.top = curY  + "px";
	}

	this.kill = function(){
		clearTimeout(me.killCall);
		me.element.className = me.oldClassName;
		me.oldClassName = false;
		me.element = false;
		document.onmousemove = null; me.div.style.display="none";
	}
}