$(function () {
  $('.bubbleInfo').each(function () {
    // options
    var distance = 10;
	var popup_distance = 20;
    var time = 125;
    var hideDelay = 200;
    var toppos, leftpos;
	var zindex = -1;
    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);
	var content = $('.popup-content', this);
	var loading = $('.popup-loading', this);
	
    leftpos=(trigger.width()-popup.width())/2+trigger.position().left;
    toppos=trigger.position().top + popup_distance;
	
	popup.css({
		width: popup.width()+40+'px', 
		height: popup.height(),
		zIndex: -1,
        left: leftpos
    });
	
	content.css('background-color','#FFFFFF');
	
	loading.css({
		backgroundImage: "url('/images/spinner.gif')",
		backgroundPosition: '50% 50%',
		backgroundRepeat: 'no-repeat',
		width: content.width()+'px',
		height: content.height()+'px'
	});
	
	if (!$.browser.msie) {
		popup.css({backgroundColor:'transparent'});
	}
	
	if (!$.browser.opera) {
		popup.css({top:toppos});
	}
	
	popup.css({display: 'none', zIndex: -1, opacity: 0});
	
	$([popup.get(0)]).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
	});
    $([trigger.get(0)]).click(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      if (beingShown || shown) {
        return false;
      } else {
        beingShown = true;
		content.show();
        popup.css({
			//top: toppos,
			left: leftpos,
			zIndex: 50,
			opacity: 100,
			display: 'block' // brings the popup back in to view
        })
		.show();
        beingShown = false;
        shown = true;
      }
	  return false;
    });
	$([trigger.get(0),popup.get(0)]).mouseout(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          opacity: 0
        }, time, 'swing', function () {
          shown = false;
          popup.css({display: 'none', zIndex: -1});
        });
      }, hideDelay);
    });
  });
});
