function mainNavHit( x, y, name, ismy ){
	var hits = false;
	var off = $("#"+name).offset();
	var mxx = off.left + $("#"+name).width();
	var mxn = off.left;
	var myx = off.top + $("#"+name).height();
	var myn = off.top;
	if( x > mxx ){
		hits = true;
	}else if( x < mxn ){
		hits = true;
	}
	if( y > myx ){
		hits = true;
	}else if( y < myn ){
		hits = true;
	}
	if(!ismy){
		if(x < mxx-55){
			if( y < off.top+27){
				hits=true;
			}	
		}
	}
	return hits;
};
function hideMenu(){
	$("#userMenuList").hide();
	$("#qaMenuList").hide();
	$("#docMenuList").hide();
	$("#topicsMenuList").hide();
};
function new_login(){
	if(_isSubmit){
		return false;
	};
	
	var qUn = $(".nav_username");
	var qPw = $(".nav_password");
	
	var un = $.trim(qUn.val());
	var pw = $.trim(qPw.val());
	var r = false;
	
	_isSubmit = true;
	var pa={"username":un,"password":pw,"_spring_security_remember_me":r};
	$.getJSON("/ajax/checkedLoginUser.html",pa, function(o) {
		_isSubmit = false;
		if (o.map.isok) {
			$.tip.load({id:'navLogin',html:'/common/navLogin.html'});
		} else{
			var url = encodeURI(location.href);
			window.location = "/login.html?error=true&beforeLoginUrl="+url;
		}
	});
};
(function(){
	
	//初始化方法
	var init=function(){
		$("#userMenu").bind("mouseover",function(){
			hideMenu();
			$("#userMenuList").show();
			$(document).bind("click",function menuListClick(e){if(mainNavHit(e.pageX,e.pageY,"userMenuList",true)){$("#userMenuList").hide();$(document).unbind("click")}}); 
			$(document).bind("mousemove",function menuListOver(e){if(mainNavHit(e.pageX,e.pageY,"userMenuList",true)){$("#userMenuList").hide();$(document).unbind("mousemove");}});
		});

		$("#qaMenu").bind("mouseover",function(){
			hideMenu();
			$("#qaMenuList").show();
			$(document).bind("click",function menuListClick(e){if(mainNavHit(e.pageX,e.pageY,"qaMenuList",false)){$("#qaMenuList").hide();$(document).unbind("click",menuListClick)}}); 
			$(document).bind("mousemove",function menuListOver(e){if(mainNavHit(e.pageX,e.pageY,"qaMenuList",false)){$("#qaMenuList").hide();$(document).unbind("mousemove");}});
		});

		$("#topicsMenu").bind("mouseover",function(){
			hideMenu();
			$("#topicsMenuList").show();
			$(document).bind("click",function menuListClick(e){if(mainNavHit(e.pageX,e.pageY,"topicsMenuList",false)){$("#topicsMenuList").hide();$(document).unbind("click")}}); 
			$(document).bind("mousemove",function menuListOver(e){if(mainNavHit(e.pageX,e.pageY,"topicsMenuList",false)){$("#topicsMenuList").hide();$(document).unbind("mousemove");}});
		});

		$("#docMenu").bind("mouseover",function(){
			hideMenu();
			$("#docMenuList").show();
			$(document).bind("click",function(e){if(mainNavHit(e.pageX,e.pageY,"docMenuList",false)){$("#docMenuList").hide();$(document).unbind("click")}});
			$(document).bind("mousemove",function(e){if(mainNavHit(e.pageX,e.pageY,"docMenuList",false)){$("#docMenuList").hide();$(document).unbind("mousemove");}});
		});

		$(".nav_username").bind("focus",function(){
			if($(this).val() == "请输入帐号"){
				$(this).val("");
			}
		});
		$(".nav_password").bind("focus",function(){
			if($(this).val() == "1234"){
				$(this).val("");
			}
		});
		$(".nav_username").bind("blur",function(){
			if($(this).val().length < 1){
				$(this).val("请输入帐号");
			}
		});
		$(".nav_password").bind("blur",function(){
			if($(this).val().length < 1){
				$(this).val("1234");
			}
		});
		$(".nav_login_btn").bind("click",function(){
			new_login();
		});
		$(".nav_password").bind("keyup",function(event){
			if(event.keyCode == 13){
				new_login();
			}
		});
		$(".nav_register_btn").bind("click",function(){
			var url = encodeURI(location.href);
			window.location = "/signup.html?beforeLoginUrl="+url;
			if(window.event){window.event.returnValue = false;};
		});
	};
	
	//激活邮箱提示
	var avtive=function(){
		var cookieValue = getCookie("tip"+loginUserId);
		if(cookieValue != null && cookieValue == "yes"){
			$("#logo_tip").hide();
		}else{
			$("#logo_tip").show();
		}
		$("#logo_tip_cl_a").click(function(){
			setCookie("tip"+loginUserId,"yes",1,"/");
			$("#logo_tip").hide();
		});
		$("#mailTo").click(function(){
			var userMail = ""+loginUserEmail;
			var mail = userMail.substring(userMail.indexOf('@') + 1);
			window.open("http://mail."+mail);
		});
	};
	//购物车
	var card=function(){
		var qCb = $("#cartBox");
		var qNo = $("#cartCount");
		var qLc = $("#logoCart");
		var flag = true;//标记是否可以关闭
		
		var fShoCart = function(){//购物车浮动层显示
			flag = false;
		if(!qCb.html()){
			qCb.load("/cart/my_cart_div.html?sessionId="+Math.random(),function(){qLc.show();qCb.show();}); 
		};
		};
		var fCloCart = function(){//购物车浮动层关闭
			flag = true;
		setTimeout(function(){
			if(flag){
				qCb.html('');
				qLc.hide();
			};
		},300);
		};
		//购物车鼠标移上|移出效果
		qNo.hover(fShoCart,fCloCart);
		qCb.hover(fShoCart,fCloCart);
	};
	
	//"新消息"对象
	var mes = function() {
	    var t = 15; // 刷新消息时间间隔（单位秒）
	    var c = 0; // 大于几个新消息触发
	    var fi = 0; // 闪烁函数ID
	    var ft = 1000; // 闪烁频率（单位毫秒）
	    var fc = 20; // 闪烁次数（多少次后停止）
	    var ot = document.title; // 当前网页的标题
	    var lc = 0; // 上一次得到的新消息数
	    var flag = true; // 是否第一次初始化
	    var qMes = $("#newMessageFloating");
	    var qInf = $("#nav_new_message_li");
	
	    // 显示新消息浮动层
	    var showMes = function() {
	        // if (qMes.find("li").html()) {
	            qMes.show();
	            setCookie("newMessageFloating", 1, 365, "/");
	        // };
	    };
	    // 隐藏新消息浮动层
	    var closeMes = function() {
	        qMes.hide();
	        setCookie("newMessageFloating", 0, 365, "/");
	    };
	    // 闪烁页面标题
	    var fliTit = function() {
	        var fCount = 0;
	        fi = setInterval(function() {
	            if (fCount % 2 == 0 || fCount >= fc) {
	                document.title = "【新消息】" + ot;
	            } else {
	                document.title = "【　　　】" + ot;
	            };
	            fCount += 1;
	        },ft);
	    };
	    // 修改消息数量的数字
	    var newMes = function(n) {
	        var htmlObj = $("#itemMsg");
	        if (n > c) {
	            var str = "新消息<em>(" + n + ")</em>";
	            htmlObj.html(str);
	            if (fi == 0 && !flag) {
	                fliTit();
	            }
	        } else {
	            if (fi != 0) {
	                clearInterval(fi);
	                document.title = ot;
	            };
	            if (htmlObj.html()) {
	                htmlObj.remove();
	            };
	            qMes.hide();
	        }
	    };
	    // 初始化新消息
	    var mesFlo = function(id, count, o) {
	        if (count > 0) {
	            var ul = qInf;//qMes.find("ul");
	            ul.empty();
	            if (o.expNoteComment > 0) {
	                ul.append('<p><a href="/people/' + id + '/expnotes/?orderby=4">手记有新评论<em>(' + o.expNoteComment + ')</em></a></p>');
	            };
	            if (o.privateMessage > 0) {
	                ul.append('<p><a href="/mine/info/private_letter/">新私信<em>(' + o.privateMessage + ')</em></a></p>');
	            };
	            if (o.message > 0) {
	                ul.append('<p><a href="/mine/info/board/">新留言<em>(' + o.message + ')</em></a></p>');
	            };
	            if (o.beFollowers > 0) {
	                ul.append('<p><a href="/mine/info/be_followers/">新粉丝<em>(' + o.beFollowers + ')</em></a></p>');
	            };
	            if (o.system > 0) {
	            	ul.append('<p><a href="/mine/info/system/">新系统消息<em>(' + o.system + ')</em></a></p>');
	            };
	//            if (o.saleProductCount > 0) {
	//                ul.append('<p><a href="/mine/exp/transaction/sold/">新资料售出<em>(' + o.saleProductCount + ')</em></a></p>');
	//            };
	            if (o.productCommentCount > 0) {
	                ul.append('<p><a href="/mine/exp/comment_mine/">新资料被评价<em>(' + o.productCommentCount + ')</em></a></p>');
	            };
	//            if (o.payWaitCount > 0) {
	//                ul.append('<p><a href="/mine/order/mine_order_waitpay/">待付款<em>(' + o.payWaitCount + ')</em></a></p>');
	//            };
	//            if (o.commentCount > 0) {
	//                ul.append('<p><a href="/mine/order/mine_order_waitrating/">待评价<em>(' + o.commentCount + ')</em></a></p>');
	//            };
	            if (flag)qMes.find(".message_cl").bind("click", closeMes);
	            $("#itemMsg").click(showMes);
	            var nmf = qMes;
	            if (getCookie("newMessageFloating") == null || getCookie("newMessageFloating") == 1) {
	                nmf.show();
	            } else {
	                nmf.hide();
	                if (!flag && count > lc) {
	                    showMes();
	                }
	            };
	            flag = false;
	            lc = count;
	        }
	    };
	    // 取得用户最新消息数
	    var mesCount = function() {
	        if (!_isSubmit) {
	            _isSubmit = true;
	            $.getJSON("/ajax/mine/newMessageCount.html",function(o) {
	                _isSubmit = false;
	                if (o.map.isok) {
	                	var ac = o.map.allCount ;
	                    newMes(ac);
	                    mesFlo(o.map.userId, ac, o.map.result);
	                }
	            });
	        };
	    };
	    return (function() { //新消息初始化
	        mesCount();
	        setInterval(mesCount, t * 1000);
	    });
	};

	init();
	avtive();
	if (userIsLogin) {
		mes()();
		card();
	};
})();
