//written by wangjiujun
//检查新消息

var g_blinkid = 0;
var g_blinkswitch = 0;
var g_blinktitle = document.title;
var g_has_new_pm = 0;
var g_has_new_fr = 0;
var g_has_new_notify = 0;

function blinkNewMsg() {
	/*
	if(document.title == "【　　　】 - " + g_blinktitle) {
		document.title = "【新消息】 - " + g_blinktitle;
	} else {
		document.title = "【　　　】 - " + g_blinktitle;
	}
	*/
	document.title = g_blinkswitch % 2 ? "【　　　】 - " + g_blinktitle : "【新消息】 - " + g_blinktitle;
	g_blinkswitch++;
}

function stopBlinkNewMsg() {
	if (g_blinkid) {
		clearInterval(g_blinkid);
		g_blinkid = 0;
		/*
		$("havepm").innerHTML = "";
		$("havefriend").innerHTML = "";
		$("havenotice").innerHTML = "";
		*/
		document.title = g_blinktitle;
		g_has_new_pm = 0;
		g_has_new_fr = 0;
		g_has_new_notify = 0;
	}
}

function clear_blink_new_msg() {
	if (g_blinkid) {
		if(!g_has_new_pm && !g_has_new_fr && !g_has_new_notify) {
			clearInterval(g_blinkid);
			g_blinkid = 0;
			document.title = g_blinktitle;
		}
	}
}

function checkNewMsg(){
	if(g_uid != '') {
		//var postdata="touid=" + g_uid;
		postdata = "";
		startRequest('POST','index.php?mod=mail&act=chk_new_msg&timestamp=' + new Date().getTime(),postdata,exc_themedata);
	}
}

function exc_themedata(v){
	setTimeout(checkNewMsg, 60000);

   if(v){
	   var v_a=v.split('|');
   }else{
	   clear_blink_new_msg();
       return;
   }
   //alert(v)
   if(v_a[0] !=0 && !isNaN(v_a[0])){
	   msg_show_new_pm();
   } else {
		msg_hidden_new_pm();
   }
   if(v_a[1] !=0 && !isNaN(v_a[1])){
	   msg_show_new_fr();
   } else {
		msg_hidden_new_fr();
   }
   if(v_a[2] !=0 && !isNaN(v_a[2])){
	   msg_show_new_notify(v_a[2]);
   } else {
		msg_hidden_new_notify();
   }

   if(g_has_new_pm || g_has_new_fr || g_has_new_notify) {
		//有新消息
		if(g_blinkid == 0) {
			g_blinkid = setInterval(blinkNewMsg, 1000);
		}
   } else {
		//没有新消息
		stopBlinkNewMsg();
		//clearInterval(g_blinkid);
		//g_blinkid = 0;
		//document.title = g_blinktitle;
   }
}

function msg_show_new_pm() {
	g_has_new_pm = 1;
	$('havepm').innerHTML='&nbsp;<a href="index.php?mod=mail"><img  align="absmiddle" src=images/common/email.gif title="有新消息">&nbsp;</a>';
}

function msg_show_new_fr() {
	g_has_new_fr = 1;
	$('havefriend').innerHTML='&nbsp;<a style="cursor:pointer"  onclick="ShowWinList(\'index.php?mod=friends&act=detealfriend\',310,500,\'处理加为好友请求!\');"><img  align="absmiddle" src=images/common/friends.gif title="有交友请求">&nbsp;</a>';
}

function msg_show_new_notify(v) {
	g_has_new_notify = 1;

	if(v == "2") {
		$('havenotice').innerHTML='&nbsp;<a href="index.php?mod=mail&act=ct"><img  align="absmiddle" src=images/common/novify.gif title="有新的评论">&nbsp;</a>';
	} else if(v == "3") {
		$('havenotice').innerHTML='&nbsp;<a href="index.php?mod=mail&act=ct&t=1"><img  align="absmiddle" src=images/common/novify.gif title="有新的评论回复">&nbsp;</a>';
	} else {
		$('havenotice').innerHTML='&nbsp;<a href="index.php?mod=mail&act=notify"><img  align="absmiddle" src=images/common/novify.gif title="有新通知">&nbsp;</a>';
	}
}

function msg_hidden_new_pm() {
	g_has_new_pm = 0;
	$('havepm').innerHTML='';
}

function msg_hidden_new_fr() {
	g_has_new_fr = 0;
	$('havefriend').innerHTML='';
}

function msg_hidden_new_notify() {
	g_has_new_notify = 0;
	$('havenotice').innerHTML='';
}

/*
function exc_new_system_notify(r) {
	if(r == "1") {
		g_has_new_msg=1;
		//有新的系统通知
       $('havenotice').innerHTML='&nbsp;<a href="index.php?mod=mail&act=system"><img  align="absmiddle" src=images/common/novify.gif title="有新通知">&nbsp;</a>';
	}
}
*/