


function openLoginScreen(refreshAll){//打开登录窗口
	var host = window.location.host;
	refreshAll=refreshAll||"";
	var loginScreen = window.open(
			'http://sso.onezone.com/login.action?referer=http://'+host+'/user/loginSuccess.jsp?refreshAll='+refreshAll+'&time=' + new Date(),
			'loginScreen',
			'scrollbars=no,resizable=no,width=450,height=280,left=550,top=200,screenX=500,screenY=200');
	loginScreen.focus();
	return false;
}
//登录成功回调函数
function loginCallback(){
	var refreshAll = 'null';
	if(refreshAll == 'true'){
		window.location.reload();
	}else{
		try{
			if(window.loadUserInfo){
				loadUserInfo();
			}
		}catch(E){}
	}
}
 function getAjaxRequest(){
 	var ajaxRequest; 
 	if(window.XMLHttpRequest){
		ajaxRequest = new XMLHttpRequest();
		
	}else if(window.ActiveXObject){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		}
		if(!ajaxRequest){
			window.alert("你的浏览器不支持XMLHttpRequest!");
		}
	//标明这个AJAX请求
	
	
	return ajaxRequest;

 }
function setCheckLoginFlag()
 {
 	setCookie("checkTimes", 1, null, "/", null, null);
 }
 
 function clearCheckLoginFlag()
 {
 	deleteCookie("checkTimes",null,null);
 }
 
 function isNeedCasCheckLogin()
 {
 	var checkTimes=getCookie("checkTimes");
    if(checkTimes==null)
    	return true;
        
    //有去cas验证过，但是可能在别的域登录了，所以还是要验证
    var uid=getCookie("userId");
	if(uid!=null)
    	return true;
 }

 function loadUserInfo(callback){
 	var ajaxRequest = getAjaxRequest();
 
	ajaxRequest.onreadystatechange=function (){
		if(ajaxRequest.readyState==4){		
			   var isUserLogon=false;
			　　if(ajaxRequest.status==200){
					var headUserInfo =ajaxRequest.responseText;
					isUserLogon=headUserInfo.indexOf('退出')>0;
					
					if(isUserLogon){ //看是否已经在本服务器登录,如果为true,则为已经登录!
                    	//清除cas登录检查标志
                    	clearCheckLoginFlag();
                        
                        //跨域跳转回调，add by chenyg,1029
                        if(callback)
                        {
                        	callback();
                            return;
                        }
                        if(document.getElementById('site_head_top_l') != null){
                    　　　	document.getElementById('site_head_top_l').innerHTML= headUserInfo;
                            if( ajaxRequest.responseText.indexOf('退出') > 0)
                            {
                                if(document.getElementById('leftLogin') != undefined)
                                {
                                    document.getElementById('leftLogin').style.display="none";
                                }
                                
                                executeNotify();
                                
                            }
                        }
                        					
			　	　}else{
            		
                   	//跨域跳转回调，add by chenyg,1029
                       if(callback)
                       {
                           callback();
                           return;
                       }
                       
                   	document.getElementById('site_head_top_l').innerHTML= headUserInfo;
                   
				}
                
            　	if(window.refreshUserState)
                {	               
                    refreshUserState(isUserLogon);
                }	
			}
　		}
	};
	ajaxRequest.open(
 			 "POST", 
		 	 "/head/userStateInfo.jsp?loginType=", 
		 	 true
		 	);
	ajaxRequest.send("refreshAll=null");
 } 
 
//刷新未读信息条数提醒DIV
var newSmsCount = 0;
var titleIntervalId = null;
var OZP_notify = null;
var imReqUserId = null;
function executeNotify() {
	try{
		getNotifyData();
		//好友数
		refreshFriendData();
	} catch(e) {
		window.setTimeout(executeNotify,2000);
	}
}
function refreshNotifyData(userId,response) {
	
	var smsUrl = "http://my.onezone.com/mycenter/sms.action?type=0";
	
	if (response.notify != null) {
		
		var lastUnreadNumber = newSmsCount;
		
		OZP_notify = response.notify;
		newSmsCount = response.notify.unreadNumber;
		imReqUserId = response.notify.imReqUserId;	
		
		var unreadNumber = getCookie("unreadNumber");
		if(unreadNumber != null && unreadNumber != '') {
			var str = unreadNumber.split(":");
			if(str[0] == userId) {
				lastUnreadNumber = parseInt(str[1]);
			}
		}
		setCookie("unreadNumber", userId+":"+newSmsCount, getExpDateWithSecond(0,0,0,10), "/", "onezone.com", false);
			
		// 站内信
		var sms_div = document.getElementById("sms");
		if(sms_div && sms_div != null && typeof(sms_div) != 'undefined') {
			sms_div.innerHTML = "<a class='message' href='"+smsUrl+"'>(<span>"
					+ newSmsCount + "</span>)</a>";
		}
		
		// webim
		if(imReqUserId != '') {
			try{
				openWebim(imReqUserId, true);
			}catch(e) {
				//alert(imReqUserId+e);
			}
		}
				
		if(imReqUserId != '') {
			if(titleIntervalId == null) {
				titleIntervalId = window.setInterval(animatetitle,500);
			}
			window.setTimeout("newSmsNotify('')", 1000);
		}else if(newSmsCount == 0) {
			if(titleIntervalId != null) {
				window.clearInterval(titleIntervalId);
				window.document.title = current_title;
				titleIntervalId = null;
			}
		}else if (lastUnreadNumber >= newSmsCount) {
			if(titleIntervalId == null) {
				titleIntervalId = window.setInterval(animatetitle,500);
			}
		}else{
			if(titleIntervalId == null) {
				titleIntervalId = window.setInterval(animatetitle,500);
			}
			window.setTimeout("newSmsNotify('')", 1000);
		}
		
	}

}

function getExpDateWithSecond(days, hours, minutes, seconds)
{
    var expDate = new Date();
    if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number" && typeof(seconds) == "number")
    {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
		expDate.setSeconds(expDate.getSeconds() + parseInt(seconds));
        return expDate.toGMTString();
    }
}

function refreshFriendData() {
	var friend_div = document.getElementById("friend_count");
	if(friend_div && friend_div != null && typeof(friend_div) != 'undefined') {
		jQuery.get("/webim/onlineFriendCount.action?time=" + new Date(), null, function(response){
			response = jQuery.trim(response);
			var resp = response.split(",");
			var onlineFriendCount = resp[0];
			var friendCount = resp[1];
			friend_div.innerHTML = onlineFriendCount + "/" + friendCount;
			window.setTimeout(refreshFriendData,180000);
		});
	}
}

function doLogout(url){
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement("script");
	var host = window.location.host;
	script.src = 'http://sso.onezone.com/logout.action?host='+ host +'&currentTime='+new Date().getTime().toString();
	script.onload = script.onreadystatechange = function(){
		if (!this.readyState || 
				this.readyState == "loaded" || this.readyState == "complete")  {
			try{
				if(window.logoutCallback){
					logoutCallback();
				}else{
					if(url){
						if(window.location.href==url || window.location.href==(url+"/")|| (window.location.href+"/")==url){
							window.location.reload();
						}else{
							window.location.href=url;
						}
					}else{
						window.location.reload();
					}					
				}
			}catch(E){}
			head.removeChild( script );
		}
	};
	head.appendChild(script);
	return false;
}
