$(document).ready(function(){
	var mouseovermenu;
	var xpos = 0;
	var ypos = 0;
	var tmpx = 0;
	var tmpy = 0;
	var hovertimer;
	var okcounter = 0;
	var threshold = 20;
	$('#nav li').hover(
		function () {
			//Show subment
			//$('ul', this).slideDown(100);
			mouseovermenu = this;
			okcounter = 0;
			if(hovertimer != undefined){
				window.clearInterval(hovertimer);
			}
			hovertimer = window.setInterval(checkposition, 10);
		},
		function () {
			//Hide submenu
			if(hovertimer != undefined){
				window.clearInterval(hovertimer);
			}
			$('ul:not(.navlinkbg)', this).slideUp(100);
		}
	);

	$(document).mousemove(function(e){
		xpos = e.pageX;
		ypos = e.pageY;
	});

	function checkposition(){
		if(tmpx >= (xpos - 3) && tmpx <= (xpos + 3) && tmpy >= (ypos - 3) && tmpy <= (ypos + 3)){
			okcounter++;
			if(okcounter > threshold){
				window.clearInterval(hovertimer);
				$('ul:not(.navlinkbg)', mouseovermenu).slideDown(100);
			}
		}
		else{
			okcounter = 0;
		}
		tmpx = xpos;
		tmpy = ypos;
	}
});

