﻿// JScript 文件
if(!Array.prototype.push)
{
    Array.prototype.push=function()
    {
        var startLength=this.length;
        for(var i=0;i<arguments.length;i++)
            this[startLength+i]=arguments[i];
        return this.length
    }
};

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}
var i = 0;

function QueueMessage()
{
    this.CookieName = "QueueMessages" ;
    this.CookieDomain = null;
    this.RandomCode = "QueueMessages" + Math.random();
    
    if(arguments.length>0)
        this.CookeName += arguments[0];
    
    this.HoldingInterval = 1000;
    this.timer = 0;
   
	this.Messages = new Array();
	this.MSG = new CLASS_MSN_MESSAGE(null,null,null,null,null,null,null,null);
	this.Initialize();
}

//初始化
QueueMessage.prototype.Initialize = function ()
{
    var me = this;
    addEvent(window,"focus",function(){me.WinFocus();});
    addEvent(window,"unload",function(){if(me.MSG&&me.MSG.Pop){me.MSG.Pop.hide();}});
    me.WinFocus(); 
}

QueueMessage.prototype.Holding = function()
{
    var me = this;
    if(me.timer > 0)
        window.clearTimeout(me.timer);

    if(me.IsActive())
    {
        if(me.MSG != null && !me.MSG.Showing())
        {

            me.ReadAll();
            //alert(me.Messages.length);
            if(me.Messages.length > 0 )
            {
                me.Show(me.Messages[0]);
                return;
            }
        }
        me.timer = window.setTimeout(function(){me.Holding();},me.HoldingInterval );
    }
}

QueueMessage.prototype.IsActive = function()
{
        var me = this;
        act = getCookie(me.CookieName+"RandomCode") == me.RandomCode;
        return act;
};

QueueMessage.prototype.ReadAll = function()
{
    var me = this;
    me.Messages = new Array();
    var  serialMessage = getCookie(me.CookieName);

	if(serialMessage)
	{
	    var messageList = serialMessage.split(">");
	    for(var i = 0;i<messageList.length;i++)
	    {

	        var info = unescape(messageList[i]).split(">");
	        if(info.length == 3)
	        {
	            if(info[0] ==0)
	            {
	        	    var msg = new Message();
                    msg.Type = info[0];
                    msg.Uid = info[1];
                    msg.UName = unescape(info[2]);
                    me.Messages[i] = msg;
                }
//                else
//                {
//                    alert("不是聊天消息。");
//                }
	        }
//	        else
//	        {
//	            alert("消息格式错误。");
//	        }
	    }
	}
}

//添加消息
QueueMessage.prototype.Push = function(_Message)
{
    var me = this;
    me.ReadAll();
    me.Messages.push(_Message);
    me.Save();
}

//保存消息到Cookies
QueueMessage.prototype.Save = function()
{
    var me = this;
    messageList = new Array();

    for(var i = 0;i<me.Messages.length;i++)
    {
        var sMsg = me.Messages[i].Type +">" + me.Messages[i].Uid +">" + escape(me.Messages[i].UName);
        messageList[i] = escape(sMsg);
    }
     var  serialMessage = messageList.join(">");
     setCookie(me.CookieName,serialMessage,null,"/",me.CookieDomain);
    
}
QueueMessage.prototype.Show = function (msg)
{
    if(uid < 1 ) return;
    var me = this;
    if(msg.Type == 0)
    {
    
        me.MSG = new CLASS_MSN_MESSAGE("aa",280,140,"您有新消息 - 同窗","<span style=\"FONT-SIZE:12px;COLOR:#336666;line-height:22px;\">[" + msg.UName +"]正请求与您对话。</span>","<span style=\"FONT-SIZE:12px;COLOR:#336666;line-height:22px;\"><a href='javascript:void(0)' id='OpenDialog'>点此打开对话窗口</a><br><a href='javascript:void(0);' id='HideDialog' hidefocus=false>不再提示此人消息</a></span>");  

    }
    CLASS_MSN_MESSAGE.prototype.onunload = function() { window.setTimeout(function(){me.Holding();},1000 ); return true; };
    CLASS_MSN_MESSAGE.prototype.onload = function() {
                                                        var openDialog = me.MSG.Pop.document.getElementById("OpenDialog");
                                                        var dialogID = uid+"_"+msg.Uid;
                                                        var dialogID2= msg.Uid+"_"+uid;

                                                        if(getCookie(dialogID2) == "opened")
                                                        {
                                                            dialogID = dialogID2;
                                                        }
                                                        openDialog.onclick = function(){window.open(RelativeAddress + "/Chat/?Incept=true&InceptID="+msg.Uid,dialogID,'height=450, width=460, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');me.MSG.hide();return true;};
                                                        var hideDialog = me.MSG.Pop.document.getElementById("HideDialog");
                                                        hideDialog.onclick = function(){ hiddenDialog(msg.Uid);me.MSG.hide();};
                                                    };
	me.MSG.rect(null, null, null, screen.height - 50);
    me.MSG.speed = 20;
    me.MSG.step = 5;
    me.MSG.autoHide = false;
    me.MSG.show(); 
    me.Messages.shift();
    me.Save();

}

QueueMessage.prototype.WinFocus = function(){
    var me = this;
    setCookie(me.CookieName+"RandomCode",me.RandomCode,null,"/",me.CookieDomain);
    me.Holding();
}
QueueMessage.prototype.SetCookieName = function(_CookieName){
    me.CookieName = "QueueMessages" + _CookieName;
    setCookie(me.CookieName+"RandomCode",me.RandomCode,null,"/",me.CookieDomain);

}

function Message()
{
    this.Type;
    this.Uid;
    this.UName;
}

