var Tooltips = {

	offsetX : 10,
	offsetY : -20,
	cache : [],

	init : function(cssClass)
	{
		this.elements = $('.' + cssClass);
		
		var scope = this;
		this.elements.each(function(ix)
		{
			$(this).click(function(e){ scope.trigger(this,e); e.stopPropagation(); });
		});
		
		
		$(document).click(function(e){ scope.hide(this,e); });
		
		
		var el = document.createElement('div');
		var inner = document.createElement('div');
		
		document.body.appendChild(el);
		el.appendChild(inner);
		
		el.id = 'TT';
		inner.className = 'inner';
		
		
		this.content = $(inner);
		this.root = $(el);

		$(this.root).click(function(e){ e.stopPropagation(); });
		
		this.root.hide();
	},
	
	trigger : function(src, e)
	{
		this.event = e;
		var id = src.title;
		var scope = this;
		
		if(this.cache[id]) { this.show(this.cache[id]); }
		else { $.get('/help.php?id=' + id, function(data) { scope.ajaxReceived(id,data); }); }
	},
	
	ajaxReceived : function(id, data)
	{
		if(!data) { this.hide(); return; }
		this.cache[id] = data;
		this.show(data);
	},
	
	show : function(content)
	{
		var e = this.event;
		this.content.html(content);
		
		this.root.css({
			'left' : e.pageX + this.offsetX,
			'top' : e.pageY - this.root.height() + this.offsetY
		});
	
		this.root.fadeIn(300);
	},
	
	hide : function()
	{
		this.root.fadeOut(300);
	}
	
}
