$(document).ready(function(){

	$('#nav ul li[class!=nav_line]').each(function(index){
		$(this).mouseover(function(){
			
			if(!$(this).is('[hover=true]')){
				$('#nav .nav_hover').each(function(subIndex){
					if(!$(this).is('[hover=true]')){
						$(this).removeClass('nav_hover');
						$(this).addClass('nav_off');
					}
				});
				
				$(this).removeClass('nav_off');
			    $(this).addClass('nav_hover');
				$('.sub').hide();
				$('#sub'+index).show();
			}
		});
		
		$(this).click(function(){
			//如果点击的1级菜单不存在子菜单 或 设置了属性 linkable="true" 则 切换菜单状态 否这不改变状态
			var clickable = false;
			var obj = $(this).context;
				$('#nav ul li[class!=nav_line]').each(function(index){
					if(($(this).context)==obj){
						if($(this).attr('linkable')=="true" || $('#sub'+index).length==0)
							clickable = true;
					}
				});

			if(clickable){
				$('#nav ul li[class!=nav_line]').each(function(index){
					if(($(this).context)==obj){
						if(!($(this).attr('hover')=='true')){
							$('.sub[hover=true]').attr('hover','false');
							$('#sub'+index).attr('hover','true');
						}
					}
				});
		 
				if(!$(this).is('[hover=true]')){
					var menuLi = $('#nav .nav_hover[hover=true]');
					menuLi.removeClass('nav_hover');
					menuLi.addClass('nav_off');
					menuLi.attr('hover','false');
					$(this).attr('hover','true');
				}
			}else return;

		});
	});
	/*
	$('#navMenu').mouseout(function(e){
	
		//存在div冒泡
		$('#nav ul li[class!=nav_line]').each(function(){

			if(!$(this).is('[hover=true]')){
				$(this).removeClass('nav_hover');
				$(this).addClass('nav_off');
			}
		});
		
		$('.sub').hide();
		$('.sub[hover=true]').show();
	});*/
	
	$('#navMenu').hover(function(){},function(){
	
		$('#nav ul li[class!=nav_line]').each(function(){

			if(!$(this).is('[hover=true]')){
				$(this).removeClass('nav_hover');
				$(this).addClass('nav_off');
			}
		});
		
		$('.sub').hide();
		$('.sub[hover=true]').show();
	});

	$('.sub li').click(function(){
		
		$('.sub').each(function(){
			$(this).attr('hover','false');
		});
		var subParent = $(this).parent().parent();
		subParent.attr('hover','true');
		$('.sub').hide();
		$('.sub[hover=true]').show();
		
		var id = subParent.attr('id');
		var menuIndex = id.substring(3,id.length);
		
		$('#nav ul li[class!=nav_line]').each(function(index){
		    if(menuIndex==index){
				$(this).removeClass('nav_off');
				$(this).addClass('nav_hover');
				$(this).attr('hover','true');
			}
			else{
				$(this).removeClass('nav_hover');
				$(this).addClass('nav_off');
				$(this).attr('hover','false');
			}
		});

	});
});
