// JavaScript Document

var t;
var allowHide = true;
var h;
var w;

$(document).ready(function() {
	var ttString = '<table class="blackTooltip" border="0" cellspacing="0" cellpadding="0"><tr><td class="content">';
	ttString += '<div class="title"></div><span class="desc"></span><br /><a id="link" href="">more details &raquo;</a>';
	ttString += '</td></tr><tr><td class="tail">&nbsp;</td></tr></table>';
	
	$('body').prepend(ttString);
	$('.blackTooltip').mouseenter(function() { allowHide = false; });
	$('.blackTooltip').mouseleave(function() { allowHide = true; hideTooltip(); });
});

function showTooltip(t, d, l, o) {
	var p = $(o).offset();
	$('.blackTooltip .content .title').html(t);
	$('.blackTooltip .content .desc').html(d);
	
	if(l == '') {
		$('.blackTooltip .content #link').hide();
	} else {
		$('.blackTooltip .content #link').attr('href', l);
	}
	h = $('.blackTooltip').height();
	w = $('.blackTooltip').width();
	h = Math.round(p.top)-h+18;
	w = Math.round(p.left)-(w/2 - ($(o).width() / 2));
	
	var gap;
	var tailPost;
	if(w < 0) {
		gap = Math.abs(w);
		tailPost = 150 - gap;
		w = 0;
		$('.blackTooltip .tail').css('background-position', tailPost + 'px top');
	} else {
		if( (w + 310) > $(window).width() ) {
			gap = (w + 310) - $(window).width();
			tailPost = 150 + gap;
			w = $(window).width() - 310;
			$('.blackTooltip .tail').css('background-position', tailPost + 'px top');
		} else {
			$('.blackTooltip .tail').css('background-position', '150px top');
		}
	}

	$('.blackTooltip').css( {'left':w+'px', 'top':h+'px'} );
	//$('.blackTooltip').fadeIn(300);
	$('.blackTooltip').show();
}

function hideTooltip() {
	t = setTimeout(function() {
		if(allowHide) {
			//$('.blackTooltip').fadeOut(300);
			$('.blackTooltip').hide();
			allowHide = true; 
		}
	}, 0);

}
