$(function() {
	$('.tooltip').hide();
	$('.trigger, .close a').live("click", function(e) {
	$tooltip  = $(this).find(".tooltip");
		if ($(this).is(".close a")) { 
			$(this).parent().parent().hide();
			return false;
		} 
		if ($tooltip.is(':hidden')) { 
			// Move Tooltip With Mouse
			offset = 20;
			mouseX = e.pageX + offset;
			mouseY = e.pageY + offset - 152;
			
			tooltipWidth = $tooltip.width() + parseInt($tooltip.css('padding-left').replace("px","")) + parseInt($tooltip.css('padding-right').replace("px","")) + parseInt($tooltip.css('border-left-width').replace("px","")) + parseInt($tooltip.css('border-right-width').replace("px",""));
			tooltipHeight = $tooltip.height() + parseInt($tooltip.css('padding-top').replace("px","")) + parseInt($tooltip.css('padding-bottom').replace("px","")) + parseInt($tooltip.css('border-top-width').replace("px","")) + parseInt($tooltip.css('border-bottom-width').replace("px",""));
			if(mouseX + tooltipWidth + offset > $(window).width()) { mouseX = mouseX - tooltipWidth - offset * 1.5; }
			if(mouseY + tooltipHeight + offset > $(window).height()) { mouseY = mouseY - tooltipHeight - offset * 1.5; }
			
			// Display Tooltip
			if (!$(this).children(".tooltip").children().hasClass(".close")) {
				$(this).children(".tooltip").prepend('<div class="close">X <a href="#">Close Window</a></div>');
			}
			$(this).children(".tooltip").css({ 'position': 'absolute', 'left' : mouseX+'px', 'top' : mouseY+'px', 'zIndex' : 1000 }).show();
			return false;
		}
	});
});