function keepInWindow() {
	$(this).css("top", "-2px");
	var pageHeight = parseInt($(window).height(), 10);
	var objHeight = parseInt($(this).height(), 10);
	var offsetY = parseInt($(this).offset().top - $(window).scrollTop(), 10);
	var top = 0;
	if (offsetY < 0)
		top -= offsetY;
	else if (offsetY + objHeight > pageHeight)
		top -= Math.min(offsetY, (offsetY + objHeight) - pageHeight);
	if (top != 0)
		$(this).css("top", (top - 2).toString() + "px");
}

$(document).ready(function () {
	$("ul#nav li ul li").mouseenter(function () {
		$(this).children("div").each(keepInWindow);
	});
});

