// ajax load sso uc_passport 
$.ajax({
     url: basePath +'/member?excuteLoregAjax=true&time=' + (new Date()).getTime(),  //后台处理程序
     async:false,
     type:'post',    //数据发送方式
     dataType:'json',  //接受数据格式
     success:function(json){
     	var state = json.value;
     	var states = state.split(",");
		if(states[0] == "noLogin"){
			$("#userNoLogin").show();
			$("#alreadyLogin").val("1"); //没有登陆
			$(".user-top").hide();
		}else if(states[0] == "alreadyLogin"){
			$("#userLogin").show();
			$("#alreadyLogin").val("0"); //已登陆
	  		$("#user_name").html(states[1]);
		}
     }
});

// ajax load header function
$(document).ready(function (){
      
	$.ajax({
		   url: shopPath+"/getSessionGoodsNum.action",// 购物车数据统计
		   type:"get",
		   success:function(msg){
				$("#goodsNum1").html(msg); 
				$("#goodsNum2").html(msg);
		   }
	});
		
	$('#headSms').bind('mouseenter', function() {
  		$('#headSmsNum').fadeIn(300);
  		$(this).attr("class","headhover");
		$(this).bind("mouseleave",function(){
			$(this).attr("class","atz");
			$('#headSmsNum').hide();
		});
	});
	
	if($("#alreadyLogin").val() == "0"){
		$.getJSON(snsPath+"/member/smsCenterCount.action?jsoncallback=?",{ajaxFlag:0},function(data){
			if(data.sysTotal > 0 ){
				var smsNumURL = snsPath + "/member/sms/my_sms_receive.jsp?isRead=false";
				var friendApplyNumURL = snsPath + "/member/sms/my_sms_addfriend.jsp";
				var giftGiveNumURL = snsPath + "/gift/member/my_gift_receive.jsp";
				var giftAskNumURL = snsPath + "/member/giftAskedMine.action?giftAskModel.orderField=noRe";
				var otherNumURL = snsPath + "/member/sms/my_sms_system.jsp";
				if(data.smsNum > 0){
					$("#smsNum").html("<a href="+smsNumURL+">"+data.smsNum+"</a>");
				}
				if(data.friendApplyNum > 0){
					$("#friendApplyNum").html("<a href="+friendApplyNumURL+">"+data.friendApplyNum+"</a>");
				}
				if(data.giftGiveNum > 0){
					$("#giftGiveNum").html("<a href="+giftGiveNumURL+">"+data.giftGiveNum+"</a>");
				}
				if(data.giftAskNum > 0){
					$("#giftAskNum").html("<a href="+giftAskNumURL+">"+data.giftAskNum+"</a>");
				}
				if(data.otherNum !=0){
					$("#otherNum").html("<a href="+otherNumURL+">"+data.otherNum+"</a>");
				}
				$("#headSmsImage").attr("src",snsPath + "/image/mail2.gif");
				$("#headSmsImageNum").text(data.sysTotal);
			}
		});
	}
});

// 登陆
function login(){	 
    location.href=shopPath+"/login.jsp?returnURL="+escape(location.href);
    return false;
}

// 英文登陆
function loginEN(){	 
    location.href=shopPath+"/login_en.jsp?returnURL="+escape(location.href);
    return false;
}

// 注册
function register(){
    location.href=shopPath+"/register.jsp?returnURL="+escape(location.href);
    return false;
}

// 英文注册
function registerEN(){
    location.href=shopPath+"/register_en.jsp?returnURL="+escape(location.href);
    return false;
}

// Header搜索
function subHeaderForm(thisForm) {
	// 关键字
	var keyword_text = thisForm.keyword_text.value;
	thisForm.keyword.value=keyword_text;
	thisForm.username.value=keyword_text;
	// 搜索类型 
	if(thisForm.r_type[0].checked){
		searchMember(thisForm);
	}
	if(thisForm.r_type[1].checked){
		searchGoods(thisForm);
	}
}

// 搜索会员
function searchMember(thisForm){
	if('查找朋友'==thisForm.inputInHead.value){
		thisForm.inputInHead.value = '';
	}
	thisForm.action=snsPath+"/member/jumpSearch.action?inputInHead="+$("#inputInHead").attr("value");
	thisForm.submit();		
}

// 搜索商品
function searchGoods(thisForm){
	if('搜索商品'==thisForm.keyword.value){
		thisForm.keyword.value = '';
	}
	if(thisForm.goodsTypeId.value=='' && thisForm.keyword.value==''){
		alert('请输入关键字或选择商品类型');
		thisForm.keyword.focus();
		return false;
	} else if (thisForm.keyword.value.length > 30) {
		alert('关键字太长');
		thisForm.keyword.focus();
		return false;
	}
	thisForm.action=shopPath+"/search.action";
	thisForm.submit();
}


// 搜索文章
function searchNews(thisForm){
	thisForm.action=newsPath+"/?s="+thisForm.s.value+"&searchsubmit=Find";
	thisForm.submit();
}

// 搜索关键字
function searchKeyword(thisForm,thisValue){
	thisForm.keyword.value=thisValue;
	thisForm.action=shopPath+"/search.action";
	thisForm.submit();
}

// 回车键搜索
function gotoSearch(e) {
	if ( !e) {
		e = window.event;
	}
	if (e.keyCode != 13) {
		return;
	}
	searchGoods(document.HeaderForm);
}

// 设置功能隐藏和显示
function toggleSettings() {
	$("#snsSettings").toggle();
}
