﻿var g_blinktitle = document.title;
var g_blinkswitch = 0;
var g_inputtime = 0;
var g_pre_newmsgstatus = "";
var g_blinkid = 0;
var g_newmsg_sound = null;

function blinkNewMsg()
{
	var now    = new Date();
	var nowtime = now.getTime();
	if(nowtime - g_inputtime > 5000)
	{
		document.title = g_blinkswitch % 2 ? "【　　　】 - " + g_blinktitle : "【新消息】 - " + g_blinktitle;
	}
	
	g_blinkswitch++;
}

var s = 0;
function checkNewMsgStatus()
{
	var url = "/Ajax/system_notice.aspx?ac=getstatus";
	var pars = "";
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { checkNewMsgShow(req); } });
}

function getNewMsg()
{
    var url = "/Ajax/system_notice.aspx?ac=gethtml";
	var pars = "pre_status=" + g_pre_newmsgstatus;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { getNewMsgShow(req); } });
}

function executeAjaxLog(log)
{
    var url = "/Ajax/system_notice.aspx?ac=log";
	var pars = "log=" + log;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) {  } });
}


function checkNewMsgShow(req)
{
	//获取返回值
	var r = req.responseText;
	if(r=="nologin")
	    return;

	setTimeout(checkNewMsgStatus, 60000);   //控制间隔

    //executeAjaxLog(r);
	//首次运行,只记录状态,不显示闪动和声音效果
	if(g_pre_newmsgstatus==""||g_pre_newmsgstatus=="0,0,0,0,0,0")
	{
	    g_pre_newmsgstatus = r;
	    getNewMsg();
	}
	else
	{
	    //比较两次的结果,如果不同,显示闪动\声音效果\更新html
	    var result;
	    var status;
	    
	    result=r.split(",");
	    status=g_pre_newmsgstatus.split(",");


	    if(result[0]>status[0]||result[1]>status[1]||result[2]>status[2]||result[3]>status[3]||result[4]>status[4]||result[5]>status[5])
	    {
            //显示闪动
            clearInterval(g_blinkid);
            g_blinkid = setInterval(blinkNewMsg, 1000);
            
            //播放声音
        	if (g_newmsg_sound == null)
			{
				g_newmsg_sound = new SWFObject("/flash/newmsg_sound.1.0.swf", "newmsg_sound_swf", "1", "1", "8", "#ffffff", true);
				g_newmsg_sound.addParam("allowscriptaccess", "always");
				g_newmsg_sound.addParam("wmode", "opaque");
				g_newmsg_sound.addParam("menu", "false");
				g_newmsg_sound.addVariable("autoplay","0");
			}
			g_newmsg_sound.write("head_msgsound_div");
			
            getNewMsg();
            g_pre_newmsgstatus = r;
        }        
	}
}

//将返回的最新通知html更新到页面
function getNewMsgShow(req)
{
    var r = req.responseText;
    $('notice_list').innerHTML = r;
}
