
$(function () {
	$.fn.extend({
		tooltip: function() {
			var distance = 10;
			var time = 300;
			var showDelay = 500;
			var hideDelay = 500;
		
			var showDelayTimer = null;
			var hideDelayTimer = null;
		
			var beingShown = false;
			var shown = false;
			
			var trigger = $('.tooltip-trigger:first', this);
			var info = $('.tooltip-popup:first', this).css('opacity', 0);
			
			$([trigger.get(0), info.get(0)]).mouseover(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					return;
				} else {
					showDelayTimer = setTimeout(function() {
						beingShown = true;
						info.css({
							top: trigger.offset().top + 10,
							left: trigger.offset().left,
							display: 'block'
						}).animate({
							top: '+=' + distance + 'px',
							opacity: 1 
						}, time, 'swing', function() {
							beingShown = false;
							shown = true;
						});
					}, showDelay);
				}
				return false;
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (showDelayTimer) clearTimeout(showDelayTimer);
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					info.animate({
						top: '+=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
					});
			
				}, hideDelay);
		
				return false;
			});
		}
	});
});


