﻿function CloseAllBubbles()
{for (var x = 0;x<AllBubbles.length;x++)
	{AllBubbles[x].CloseButton.onclick();
	}
}

var mouseX = 0;
var mouseY = 0;
$().mousemove(function(e) { mouseX = e.pageX; mouseY = e.pageY; });

var AllBubbles = new Array()
$(function()
	{$('.Bubble').each(function()
		{// options
		var distance = 10;
        var time = 250;
        var hideDelay = 100;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;
		var	FollowHorizontal = null;
        var Trigger = $('.Trigger', this);
		FollowHorizontal = Trigger[0].getAttribute("FollowHorizontal");
		var PopUpWindow = $('.Popup', this);
        var popup = PopUpWindow.css('opacity', 0);
		popup[0].setAttribute("extraXVal", 0)
		popup[0].setAttribute("extraYVal", 500)
		var CloseTrigger = $('.CloseButton', PopUpWindow[0]);
		AllBubbles[AllBubbles.length] = {PopUp:popup, CloseButton:CloseTrigger[0]};
		CloseTrigger[0].onclick = function()
				{// reset the timer if we get fired again - avoids double animations
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
					setTimeout(function()
									{hideDelayTimer = null;
									popup.animate(
										{top: '-=' + distance + 'px',
										opacity: 0
										},
										time,
										'swing',
										function()
											{// once the animate is complete, set the tracker variables
											shown = false;
											// hide the popup entirely after the effect (opacity alone doesn't do the job)
											popup.css('display', 'none');
											}
										);
									}, hideDelay);

				};

        // set the mouseover and mouseout on both element
        $([Trigger.get(0), popup.get(0)])
			.click(function()
				{for (var x = 0;x<AllBubbles.length;x++)
					{if (AllBubbles[x].PopUp != popup)
						{AllBubbles[x].CloseButton.onclick();
						}
					}
				// stops the hide event if we move from the Trigger to the popup element
				if (hideDelayTimer) clearTimeout(hideDelayTimer);

				// don't Trigger the animation again if we're being shown, or already visible
				if (beingShown || shown)
					{if (popup[0].getAttribute("reset") != null)
						{popup[0].removeAttribute("reset");
						popup.css
							(	{top: 0 + (/true/i.test(FollowHorizontal)?mouseY - popup[0].getAttribute("extraYVal") + document.body.scrollTop:0),
								left: -50 + popup[0].getAttribute("extraXVal"),
								zIndex:3,
								display: 'block' // brings the popup back in to view
								}
							)
						}
					return;
					}
				else{beingShown = true;
					popup[0].removeAttribute("reset");
					// reset position of popup box
					//FollowHorizontal
					popup.css
						(	{top: 0 + (/true/i.test(FollowHorizontal)?mouseY - popup[0].getAttribute("extraYVal") + document.body.scrollTop:0),
							left: -50 + popup[0].getAttribute("extraXVal"),
							zIndex:3,
							display: 'block' // brings the popup back in to view
							}
						)
					// (we're using chaining on the popup) now animate it's opacity and position
						.animate(
							{top: '-=' + distance + 'px',
							opacity: 1
							},
							time,
							'swing',
							function()
								{// once the animation is complete, set the tracker variables
								beingShown = false;
								shown = true;
								}
							);
					}
				}
				)
		});
	});