
$(function() {
	// email replacement.
	$(".email").each(function() {
		var address = $(this).text();
		address = address.replace('[at]', '&#64;');
		$(this).replaceWith('<a href="mailto:'+address+'">'+address+'</a>');
	});
	
	$(".dict-info").live("click", function(e) {
        e.preventDefault();

		var $link = $(this);
		var countdown = null;
		var dict = $link.attr("href").slice(1);
		
		$("div.dict-entry").remove();
		
		var $layer = $('<div class="dict-entry"><div class="ajax-loading"></div></div>').css({
			position: "absolute",
			top: $link.offset().top + "px",
			left: $link.offset().left + 16 + "px"
		}).appendTo("body");
		
		function restartCountdown() {
			stopCountdown();
			countdown = setTimeout(function() {
				$layer.remove();
			}, 800);
		}
		
		function stopCountdown() {
			if (countdown)
				clearTimeout(countdown);	
		}
		
		$link.hover(function() {
			$layer.remove();
		}, function() {
			restartCountdown();
		});
		
		$layer.hover(function() {
			stopCountdown();
		}, function() {
			restartCountdown();
		});
		
		$.get("/index.php?dict=" + dict, function(data) {
			var text = data.text;
			if (!text) {
				$layer.remove();	
			}
			$layer.html(text);
		}, 'json');
	});
});

