function Menu(id) {
	var timer = null, show = false,
	
	flip = function (obj) {
		return function () {
			$(id + ' > li').removeClass('hover');
			if (show) {
				obj.addClass('hover');
			}
			timer = null;
		};
	};
	
    $(id).addClass('js');
	$(id + ' > li').hover(
		function () {
			if (timer !== null) {
				clearTimeout(timer);
				timer = null;
			}
			show = true;
			timer = setTimeout(flip($(this)), 200);
		},
		function () {
			if (timer !== null) {
				clearTimeout(timer);
				timer = null;
			}
			show = false;
			timer = setTimeout(flip($(this)), 200);
		}
	);
}

$(document).ready(function(e) {
	var menu = new Menu('#nav'),
	menu2 = new Menu('#sub-nav');
});
