/* Tooltip from http://jqueryfordesigners.com/demo/coda-bubble.html */
$(function () {
    $('.bubbleInfo').each(function () {
        var distance = 10;
        var time = 250;
        
		var showDelay = 300;
        var showDelayTimer = null;

        var hideDelay = 100;
        var hideDelayTimer = null;

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

		var triggerW = trigger.width() + 40;


        $([trigger.get(0), info.get(0)]).mouseover(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            
            if (showDelayTimer) clearTimeout(showDelayTimer);
            	showDelayTimer = setTimeout(function () {
                showDelayTimer = null;            
                
	            if (beingShown || shown) {
	                // don't trigger the animation again
	                return;
	            } else {
	                // reset position of info box
	                beingShown = true;
	
	                info.css({
	                    top: -18,
	                    left: triggerW ,
	                    display: 'block'
	                }).animate({
	                    left: '-=' + distance + 'px',
	                    opacity: 1
	                }, time, 'swing', function() {
	                    beingShown = false;
	                    shown = true;
	                });
	            }
            }, showDelay);

            return false;
        }).mouseout(function () {
            if (showDelayTimer) clearTimeout(showDelayTimer);
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            	hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    left: '+=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });

            }, hideDelay);

            return false;
        }).click(function () {
            if (showDelayTimer) clearTimeout(showDelayTimer);
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            	hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                shown = false;
                info.css('display', 'none');
            }, hideDelay);

            return false;
        });
    });
});

