﻿
function chat()
{
    this.toUsers = new Array();
    this.tagArray = new Array();
    this.chatInfo = new Array();
    this.managerIndex = 0;
    this.propIndex = 0;
    this.isStop = false;
    this.isGetNewNews = true;
    this.closeTagID = 0;
    this.onTag = null;
    this.propList = {};
    this.newNewsSpace = 0;
    this.getNewsSpace = 0;
    this.managerItem = new Array("<a href='javascript:openurl(\"/wo/user/userinfo.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/userInfoSet.gif' title='个人资料设置' style='cursor:pointer;padding-right:10px' /></a>",
"<a href='javascript:openurl(\"/wo/garden/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/garden.gif' title='魔法花园' style='cursor:pointer;padding-right:9px' /></a>",
"<a href='javascript:openurl(\"/wo/map/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/domain.gif' title='我的地盘' style='cursor:pointer;padding-right:9px' /></a>",
"<a href='javascript:openurl(\"/wo/leaveword/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/leaveword.gif' title='留言' style='cursor:pointer;padding-right:9px' /></a>",
"<a href='javascript:openurl(\"/wo/friends/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/friend.gif' title='好友圈' style='cursor:pointer;padding-right:9px' /></a>",
"<a href='javascript:openurl(\"/wo/Album/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/album.gif' title='相册' style='cursor:pointer;padding-right:10px' /></a>",
"<a href='javascript:openurl(\"/wo/blog/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/blog.gif' title='日记' style='cursor:pointer;padding-right:9px'  /></a>",
"<a href='javascript:openurl(\"/wo/message/MyInNews.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/news.gif' title='更多消息' style='cursor:pointer;padding-right:9px'  /></a>",
"<a href='javascript:openurl(\"/wo/user/account.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/account.gif' title='我的帐户' style='cursor:pointer;padding-right:9px'/></a>",
"<a href='javascript:openurl(\"/wo/wish/default.aspx?id="+userID+"\");'><img src='/wo/images/chat/1/wish.gif' title='愿望管理' style='cursor:pointer;padding-right:10px' /></a>");

    this.tagArray[0] = new Tag("0","全部","");
    if(userID != toUserID)
    {
        this.tagArray[1] = new Tag(toUserID,unescape(toUserName),"");
        var Users = new Array();
        Users[0] = new ToUser(toUserID,toUserName);
        this.toUsers = Users;
        this.onTag = this.tagArray[1];
    }
    else
    {
        this.onTag = this.tagArray[0];
    }
}

function ToUser(UserID,NickName)
{
    this.userID = UserID;
    this.nickName = NickName;   
}

function Tag(UserID,NickName,Latest)
{
    this.userID = UserID;
    this.nickName = NickName;
    this.latest = Latest;
}

function Message(UserID,NickName,AddTime,Content)
{
    this.UserID = UserID;
    this.NickName = NickName;
    this.AddTime = AddTime;
    this.Content = Content;
}

chat.prototype.SelectToUsers = function()
{
    parent.$("ToUserName").value = "";
    var Users = new Array();
    var form = document.forms["UserForm"];
    var j = 0;
    for(var i=0; i< form.elements.length; i++) 
    {
        if (form.elements[i].type=="checkbox" 
            && form.elements[i].checked == true 
            && form.elements[i].name == "chkUser") 
        {
            var infos =  form.elements[i].value.split("|");
            Users[j] = new ToUser(infos[0],infos[1]);
            parent.$("ToUserName").value += infos[1]+",";
            j++;
        }
    }
    return Users;
}

chat.prototype.SelectToUser = function(UserID,NickName)
{
    NickName = unescape(NickName);
    this.isStop = true;
    var tag = new Tag(UserID,NickName,"");
    var isExist = false;
    for(var i=0;i<this.tagArray.length;i++)
    {
        if(this.tagArray[i].userID == UserID)
        {
            isExist = true;
            break;
        }
    }
    
    if(!isExist)
    {
        this.tagArray.splice(1,0,tag);
        if(this.tagArray.length >5)
        {this.tagArray.pop();}
    }
    var Users = new Array();
    Users[0] = new ToUser(UserID,NickName);
    this.toUsers = Users;
    this.onTag = tag;
    this.ViewTag(UserID);
    this.SelectTag(UserID);
    $("ToUserName").value = NickName;
    this.ViewChatInfo();
}

chat.prototype.ViewManageItem = function(state)
{
    if(state == "+")
    {
        var index = this.managerIndex + 1;
        if(index+8 >this.managerItem.length){return;}
        this.managerIndex++;
    }
    else if(state == "-")
    {
        var index = this.managerIndex - 1;
        if(index < 0){return;}
        this.managerIndex--;
    }
    
    $("managerItem").innerHTML = "";
    var j = 0;
    for(var i=this.managerIndex;i<this.managerItem.length;i++)
    {
        if(j>7){break;}
        $("managerItem").innerHTML += this.managerItem[i];
        j++;
    }
}

chat.prototype.GetChatInfo = function()
{
    this.isGetNewNews = false;
    var parameter = "";
    var self = this;
    parameter = addURLParam(parameter,"toUserID",userID);
    if(self.onTag.userID != 0)
    {
        parameter = addURLParam(parameter,"UserID",self.onTag.userID);
    }  
    if(self.onTag.latest != "")
    {
        parameter = addURLParam(parameter,"latest",self.onTag.latest);
    }
    parameter = addURLParam(parameter,"date",Math.random());
    Http.get("/wo/chat/getmessage.aspx"+parameter,function(retext){self.SetChatInfo(retext,"");});    
}

chat.prototype.SetChatInfo = function(info,tag)
{   $("page").innerHTML = "";
    var result = JSON.parse(info);
    for(var i=result.length-1;i>=0;i--)
    {   
        this.chatInfo.unshift(result[i]);
        if(this.chatInfo.length >20)
        {this.chatInfo.pop();}

        if(i == 0)
        {
            for(var j = 0;j< this.tagArray.length;j++)
            {
                if(this.tagArray[j].userID == this.onTag.userID)
                {
                    this.tagArray[j].latest = result[0].AddTime;
                    this.onTag.latest = result[0].AddTime;}
            }
        }
    }

    for(var k=0;k<this.chatInfo.length;k++)
    {
        if(this.chatInfo[k].UserID == userID)
        {
            $("page").innerHTML += "<span class='orange2'>" + unescape(this.chatInfo[k].NickName) +"&nbsp;"+ this.chatInfo[k].AddTime+"&nbsp;说：</span><br/>&nbsp;&nbsp;<span style='color:#FF0000'>"+this.chatInfo[k].Content+"<span><br/>";
        }
        else
        {
            $("page").innerHTML += "<a href=javascript:oChat.SelectToUser('"+this.chatInfo[k].UserID+"','"+escape(this.chatInfo[k].NickName)+"')>" + unescape(this.chatInfo[k].NickName) +"&nbsp;<a/>"+ this.chatInfo[k].AddTime+"&nbsp;说：<br/>&nbsp;&nbsp;<span class='black'>"+this.chatInfo[k].Content+"<span><br/>";
        }
    }
}

chat.prototype.SelectTag = function(tagID)
{
    for(var i=0;i<this.tagArray.length;i++)
    {
        if(this.tagArray[i].userID == tagID)
        {
            $("tag_"+tagID).className = "tag_on";
            this.onTag = this.tagArray[i];
            this.ViewTag();
            this.GetChatInfo();
        }
        else
        {
            $("tag_"+this.tagArray[i].userID).className = "tag";
        }
    }  
}

chat.prototype.ViewTag = function()
{
    this.chatInfo = new Array();
    $("chatInfoTags").innerHTML = "";
    for(var i=0;i<this.tagArray.length;i++)
    {
        var tagClose = "<div id='tagClose_"+this.tagArray[i].userID+"' class='tagClose' style='display:none' title='关闭' onclick='oChat.CloseTag();' onmousemove=\"this.style.display='block';\"></div>";
        if(this.tagArray[i].userID == this.onTag.userID)
        {
            $("chatInfoTags").innerHTML += '<div id="tag_'+this.tagArray[i].userID+'" class="tag_on" onmouseover="oChat.TagOver('+this.tagArray[i].userID+')" onmouseout="oChat.TagOut('+this.tagArray[i].userID+')" onclick="oChat.SelectTag('+this.tagArray[i].userID+')">'+this.tagArray[i].nickName.StringLeft(6)+tagClose+'</div>';
        }
        else
        {
            $("chatInfoTags").innerHTML += '<div id="tag_'+this.tagArray[i].userID+'" class="tag" onmouseover="oChat.TagOver('+this.tagArray[i].userID+')" onmouseout="oChat.TagOut('+this.tagArray[i].userID+')" onclick="oChat.SelectTag('+this.tagArray[i].userID+')">'+this.tagArray[i].nickName.StringLeft(6)+tagClose+'</div>';
        }
    }
}

chat.prototype.ViewSelectUserPage = function(obj)
{
    if($("chatUsers").style.display == "none")
    {
        var top = kg.Page.getRealTop(obj);
	    var left = kg.Page.getRealLeft(obj);
	    //$("chatUsers").style.left = (left-310)+"px";
	    //$("chatUsers").style.top = (top-230)+"px";
        $("chatUsers").style.display = "block";
        obj.src = "/wo/images/chat/1/selectUserOn.gif";
    }
    else
    {
        $("chatUsers").style.display = "none";
        obj.src = "/wo/images/chat/1/selectUser.gif";
    }
}

chat.prototype.ViewChatInfo = function()
{
    this.isStop = true;
    this.ViewTag();
    //this.onTag = this.tagArray[0];
    //var top = kg.Page.getRealTop(obj);
	//var left = kg.Page.getRealLeft(obj);
	//$("chatInfoDiv").style.left=(left+3)+"px";
	//$("chatInfoDiv").style.top=(top-220)+"px";
    $("chatInfoDiv").style.display="block";
}

chat.prototype.ViewNewNews = function()
{
    $("minimizePic").src = "/wo/images/chat/1/minimize.gif";
    var self = this;
    Http.get("/wo/chat/default.aspx",function(retext){self.ViewChat(retext,self);});
    self.ViewChatInfo();
    $("NewMessagePic").style.display = "none"; 
}

chat.prototype.CheckState = function()
{
    if(this.isStop && this.onTag != null)
    {
        if(this.getNewsSpace < 2)
        {
            this.getNewsSpace++;
            return;
        }
        this.getNewsSpace = 0;
        this.GetChatInfo();
    }
    
    if(this.isGetNewNews)
    {
        if(this.newNewsSpace < 6)
        {
            this.newNewsSpace++;
            return;
        }
        this.newNewsSpace = 0;
        this.CheckNewNews();
    }
}

chat.prototype.TimingRefurbish = function()
{
    var self = this;
    kg.Event.add(window,"scroll",function(){setTimeout(self.SetChatInfoTop,20);},false);
    var WinHeight = kg.Page.getWindowClientHeight();
    $("Operate_Chat").style.top = (WinHeight-50)+"px";
    var self = this;
    window.setInterval(function(){self.CheckState();},1000);
}

chat.prototype.TagOver = function(tagID)
{
    
    if(tagID == 0){return};
    this.closeTagID = tagID;
    //var obj = $("tag_"+tagID);
    //var top = kg.Page.getRealTop(obj);
	//var left = kg.Page.getRealLeft(obj);
	//$("tagClose").style.top = (top+3)+"px";
	//$("tagClose").style.left = (left+40)+"px";
	$("tagClose_"+tagID).style.display = "block";
}

chat.prototype.TagOut = function(tagID)
{
	$("tagClose_"+tagID).style.display = "none";
}

chat.prototype.CloseTag = function()
{
	for(var i=0;i<this.tagArray.length;i++)
	{
	    if(this.tagArray[i].userID == this.closeTagID && this.closeTagID != 0)
	    {
	        this.tagArray.splice(i,1);
	        $("tagClose_"+this.closeTagID).style.display = "none";
	        this.onTag = this.tagArray[0];
	        this.ViewTag();
            this.GetChatInfo();
	        break;
	    }
	}
}

chat.prototype.CloseChatPage = function()
{
	$("chatInfoDiv").style.display="none";
	this.isStop = false;
	this.isGetNewNews = true;
}

chat.prototype.SendMessage = function()
{
    var chatContent = $("content").value;
    if(chatContent == "")
    {
        alert("请输入消息内容！");
        return;
    }
    if(this.toUsers.length == 0)
    {
        alert("请选择收件人！");
        return;
    }
    
    var parameter = "";
    var toUserIDs = "";
    for(var i=0;i<this.toUsers.length;i++)
    {
        if(toUserIDs =="")
        {
            toUserIDs = this.toUsers[i].userID;
        }
        else
        {
            toUserIDs += "," + this.toUsers[i].userID;
        }
    }
    parameter = addPostParam(parameter,"toUserID",toUserIDs);
    parameter = addPostParam(parameter,"content",chatContent);
    
    var self = this;
    Http.post("/wo/chat/SendMessage.aspx", parameter,function(retext){self.SendMessageState(retext,toUserIDs);});
}

chat.prototype.SendMessageState = function(info,toUserIDs)
{
    if(info != "ok")
    {
        alert("消息发送失败！");
        return;
    }
    
    var myDate = new Date();
    var strDate = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate()+" "+myDate.toLocaleTimeString();
    var news = new Message(userID,userName,strDate,$("content").value);
    $("content").value = "";
    this.chatInfo.unshift(news);
        if(this.chatInfo.length >20)
        {this.chatInfo.pop();}
      
    $("page").innerHTML = "";  
    for(var k=0;k<this.chatInfo.length;k++)
    {
       if(this.chatInfo[k].UserID == userID)
        {
            $("page").innerHTML += "<span class='orange2'>" + unescape(this.chatInfo[k].NickName) +"&nbsp;"+ this.chatInfo[k].AddTime+"&nbsp;说：</span><br/>&nbsp;&nbsp;<span style='color:#FF0000'>"+this.chatInfo[k].Content+"<span><br/>";
        }
        else
        {$("page").innerHTML += "<a href=javascript:oChat.SelectToUser('"+this.chatInfo[k].UserID+"','"+escape(this.chatInfo[k].NickName)+"')>" + unescape(this.chatInfo[k].NickName) +"&nbsp;<a/>"+ this.chatInfo[k].AddTime+"&nbsp;说：<br/>&nbsp;&nbsp;<span class='black'>"+this.chatInfo[k].Content+"<span><br/>";}
    }
    
    var parameter = "";
    parameter = addURLParam(parameter,"toUserID",toUserIDs);
    parameter = addURLParam(parameter,"UserID",userID);
    parameter = addURLParam(parameter,"date",Math.random());
    Http.get("/wo/chat/settoread.aspx"+parameter,function(retext){});    
}

chat.prototype.Enter = function()
{
    var chatEvend = EventUtil.getEvent();
    if(chatEvend.keyCode == 13)
    {
        oChat.SendMessage();
    }
}

chat.prototype.CheckNewNews = function()
{
    var NewCount = 0;
    var parameter = "";
    var self = this;
    parameter = addURLParam(parameter,"toUserID",userID);
    parameter = addURLParam(parameter,"date",Math.random());
    Http.get("/wo/chat/checknew.aspx"+parameter,function(retext){self.CheckNewNewsBack(retext);});
}

chat.prototype.CheckNewNewsBack = function(NewCount)
{
    if(NewCount > 0)
    {
        //var myName = $("myName");
        //var top = kg.Page.getRealTop(myName);
	    //var left = kg.Page.getRealLeft(myName);
	    //$("NewMessagePic").style.top = top+"px";
	    //$("NewMessagePic").style.left = left+"px";
	    $("NewMessagePic").style.display = "block";  
    }
    else
    {
        $("NewMessagePic").style.display = "none"; 
    }
}

chat.prototype.HideChat = function()
{
    if($("chatThreeDiv").style.display == "block")
    {
        $("chatInfoDiv").style.display="none";
        $("chatThreeDiv").style.display = "none";
        $("chatThreeDiv").style.display = "none";
        $("minimizePic").src = "/wo/images/chat/1/max.gif";
        this.isStop = false;
	    this.isGetNewNews = true;
	    this.SetChatInfoTop();
	}
	else
	{
	    //$("chatInfoDiv").style.display="block";
        //$("chatUsers").style.display = "none";
        $("minimizePic").src = "/wo/images/chat/1/minimize.gif";
        var self = this;
        Http.get("/wo/chat/default.aspx",function(retext){self.ViewChat(retext,self);});
        //this.ViewManageItem("0");
        //this.GetPropList("30000");
        //$("chatThree").style.display = "block";
        //this.isStop = false;
	    //this.isGetNewNews = true;
	}
}

chat.prototype.ViewChat = function(html,self)
{
    setInnerHTML($("chatThreeDiv"),html);
     $("chatThreeDiv").style.display = "block";
    self.ViewManageItem("0");
    self.GetPropList("30000");
    EventUtil.addEvent($("send"),"keydown",oChat.Enter);
    this.SetChatInfoTop();
}

chat.prototype.SetChatInfoTop = function()
{

    $("Operate_Chat").style.left=0+"px";
    var height= kg.Page.getWindowScrollTop()+kg.Page.getWindowClientHeight()-kg.Page.getObjOffsetHeight($("Operate_Chat"));
    if(height< kg.Page.getWindowScrollHeight())
    {
        $("Operate_Chat").style.top =height+"px";
    }
    else{return;}
}

chat.prototype.GetPropList = function(typeID)
{
    this.propIndex = 0;
    var self = this;
    var result = "";
    var parameter = "";
    parameter = addURLParam(parameter,"TypeID",typeID);
    Http.get("/wo/block/GetPropList.aspx"+parameter,function(retext){self.GetPropListBack(retext,self);});
    
}

chat.prototype.GetPropListBack = function(info,self)
{
    self.propList = JSON.parse(info);
    self.ViewProp(0);
}

chat.prototype.ViewProp = function(state)
{  
    if(state == "+")
    {
        var index = this.propIndex + 1;
        if(index+5 >this.propList.length){return;}
        this.propIndex++;
    }
    else if(state == "-")
    {
        var index = this.propIndex - 1;
        if(index < 0){return;}
        this.propIndex--;
    }
    
    $("propList").innerHTML = "";
    var j = 0;
    
    if(this.propList.length > 0)
    {
        for(var i=this.propIndex;i<this.propList.length;i++)
        {
            if(j>4){break;}
            //alert("<li><img src='"+this.propList[i].Photo+"' title='"+this.propList[i].PropName+"' style='width:35px;height:35px;cursor:pointer' onclick='myWoStyle.UsePropFromWidget("+this.propList[i].TypeID+","+this.propList[i].PropID+","+this.propList[i].ID+");' /></li>");
            $("propList").innerHTML += "<li><img src='"+this.propList[i].Photo+"' title='"+this.propList[i].PropName+"' style='width:35px;height:35px;cursor:pointer' onclick='myWoStyle.UsePropFromWidget("+this.propList[i].TypeID+","+this.propList[i].PropID+","+this.propList[i].ID+");' /></li>";
            j++;
        }
    }
    else
    {
        $("propList").innerHTML = "<div style='width:100%;text-align:center; color:Red;line-height:37px;height:37px'>该分类下没有没有道具！</div>";
    }
}

function alerts()
{
//    var aaa = new Array('aaa','bbb');
//    //aaa.unshift("ddd");
//    //aaa.pop();
//    aaa.splice(0,0,"gggg");
//    for(var j=0;j<aaa.length;j++)
//    {
//        alert(aaa[j]);
//    }
    var myDate = new Date();
    var str = myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate()+" "+myDate.toLocaleTimeString();
    var str = escape('~’絲念//,,,""');
    alert(unescape(str));
    
}

String.prototype.getLength=function(){
 var text=this.replace(/[^\x00-\xff]/g,"**");
 return text.length;
}

String.prototype.StringLeft=function(len){
    var tempStr=this;
    if(this.getLength()>len)
    {
        var i=0;
        for(var z=0;z<len;z++)
        {
            if(tempStr.charCodeAt(z)>255)
            {
                i=i+2;
            }
            else{i=i+1;}
            if(i>=len)
            {
                tempStr=tempStr.slice(0,(z + 1));
                break;
            }
        }
        return tempStr;
    }
    else{return this+"";}
} 

function SearchUser()
{    
   var pro = $("pro");
   var age = $("age");
   var radio = document.getElementsByName("sex");
   var sex;
   for(var i=0;i<radio.length;i++)
   {
        if(radio[i].checked)
        {
           sex=radio[i].value;
           break; 
        }
   }
   var p = pro[pro.selectedIndex].value;
   var a = age[age.selectedIndex].value;
   var key = p+" "+a+" "+sex;
   top.location.href="/search.aspx?pro="+p+"&age="+a+"&sex="+escape(sex);   
}