﻿// Call the function for first load
$(function() {
  createPopup();
});

// This script ensures that the jQuery runs after ajax postbacks
$(document).ready(function() {

  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandlerMethod);

  function EndRequestHandlerMethod(sender, args) { createPopup(); }

});

// This is the jQuery that creates the popup
function createPopup() {

	$('.bubbleInfo').each(function() {
		var distance = 0; // $(this).height();
		var time = 250;
		var hideDelay = 0;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		//var info = $('.popup', this)

		var triggerTarget = $('.triggerTarget', this);
		var info = $('#' + triggerTarget.val()); // $('.popup', this)

		info.css('opacity', 0);

		$([trigger.get(0)]).mouseover(function() {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;

				info.css({
					top: 0,
					left: 0,
					display: 'block'
				}).animate({
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
			
			return false;
		}).mouseout(function() {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				info.animate({
					opacity: 0
				}, time, 'swing', function() {
					shown = false;
					info.css('display', 'none');
				});
			}, hideDelay);

			return false;
		});
	});

}