
var gflag = 0;
var to = null;
var prev = 0;
var curID = 0;

function showMenu(m) {
	if (prev != 0) tmp = prev.substring(0,4);
	$("#"+tmp).removeClass('hover');
	if ($("#"+m).length == 0) { return true;}
	hideMenu(prev);
	prev = m;
	$("#"+curID).addClass('hover');
	$("#"+m).fadeIn(200);
	gflag = 1;
}

function hideMenu(m) {
	if ($("#"+m).length == 0) { return true; }
	if (gflag==0) {
		$("#"+m).fadeOut(200);
		$("#"+curID).removeClass('hover');
		clearTimeout(to);
		gflag = 0;
	}
}

function hideMenuTimed(m) {
	gflag = 0;
	to = setTimeout(function() {
		hideMenu(m);
	}, 1); 
}


function fadeOutMenu(m) {
	$("#"+m).fadeOut('200');
	clearTimeout(to);
}


$(document).ready(function(){
	
	$('.topMenu a').hoverIntent(
		function() {
			curID = $(this).attr("id");
			curPos = $(this).position();
			tmp = curID + '_sm';
			$("#"+tmp).css("left", curPos.left+"px");
			//$("#"+tmp).width($(this).outerWidth()-9);
			showMenu(tmp);
		},	
		function() {}	
	);
	
	$('.topMenu a').hover(
		function() {},
		function() {
			curID = $(this).attr("id");
			tmp = curID + '_sm';
			hideMenuTimed(tmp);
		}
	
	);
	
	$(".subMenu").hover(
		function() {					
			tmp = curID + '_sm';
			clearTimeout(to);				
		},
		function() {		
			tmp = curID + '_sm';
			hideMenuTimed(tmp);			
		}
	);
	
	
});
