$(document).ready(function() {
	
	// Options
	var distance = 10;
	var time = 250;
	var hideDelay = 500;
	var hideDelayTimer = null;
	var offsetY = -20;
	var offsetX = -200;
	
	// Tracker
	var beingShown = false;
	var shown = false;
	var trigger = $(this);
	var popup = $(".email_popup").css("opacity", 0);
	
	$("a.email_popup_link").each(function () {
								   
		$(this).click(function(e) {
			
			// 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 being shown, or already visible
			if(beingShown || shown) {
				return;
			} else {
				beingShown = true;
				
				//Reset position of popup box to where we clicked
				popup.css({
					top: e.pageY + offsetY,
					left: e.pageX + offsetX,
					display: "block"
				})
				
				// (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;
				});
			}
		}).mouseout(null, hideDelay);
	});
	
});

/**
  * Function to create the email box
  */
function createEmailBox(relativeURL, emailaddress, name) {
	try {
		var iframe = document.getElementById("emailBox");
		iframe.setAttribute("src", relativeURL + "Email.php?email=" + emailaddress + "&name=" + name);
		return false;
	} catch(ex) {
		return true;
	}
}
