
var getCoordinates = function(origNode) {
	var node = origNode;
	var coords={x:0,y:0};
	if (node.offsetParent) {
		while (node.offsetParent) {
			coords.x += node.offsetLeft;
			coords.y += node.offsetTop;
			node = node.offsetParent;
		}
	}
	return coords;
}

function InfoTooltip(target, title, description, author, date) {
	this.target = target;
	this.title = title;
	this.description = description;
	this.author = author;
	this.date = date;
}

InfoTooltip.prototype.toHTML = function() {
	var html = '';
	if (this.title) html += '<b>' + this.title + '</b><br/>';
	if (this.description) html += this.description + '<br/>';
	if (this.title || this.description) html += '<br/>';
	if (this.author) html += 'Author: <b>' + this.author + '</b><br/>';
	if (this.date) html += 'Date: <b>' + this.date + '</b>';
	return html;
}

InfoTooltip.prototype.set = function(target, title, description, author, date) {
	this.target = target;
	this.title = title;
	this.description = description;
	this.author = author;
	this.date = date;
}

InfoTooltip.prototype.refresh = function() {
	if (!this.element) {
		this.element = document.createElement("DIV");
		this.element.setAttribute('id', 'tooltip');
		this.element.className = "tooltip";
		document.body.appendChild(this.element);
	}

	var coords = getCoordinates(this.target);

	this.element.style.display = '';
	this.element.innerHTML = this.toHTML();
	this.element.style.left = (coords.x + (document.all ? 1 : 0))  + 'px';
	this.element.style.top = (coords.y + this.target.offsetHeight - (document.all ? 0 : 1)) + 'px';
	//alert(this.toHTML());
}

var tooltip;

function startInfo(target, title, description, author, date) {
	if (tooltip) {
		if (tooltip.hideTimer) clearTimeout(tooltip.hideTimer);
		if (tooltip.element) {
			tooltip.set(target, title, description, author, date);
			tooltip.refresh();
			return;
		}
	}

	tooltip = new InfoTooltip(target, title, description, author, date);
	tooltip.showTimer = setTimeout("showInfo();", 350);
}

function showInfo() {
	tooltip.showTimer = null;
	tooltip.refresh();
}

function stopInfo() {
	if (tooltip) {
		if (tooltip.showTimer) clearTimeout(tooltip.showTimer);
		if (tooltip.element) tooltip.element.style.display = 'none';
		tooltip.hideTimer = setTimeout("hideInfo();", 500);
	}
}

function hideInfo() {
	if (tooltip) {
		if (tooltip.element) document.body.removeChild(tooltip.element);
		tooltip = null;
	}
}
