function Tooltip(divid,contentcallback){
	this.divid = divid;
	this.speed = 10;
	this.step = 10;
	this.defaulttext = "";
	this.callback = contentcallback;
	/* This setting decides whether the tooltip appears to the 
	left or right of the parent item */
	this.horizontalalign = 'left';
	this.useslide = true;
	this.divclass = 'ttpdiv';
	/* This setting allows the tooltip to compensate for a 
	scolling internal element, but requires well formed code.
	Most notably, form tags cannot be intermingled with table,
	tr and td tags */
	this.compensateforinternalscroll = false;
	
	this.divElement = null;
	this.divElementStyle = null;
	this.height = 0;
	this.width = 0;
	this.opening = false;
	this.isopen = false;
	this.closing = false;
	this.loaded = false;
	this.obj = "Tooltip_" + (++ Tooltip.instance);
	eval (this.obj + "=this");
}
Tooltip.instance = 0;
	
Tooltip.prototype.getElement = function (elementId) {
	var element;
	if (document.all)
	element = document.all[elementId];
	else if (document.getElementById)
	element = document.getElementById(elementId);
	return element;
}
Tooltip.prototype.getStyle = function (element) {
	if (element) return document.layers ? element : element.style;
	else return null;
}

Tooltip.prototype.setPosition = function(owner){
	this.divElementStyle.top = this.getAbsTop(owner) + owner.offsetHeight;
	this.divElementStyle.left = this.getAbsLeft(owner) - (this.horizontalalign=='left'?this.width:-owner.offsetWidth);
}

Tooltip.prototype.closeOtherInstances = function(){
	for(var i=1; i <= Tooltip.instance; i++){
		var obj = "Tooltip_" + i;
		if(obj != this.obj){
			eval(obj + ".close()");
		}
	}
}

Tooltip.prototype.open = function (owner){
	if(!this.loaded) return;
	if(this.isopen){
		return this.close();
	}
	if(!this.useslide){
		this.drawContent(owner);
		this.setPosition(owner);
		this.closeOtherInstances();
		this.divElementStyle.display = 'block';
		this.divElementStyle.visibility = 'visible';
		this.clear();
		this.isopen = true;
	}else if(!this.opening && !this.closing){
		this.opening = true;
		this.drawContent(owner);
		this.setPosition(owner);
		this.closeOtherInstances();
		this.divElementStyle.visibility = 'hidden';
		this.divElementStyle.display = 'block';
		if(!this.height) this.setSize();
		this.divElementStyle.height = this.step+"px";
		this.divElementStyle.visibility = 'visible';
		this.clear();
		this.interval = setInterval(this.obj + '.openincrement()',this.speed);
	}
}
Tooltip.prototype.openincrement = function(){
	var height = parseInt(this.divElementStyle.height);
	if(height < this.height){
		var step = Math.min(this.step,this.height-height);
		this.divElementStyle.height = (height + step) + "px";
	}else{
		this.clear();
		this.opening = false;
		this.isopen = true;
	}
}

Tooltip.prototype.close = function (){
	if(!this.useslide){
		this.clear();
		this.divElementStyle.display = 'none';
		this.opening = false;
		this.isopen = false;
	}else if(this.isopen || this.opening){
		this.clear();
		this.opening = false;
		this.interval = setInterval(this.obj + '.closeincrement()',this.speed);
	}
}
Tooltip.prototype.closeincrement = function(){
	var height = parseInt(this.divElementStyle.height);
	if(height <= this.step){
		this.clear();
		this.divElementStyle.display = 'none';
		this.isopen = false;
	}else{
		var step = Math.min(this.step,height);
		this.divElementStyle.height = (height-step) + "px";
	}
}
Tooltip.prototype.clear = function () {
	clearInterval (this.interval);
	this.interval = null;
}

Tooltip.prototype.drawContent = function(owner){
	if(this.callback){
		var content = eval(this.callback + '(owner)');
		if(content.length > 0){
			this.divElement.innerHTML = content;
		}
	}
}

Tooltip.prototype.build = function(){
	document.write('<div id="' + this.divid + '" class="' + this.divclass + '"></div>');
	this.divElement = this.getElement(this.divid);
	this.divElementStyle = this.getStyle(this.divElement);
	
	var html = '<table class="ttptable" align="center">';
	html += '<tr><td align="center">' + this.defaulttext + '</td></tr>';
	html += '</table>';
	this.divElement.innerHTML = html;
	
	this.divElementStyle.display = 'none';
	this.loaded = true;
}

Tooltip.prototype.init = function(){
	this.divElement = this.getElement(this.divid);
	this.divElementStyle = this.getStyle(this.divElement);	
	this.divElementStyle.display = 'none';
	this.loaded = true;
}

Tooltip.prototype.getAbsTop = function (o) {
	if(this.compensateforinternalscroll){
		oTop = o.offsetTop - o.scrollTop;
		oOffset = null;
		while(o.parentNode && o.parentNode.tagName.toUpperCase() != 'BODY') {
			oParent = o.parentNode;
			if(oOffset != o.offsetParent && o.offsetParent){
				oOffset = o.offsetParent;
				oTop += oOffset.offsetTop;
			}
			oTop -= oParent.scrollTop
			o = oParent;
		}
		return oTop
	}else{
		oTop = o.offsetTop;
		while(o.offsetParent!=null) {
			oParent = o.offsetParent;
			oTop += oParent.offsetTop;
			o = oParent
		}
		return oTop;	
	}
}

Tooltip.prototype.getAbsLeft = function (o) {
	if(this.compensateforinternalscroll){
		oLeft = o.offsetLeft - o.scrollLeft;
		oOffset = null;
		while(o.parentNode && o.parentNode.tagName.toUpperCase() != 'BODY') {
			oParent = o.parentNode;
			if(oOffset != o.offsetParent && o.offsetParent){
				oOffset = o.offsetParent;
				oLeft += oOffset.offsetLeft;
			}
			oLeft -= oParent.scrollLeft
			o = oParent;
		}
		return oLeft
	}else{
		oLeft = o.offsetLeft
		while(o.offsetParent!=null) {
			oParent = o.offsetParent
			oLeft += oParent.offsetLeft
			o = oParent
		}
		return oLeft
	}
}

Tooltip.prototype.setSize = function(){
	this.height = this.divElement.offsetHeight;
	this.width = this.divElement.offsetWidth;
	this.divElementStyle.width = this.width + 'px';
}


function _tooltip_drawloading(targetelement){
	var target = document.getElementById(targetelement);
	var loading = '<table width="100%" height="100%" class="ttploading"><tr><td align="center">Loading...</td></tr></table>';
	target.innerHTML = loading;
}
