/* Taken from http://www.ajaxupdates.com/jquery-popup-bubbles/ with a few tweaks to make sure
 only 1 bubble is allowed at a time */
$(initBubbles);
    
function initBubbles() {
	$('.bubbleInfo').each(initBubble);
}

var currentBubble; // this is the current bubble that is visible
var beingShown = false;
var shown = false;

function initBubble() {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);


		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (info[0]!=currentBubble && currentBubble!=null) {
				$(currentBubble).css('opacity',0);
				$(currentBubble).css('display','none');
				$(currentBubble).stop();
				beingShown=false;
				shown=false;
			}
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;

				currentBubble=info[0];

				info.css({
					top: -45,
					left: -7,
					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 () {
				
				if (info[0]==currentBubble) {
					hideDelayTimer = null;
					info.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
					});
				} else {
					info.css('display', 'none');
				}

			}, hideDelay);

			return false;
		});
}