$(function () {
	var distance = 10;
	var time = 500;
	var hideDelay = 1000;

	var hideDelayTimer = null;

	var beingShown = false;
	var shown = false;
	var info = $('.popup').css('opacity', 0);

	$(".trigger").mouseover(function () {
		var trigger = $(this);
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		if (beingShown || shown) {
			return ;
		} else {
			beingShown = true;
			if (trigger.attr("help")) info.html(trigger.attr("help"));	
			else if (trigger.attr("key")) getTextFromBase(trigger.attr("key"), info);
			info.css({
				top: -40,
				left: -33,
				display: 'block'
			}).animate({
				top: '-=' + distance + 'px',
				opacity: 1
			}, time, 'swing', function() {
				beingShown = false;
				shown = true;
			});
		}
		return false;
	}).mouseout(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		hideDelayTimer = setTimeout(function () {
			hideDelayTimer = null;
			info.animate({
				top: '-=' + distance + 'px',
				opacity: 0
			}, time, 'swing', function () {
				shown = false;
				info.css('display', 'none');
			});
		}, hideDelay);
		return false;
	});
});

function getTextFromBase(key, info)
{
	$.post('/ajax/help/getHelp.php', 
			{
                key:  key,
                rand: Math.random()
            },
            function (data) {
                if (data != "ERROR") info.html(data);
            }
	);
}
