﻿/*
     ==================================================================================================  
    类名：MSN_MESSAGE  
    功能：提供类似MSN消息框  (支持多实例，随滚动条移动)
    示例：  
     ---------------------------------------------------------------------------------------------------  
  
             var MSG = new MSN_MESSAGE("aa",200,120,"短消息提示：","您有1封消息","今天请我吃饭哈");  
                 MSG.show();  
                 
             var MSG1 = new MSN_MESSAGE("aa",200,120,"短消息提示：","您有1封消息","今天请我吃饭哈","_blank","http://www.baidu.com");  
                 MSG1.rect(null,null,null,screen.height-50); 
                 MSG1.speed     = 10; 
                 MSG1.step     = 5; 
                 MSG1.show();  
    ==================================================================================================  

*/
  
  
//消息构造  
 //id,width,height,caption,title,message,target,action
function MSN_MESSAGE(option){  
     this.id      = option.id;  
     this.title   = option.title;  
     this.caption= option.caption;  
     this.message= option.message;  
     this.target = option.target||"_blank";  
     this.href = option.href||"javascript:void()";  
     this.width     = option.width||200;  
     this.height = option.height||120;  
     this.timeout=option.timeout||150;  
     this.speed     =option.speed|| 10; 
     this.step     =option.setp|| 1; 
     this.right=document.documentElement.clientWidth+document.documentElement.scrollLeft;
     this.bottom = document.documentElement.clientHeight+document.documentElement.scrollTop-(this.height*this.index);
     this.left   = this.right - this.width; 
     this.top    = this.bottom - this.height; 
     this.timer  = 0; 
     this.pause  =option.pause?true: false;
     this.close  =option.close?true: false;
     this.autoHide  =option.autoHide?true:false;
     this.onclose=option.onclose||function(){};
     this.onclick=option.onclick||function(){};
     this.closed=false;
     this.showed=false;
     this.index=0
     this.Alpha=0;
}

  
  
//隐藏消息方法  
MSN_MESSAGE.prototype.hide = function(){  
     if(this.onunload()){  

         var me   = this;  
         if(this.timer>0)
         {   
             window.clearInterval(me.timer);  
         }  

         var fun = function(){  
             if(me.pause==false||me.close)
             {
                 me.Alpha-=5;
                 me.Pop.style.filter="Alpha(Opacity="+ me.Alpha+", FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=140);"
 
                if(me.Alpha<=0)
                 {
                     window.clearInterval(me.timer);
                     me.Pop.hide();
                 }
             }             
         }  

         this.timer = window.setInterval(fun,this.speed)      
     }  
}
 
  
//  消息卸载事件，可以重写  

MSN_MESSAGE.prototype.onunload = function() {
    this.onclose();
     return true;  
}  

//消息命令事件，要实现自己的连接，请重写它  
 
MSN_MESSAGE.prototype.oncommand = function(){  
    this.onclick();
     this.hide();  
}
  
MSN_MESSAGE.addEventListener=function(obj,handler,fn)
{
    if(obj.attachEvent)
    {
      obj.attachEvent("on"+handler,fn);
    }
    else if(obj.addEventListener)
    {
    obj.addEventListener(handler,fn,false)
    }
}

MSN_MESSAGE.removeEventListener=function(obj,handler,fn)
{
    if(obj.detachEvent)
    {
      obj.detachEvent("on"+handler,fn);
    }
    else if(obj.removeEventListener)
    {
    obj.removeEventListener(handler,fn,false)
    }

}



MSN_MESSAGE.prototype.pageWidth=document.documentElement.scrollLeft+document.documentElement.clientWidth;
MSN_MESSAGE.prototype.pageHeight=document.documentElement.scrollTop+document.documentElement.clientHeight;
// 消息显示方法  
 
MSN_MESSAGE.prototype.show = function(){  

  
     var w = this.width;  
     var h = this.height;  
  
     var str = "<DIV id='"+this.id+"' style='display:none;position:absolute;BORDER-RIGHT: #455690 1px solid; BORDER-TOP: #a6b4cf 1px solid; Z-INDEX: 99999; LEFT: 0px; BORDER-LEFT: #a6b4cf 1px solid; WIDTH: " + w + "px; BORDER-BOTTOM: #455690 1px solid; POSITION: absolute; TOP: 0px; HEIGHT: " + h + "px; BACKGROUND-COLOR: #c9d3f3'>"  
         str += "<TABLE style='BORDER-TOP: #ffffff 1px solid; BORDER-LEFT: #ffffff 1px solid' cellSpacing=0 cellPadding=0 width='100%' bgColor=#cfdef4 border=0>"  
         str += "<TR>"  
         str += "<TD style='FONT-SIZE: 12px;COLOR: #0f2c8c' width=30 height=24></TD>"  
         str += "<TD style='PADDING-LEFT: 4px; FONT-WEIGHT: normal; FONT-SIZE: 12px; COLOR: #1f336b; PADDING-TOP: 4px' vAlign=center width='100%'>" + this.caption + "</TD>"  
         str += "<TD style='PADDING-RIGHT: 2px; PADDING-TOP: 2px' vAlign=center align=right width=19>"  
         str += "<SPAN title=关闭 style='FONT-WEIGHT: bold; FONT-SIZE: 12px; CURSOR: hand; COLOR: red; MARGIN-RIGHT: 4px' id='btSysClose"+this.id+"' >×</SPAN></TD>"  
         str += "</TR>"  
         str += "<TR>"  
         str += "<TD style='PADDING-RIGHT: 1px;PADDING-BOTTOM: 1px' colSpan=3 height=" + (h-28) + ">"  
         str += "<DIV style='BORDER-RIGHT: #b9c9ef 1px solid; PADDING-RIGHT: 8px; BORDER-TOP: #728eb8 1px solid; PADDING-LEFT: 8px; FONT-SIZE: 12px; PADDING-BOTTOM: 8px; BORDER-LEFT: #728eb8 1px solid;  COLOR: #1f336b; PADDING-TOP: 8px; BORDER-BOTTOM: #b9c9ef 1px solid; HEIGHT: 90%'>" + this.title + "<BR><BR>"  
         str += "<DIV style='WORD-BREAK: break-all;height:100%'  align=left><A href='"+this.href+"' target='"+this.target+"' hidefocus=true id='btCommand"+this.id+"'><FONT color=#ff0000>" + this.message + "</FONT></A></DIV>"  
         str += "</DIV>"  
         str += "</TD>"  
         str += "</TR>"  
         str += "</TABLE>"  
         str += "</DIV>"
         
         
    
    var d=document.createElement("div");
    d.innerHTML=str;
    document.body.appendChild(d);      
    
    var oPopup = window.document.getElementById(this.id);
    this.Pop = oPopup; 
    oPopup.style.display="";

    

  
    this.offset   = 0; 
    var me   = this;  
    oPopup.moveTo=function(left,top,width,height)
    {
    try
    {
         oPopup.style.height=height+"px";
         oPopup.style.width=width+"px";
         oPopup.style.left=left+"px";
         oPopup.style.top=top+"px";
         
         me.rect(left,top.width,height);

     }
     catch(e)
     {
     
     }

    }
    oPopup.hide=function()
    {
      
      oPopup.style.display="none";
    }
     
    oPopup.scroll=function()
     {
     
     MSN_MESSAGE.prototype.pageWidth=document.documentElement.scrollLeft+document.documentElement.clientWidth;
     MSN_MESSAGE.prototype.pageHeight=document.documentElement.scrollTop+document.documentElement.clientHeight;
     var x=(me.pageWidth-oPopup.offsetWidth);
     var y=(me.pageHeight-oPopup.offsetHeight-(oPopup.offsetHeight*me.index));
     oPopup.moveTo(x,y,me.width,me.height);
     }
        
       
     var btClose = document.getElementById("btSysClose"+this.id);  
     var btCommand = document.getElementById("btCommand"+this.id); 
      
     btClose.onclick = function(){  
              me.close = true;

         me.hide();  
     }  
  
     btCommand.onclick =function(){
           me.oncommand();
     }
     
     
     
     MSN_MESSAGE.addEventListener(oPopup,"mouseover",function(){me.pause=true;});
     MSN_MESSAGE.addEventListener(oPopup,"mouseout",function(){me.pause=false;});
     MSN_MESSAGE.addEventListener(window,"scroll",function(){setTimeout(oPopup.scroll,200)});
     

     var fun = function()
     {  
             me.timeout--; 
             me.Alpha+=5;
             if(me.Alpha==100)
                   window.clearInterval(me.timer);  

          
         oPopup.style.filter="Alpha(Opacity="+ me.Alpha+", FinishOpacity=100, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=140);"
     }  
  
  this.timer = window.setInterval(fun,this.speed)
  
  oPopup.scroll();
}  

// 设置速度方法 

MSN_MESSAGE.prototype.speed = function(s){ 
     var t = 20; 
     try { 
         t = praseInt(s); 
     } catch(e){} 
     this.speed = t; 
} 

//设置步长方法 
 
MSN_MESSAGE.prototype.step = function(s){ 
     var t = 1; 
     try { 
         t = praseInt(s); 
     } catch(e){} 
     this.step = t; 
} 
  
MSN_MESSAGE.prototype.rect = function(left,right,top,bottom){ 
     try { 
        this.right=right!=null?right:(document.documentElement.clientWidth+document.documentElement.scrollLeft); 
         this.left=left!=null?left:this.right-this.width;
         this.bottom=bottom!=null?bottom:(document.documentElement.clientHeight+document.documentElement.scrollTop-(this.height*this.index)); 
         this.top=top!=null?top:(this.bottom - this.height); 
     }
     catch(e)
     {} 
} 

var option={URL:"GetNewMessage.aspx",
            Method:"GET",
            Params:null,
            Success:function(request)
            {
            var arr=eval("("+request.responseText+")");
            ShowMessage(arr)
            },
            Failure:function(){}
}

var arrMsg=[];
var openCount=0;
function ShowMessage(arr)
{
if(arr.length<=0)
return;

    for(var i=0;i<arr.length;i++)
    {
    //判断是否已经存在该消息，如果存在则跳过
     if(Exist("MSG"+arr[i].Id))
     {
     continue;
     }
       var msg= new MSN_MESSAGE({id:"MSG"+arr[i].Id,title:arr[i].Subject,caption:"湖南制造网消息提示",autoHide:false,message:arr[i].Content.substring(0,50),href:"/Manage/Common/Default.aspx?to=Message/AcceptBox.aspx"});  
       msg.onclick=function(){remove(msg.id)}
       msg.onclose=function(){remove(msg.id)}
       msg.speed= 10; 
       msg.step= 25;
       arrMsg.push(msg);
   }
   for(var j=0;j<arrMsg.length;j++)
   {
     //判断该消息是否被用户点击过
      if(arrMsg[j].closed || arrMsg[j].showed)
      { 
      continue; 
      }
      else
      {
       arrMsg[j].index=openCount;
       openCount++;
       arrMsg[j].show();
       arrMsg[j].showed=true;
     }
   }
}
function Exist(id)
{
 for(var j=0;j<arrMsg.length;j++)
   {
    if(arrMsg[j].id==id)
    {
        return true;
    }
   }
   return false;
}
function remove(i)
{
  for(var j=0;j<arrMsg.length;j++)
   {
    if(arrMsg.id==i)
    {
         openCount--;
        arrMsg.closed=true;
    }
   }
}

//没10秒钟触发一次
setInterval(function(){Ajax(option);},5000);
