$(document).ready(function() {
	var subnav_current;
	var subnav_timeout;
	$.each($("#nav a"), function() {
		if (this.className.length > 0) {		
			var subnav_item = "#subnav_" + this.className;
			var offset_left = (Math.floor($(this).offset().left - $("#container").offset().left) - 20);
			var offset_right = (Math.floor(($(this).offset().left + $(this).width()) - $("#container").offset().left) - 20) - ($(subnav_item).width() - 8);
			if (offset_left < 715) {
				$(subnav_item).css("margin-left", offset_left);
			} else {
				$(subnav_item).css("margin-left", offset_right);
			}
			$("#nav ." + this.className).mouseover(function() {
				var targ = this.className;
				if (targ != subnav_current) {
					$("#subnav_" + targ).fadeIn("fast");
					$("#subnav_" + subnav_current).css('display', 'none');
				}
				subnav_current = targ;
				clearTimeout(subnav_timeout);
			});
			$("#nav ." + this.className).mouseout(function() {
				subnav_timeout = setTimeout(hideSubNav, 500);								
			});
		}						
	});
	$.each($("#subnav a"), function() {
		$(this).mouseover(function() {
			clearTimeout(subnav_timeout);
		});
		$(this).mouseout(function() {
			subnav_timeout = setTimeout(hideSubNav, 500);
		});
	});
	function hideSubNav() {
		$("#subnav ul").fadeOut("fast");
		subnav_current = null;
	}
});