(function($){
	$.fn.bubble = function(options) {
		
		var settings = $.extend({
			distance: 10,
			duration: 250,
			hidedelay: 250,
			popup: '.popup'
		}, options);

		return this.each(function(e) {
			var shown = false;
			var beingShown = false;
			var hideDelayTimer = null;			
			var popup = $(settings.popup, this);
			var h = popup.outerHeight();
			var w = popup.outerWidth();

			$(this).hover(function() {
				var off = $(this).position();

				if (hideDelayTimer) {
					clearTimeout(hideDelayTimer);
				}
				if (beingShown || shown) {
					// don't trigger the animation again
					return;
				} else {
					// reset position of popup box
					beingShown = true;

					popup.css({
						display: 'block',
						top: off.top - (h+5),
						left: off.left - (w/4)
					}).animate({
						top: '-=' + settings.distance + 'px',
						opacity: 1
					}, settings.duration, 'swing', function() {
						beingShown = false;
					});
				}
			}, function(e) {			
				if (hideDelayTimer) {
					clearTimeout(hideDelayTimer);
				}
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					popup.animate({
						top: '-=' + settings.distance + 'px',
						opacity: 0
					}, settings.duration, 'swing', function () {
						beingShown = false;
						popup.css('display', 'none');
					});
				}, settings.hidedelay);
			});
			


		});
		
	};
})(jQuery);
