/**
 * @author Samsung
 */


var ChatMsgType ={RECEIVE_MSG:0, SEND_MSG:1 };
var ChatMsgLength ={S:0, M:1, L:2 };


function ChatMsgBox(scene_Id, flagwithCall){
   
    this.SceneId="";
    this.flagSub = false;
    
    if(scene_Id)
    {
        this.SceneId =scene_Id;
    }
            
    this.ChatMsgBoxId ="chat_msg_box_full";
    this.ChatMsgId = "chat_msg_anchor_full";
    if(flagwithCall)
    {
        this.ChatMsgBoxId ="chat_msg_box_sub";
        this.ChatMsgId = "chat_msg_anchor_sub";
        this.flagSub  = flagwithCall;
    }
    
    $("<div id='"+ this.ChatMsgBoxId +"' />").appendTo("#"+ this.SceneId);
    
    this.msgList = new Array();
  
    this.boxHeight = 360;
    this.currHeight =0;
    this.msgNum = 0;
	this.topMsgNum =1;
	this.bottomMsgNum=1;
 
    if(this.flagSub==true)
   {
        this.tdThumbnailStyle = "td_sub_thumbnail";
        this.tdEmptyStyle = "td_sub_empty";
    }
    else
    {
        this.tdThumbnailStyle = "td_full_thumbnail";
        this.tdEmptyStyle = "td_full_empty";   
    }
    this.pLeftDateStyle = "p_full_left_date";
    this.pRightDateStyle = "p_full_right_date";
    
    this.tableStyle="table_s";
    this.tdMsgStyle ="td_full_msg_s"
    this.pLeftMsgStyle = "p_full_left_msg_s";
    this.pRightMsgStyle = "p_full_right_msg_s";
    this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_FULL_S_BG;
    this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_FULL_S_BG;
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    this.msgInfoList = new Array();
    this.msgInfoNum =0;
    
    this.onCompleteFunc=null;
	
	this.flagOldmsg = false;
	
	this.chatMemberInfo = new Array();
	this.chatMemberNum =0;
    
}

ChatMsgBox.prototype.initMsgBox=function()
{  
    $("#"+ this.ChatMsgBoxId).empty();
		 
    this.currHeight =0;
    this.msgNum = 0;
	this.msgList.length =0;

	this.msgInfoNum =0;
	this.msgInfoList.length = 0;
	
	this.chatMemberNum =0;
	this.chatMemberInfo.length = 0;
    
    this.topMsgNum =1;
	this.bottomMsgNum=1;
	
	 if(this.onCompleteFunc)
     {
        this.onCompleteFunc();
     }	 
}

ChatMsgBox.prototype.getTotalMsgNum=function()
{
    return  this.msgNum;
}

ChatMsgBox.prototype.getTopMsg=function()
{
    return  this.topMsgNum;
}

ChatMsgBox.prototype.getBottomMsg=function()
{
    return  this.bottomMsgNum;
}

ChatMsgBox.prototype.setOnCompleteFunc=function(func){
    if(func)
    {
        this.onCompleteFunc=func;
    }
}

ChatMsgBox.prototype.updateMsgBox=function(chat_msg, isOldMsg)
{
    var ChatPropNum = 5;
    
    if (chat_msg) {
		var STX = String.fromCharCode(2);
		var ETX = String.fromCharCode(3);
		var splitter = ETX + STX;
		
		var Result = chat_msg.split(splitter);
		var totalMsgNum = parseInt(Result.length / ChatPropNum);
		//alert("totalMsgNum = " + totalMsgNum );
		
		var TokenNum = 0;
		
		this.flagOldmsg = isOldMsg;
		
		if (this.flagOldmsg == false) {
			for (var i = 0; i < totalMsgNum; i++) {
				this.msgInfoNum++;
				TokenNum = i * ChatPropNum;
				
				this.msgInfoList[this.msgInfoNum] = new Object;
				
				this.msgInfoList[this.msgInfoNum].skype_name = Result[TokenNum];
				this.msgInfoList[this.msgInfoNum].display_name = Result[TokenNum + 1];
				this.msgInfoList[this.msgInfoNum].date = Result[TokenNum + 2];
				this.msgInfoList[this.msgInfoNum].msg = Result[TokenNum + 3];
				this.msgInfoList[this.msgInfoNum].status = Result[TokenNum + 4];
				
				// alert( this.msgInfoNum + " : " +  this.msgInfoList[this.msgInfoNum].skype_name + " , " + this.msgInfoList[this.msgInfoNum].display_name + " , "+ this.msgInfoList[this.msgInfoNum].date + " , " +this.msgInfoList[this.msgInfoNum].msg + " , "  +this.msgInfoList[this.msgInfoNum].status);
				
				this.addMsg(this.msgInfoNum);
			}
		}
		else {
	
			for (var i =(totalMsgNum -1) ; i >= 0; i--) {
				this.msgInfoNum++;
				TokenNum = i * ChatPropNum;

				this.msgInfoList[this.msgInfoNum] = new Object;
				
				for(var a=1; a < this.msgInfoList.length-1; a++)
				{
					this.msgInfoList[this.msgInfoList.length-a].skype_name = this.msgInfoList[this.msgInfoList.length-a-1].skype_name;
					this.msgInfoList[this.msgInfoList.length-a].display_name = this.msgInfoList[this.msgInfoList.length-a-1].display_name;
					this.msgInfoList[this.msgInfoList.length-a].date = this.msgInfoList[this.msgInfoList.length-a-1].date;
					this.msgInfoList[this.msgInfoList.length-a].msg = this.msgInfoList[this.msgInfoList.length-a-1].msg;
					this.msgInfoList[this.msgInfoList.length-a].status = this.msgInfoList[this.msgInfoList.length-a-1].status;
				}
				
				this.msgInfoList[1] = new Object;
				this.msgInfoList[1].skype_name = Result[TokenNum];
				this.msgInfoList[1].display_name = Result[TokenNum + 1];
				this.msgInfoList[1].date = Result[TokenNum + 2];
				this.msgInfoList[1].msg = Result[TokenNum + 3];
				this.msgInfoList[1].status = Result[TokenNum + 4];
				
				this.addMsg(1);
			
			}
		}	
	}

      if(this.onCompleteFunc)
     {
        this.onCompleteFunc();
     }
}


ChatMsgBox.prototype.addMsg=function(index)
{
    var max_display_length = 90;
    var num_display_mag = Math.ceil( this.msgInfoList[index].msg.length / max_display_length);
     
    var i=0;
	
    //for(var i=num_display_mag-1 ; i >= 0 ; i--)
	for( i=0 ; i < num_display_mag ; i++)
    {
		if(this.flagOldmsg==false)
		{
			var start_msg = i*max_display_length;
        	var end_msg = start_msg + max_display_length;
		}
		else
		{
			var start_msg = ((num_display_mag -1)- i)*max_display_length;
        	var end_msg = start_msg + max_display_length;
		}

		
        var msg_content = this.msgInfoList[index].msg.substring(start_msg, end_msg);

         if(msg_content.length <= (max_display_length / 3))
         {
            this.ChatMsgLength =ChatMsgLength.S;
         }
         else if(msg_content.length <= (max_display_length / 3) *2)
         {
            this.ChatMsgLength =ChatMsgLength.M;
         }
         else
         {
            this.ChatMsgLength =ChatMsgLength.L;
         }
         
         if(this.msgInfoList[index].status =="1" || this.msgInfoList[index].status =="2")
         {
            this.msgType=ChatMsgType.SEND_MSG;
         } 
         else
         {
            this.msgType=ChatMsgType.RECEIVE_MSG;
         }
     
        this.msgNum++;
		if(this.flagOldmsg==false)
		{
			this.msgList[this.msgNum] = new Object;
		}
		else
		{
			this.msgList[this.msgNum] = new Object;
			
			for(var a=1; a < this.msgList.length-1; a++)
			{
				this.msgList[this.msgList.length-a].contents = this.msgList[this.msgList.length-a-1].contents;
				this.msgList[this.msgList.length-a].msg = this.msgList[this.msgList.length-a-1].msg;
				this.msgList[this.msgList.length-a].id = this.msgList[this.msgList.length-a-1].id;
				this.msgList[this.msgList.length-a].h = this.msgList[this.msgList.length-a-1].h;
				this.msgList[this.msgList.length-a].avata =  this.msgList[this.msgList.length-a-1].avata;
				this.msgList[this.msgList.length-a].skype_name =  this.msgList[this.msgList.length-a-1].skype_name;
				this.msgList[this.msgList.length-a].display_name = this.msgList[this.msgList.length-a-1].display_name;
			}
		}
           
        this.setStyle();
        
        switch(this.msgType)
        {
            case ChatMsgType.RECEIVE_MSG:
                this.addReceiveMsg(msg_content, this.msgInfoList[index].date, PluginAPIMgr.GetChatMemberAvataImage(this.msgInfoList[index].skype_name), this.msgInfoList[index].skype_name, PluginAPIMgr.GetChatMemberDisplayName(this.msgInfoList[index].skype_name));
            break;
            
            case ChatMsgType.SEND_MSG:
                this.addSendMsg(msg_content, this.msgInfoList[index].date, PluginAPIMgr.GetMyAvataImageForChat(), PluginAPIMgr.GetMySkyeName(), PluginAPIMgr.GetMyDisplayName());
            break;
        }
        
       /* 
        if((this.topMsgNum == 1 && this.msgNum ==1)  || this.topMsgNum == this.msgNum-1)
        {
            this.currHeight = this.currHeight + this.msgList[ this.msgNum].h;
            this.scrollMsgUp();
             $(this.msgList[this.msgNum].contents).prependTo("#"+ this.ChatMsgBoxId);			
			this.insertMsgInfo(this.msgNum);
			this.topMsgNum = this.msgNum;
       }
       */
	  
	  if((this.bottomMsgNum == 1 && this.msgNum == 1) || this.bottomMsgNum == this.msgNum - 1) 
	  {
	  	this.currHeight = this.currHeight + this.msgList[this.msgNum].h;
	  	this.scrollMsgUp();
	  	$(this.msgList[this.msgNum].contents).appendTo("#" + this.ChatMsgBoxId);
	  	this.insertMsgInfo(this.msgNum);
	  	this.bottomMsgNum = this.msgNum;
	  }
	   
	   /*if(this.flagOldmsg==true)
	   {
	   		this.topMsgNum++;
			this.bottomMsgNum++;
			
			if(this.bottomMsgNum ==2)
			{
				if(this.currHeight +this.msgList[this.bottomMsgNum-1].h <= this.boxHeight)
				{
					$(this.msgList[this.bottomMsgNum-1].contents).appendTo("#"+ this.ChatMsgBoxId);
					this.insertMsgInfo(this.bottomMsgNum-1);		                
	                this.currHeight = this.currHeight +this.msgList[this.bottomMsgNum-1].h;	                
	                this.bottomMsgNum--; 
				}
			}
	   } */ 
	   
	   if(this.flagOldmsg==true)
	   {
			this.topMsgNum++;
			this.bottomMsgNum++;
			
			if(this.topMsgNum ==2)
			{
				if(this.currHeight +this.msgList[this.topMsgNum-1].h <= this.boxHeight)
				{
					$(this.msgList[this.topMsgNum-1].contents).prependTo("#"+ this.ChatMsgBoxId);
					this.insertMsgInfo(this.topMsgNum-1);		                
					this.currHeight = this.currHeight +this.msgList[this.topMsgNum-1].h;	                
					this.topMsgNum--; 
				}
			}
	   }    
	}
}

ChatMsgBox.prototype.insertMsgInfo=function(msg_index)
{
	this.insertMsg(msg_index);
	this.insertAvata(msg_index);
	this.insertDiaplayName(msg_index);
}

ChatMsgBox.prototype.insertMsg=function(msg_index)
{
	$("div#"+ this.msgList[msg_index].id+" p:first").text(this.msgList[msg_index].msg);
}

ChatMsgBox.prototype.insertAvata=function(msg_index)
{
	$("div#"+ this.msgList[msg_index].id+" img:first").attr("src",  this.msgList[msg_index].avata);
}

ChatMsgBox.prototype.insertDiaplayName=function(msg_index)
{
	$("div#"+ this.msgList[msg_index].id+"_"+this.msgList[msg_index].skype_name).html(this.msgList[msg_index].display_name);
}

ChatMsgBox.prototype.scrollMsgUp= function()
{
    var anchor_id ="";
     
    while(this.currHeight > this.boxHeight )
    {
		//alert("ChatMsgBox.prototype.scrollMsgUp: bottomMsgNum"+ this.bottomMsgNum);
        anchor_id = this.msgList[this.topMsgNum].id; 
		//alert("ChatMsgBox.prototype.scrollMsgUp remove: "+anchor_id);
        $("#"+anchor_id).remove();
        this.currHeight = this.currHeight  - this.msgList[this.topMsgNum].h ;
        this.topMsgNum++;
    }
}

//ChatMsgBox.prototype.msgUp=function()
ChatMsgBox.prototype.msgDown=function()
{   
    if( this.msgNum>0 && this.bottomMsgNum != this.msgNum)
    {
		var anchor_id = this.msgList[this.bottomMsgNum+1].id; 
		//alert("ChatMsgBox.prototype.msgUp:"+anchor_id);
		this.currHeight = this.currHeight + this.msgList[ this.bottomMsgNum+1].h;
		this.scrollMsgUp();
		$(this.msgList[this.bottomMsgNum+1].contents).appendTo("#"+ this.ChatMsgBoxId);
		this.insertMsgInfo(this.bottomMsgNum+1);
		this.bottomMsgNum++;

        if(this.bottomMsgNum <this.msgNum)
        {
            while(this.currHeight +this.msgList[this.bottomMsgNum+1].h <= this.boxHeight)
           {
                $(this.msgList[this.bottomMsgNum+1].contents).appendTo("#"+ this.ChatMsgBoxId);
                this.insertMsgInfo(this.bottomMsgNum+1);
				this.currHeight = this.currHeight +this.msgList[this.bottomMsgNum+1].h ;
                
                this.bottomMsgNum++;
                
                 if(this.bottomMsgNum >=this.msgNum)
                {
                    break;
                }
           }
       }
     }
     
     if(this.onCompleteFunc)
    {
        this.onCompleteFunc();
    }
}

ChatMsgBox.prototype.scrollMsgDown= function()
{
    var anchor_id ="";
     
    while(this.currHeight > this.boxHeight )
    {
        //alert("ChatMsgBox.prototype.scrollMsgDown: topMsgNum"+ this.topMsgNum);
        
        anchor_id = this.msgList[this.bottomMsgNum].id; 
		//alert("ChatMsgBox.prototype.scrollMsgDown remove: "+anchor_id);
        $("#"+anchor_id).remove();
        this.currHeight = this.currHeight  - this.msgList[this.bottomMsgNum].h ;
        this.bottomMsgNum--;
    }
}

//ChatMsgBox.prototype.msgDown=function()
ChatMsgBox.prototype.msgUp=function()
{   
    if( this.msgNum>0 && this.topMsgNum >1)
    {
        var anchor_id = this.msgList[this.topMsgNum-1].id;
        //alert("ChatMsgBox.prototype.msgUp:"+ anchor_id);
		this.currHeight = this.currHeight  + this.msgList[this.topMsgNum-1].h ;
		this.scrollMsgDown();

            $(this.msgList[this.topMsgNum-1].contents).prependTo("#"+ this.ChatMsgBoxId);         
			this.insertMsgInfo(this.topMsgNum-1);
            this.topMsgNum--;
            
        if(this.topMsgNum >1)
        {
           while( this.currHeight +this.msgList[this.topMsgNum-1].h <= this.boxHeight)
           {
                $(this.msgList[this.topMsgNum-1].contents).prependTo("#"+ this.ChatMsgBoxId);
                this.insertMsgInfo(this.topMsgNum-1);
				this.currHeight = this.currHeight +this.msgList[this.topMsgNum-1].h;
                
                this.topMsgNum--;    
                
                if(this.topMsgNum <=1)
                {
                    break;
                }
           }
       }
     }
     
     if(this.onCompleteFunc)
    {
        this.onCompleteFunc();
    }
}

ChatMsgBox.prototype.setStyle= function()
{    
    switch( this.ChatMsgLength)
    {
        case  ChatMsgLength.S:
            this.tableStyle="table_s";
             if(this.flagSub==true)
            {
                this.tdMsgStyle ="td_sub_msg_s";           
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_SUB_S_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_SUB_S_BG;            
            }
            else
            {
                this.tdMsgStyle ="td_full_msg_s";
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_FULL_S_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_FULL_S_BG;
            }
            this.pLeftMsgStyle = "p_full_left_msg_s";
            this.pRightMsgStyle = "p_full_right_msg_s";
			if(this.flagOldmsg==false)
			{
				this.msgList[this.msgNum].h = 90;
			}
			else
			{
				this.msgList[1].h = 90;
			}
            
        break;
        
        case  ChatMsgLength.M:
            this.tableStyle="table_m";
             if(this.flagSub==true)
            {
                this.tdMsgStyle ="td_sub_msg_m";
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_SUB_M_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_SUB_M_BG;
            }
            else
            {
                this.tdMsgStyle ="td_full_msg_m";
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_FULL_M_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_FULL_M_BG;
            }
            this.pLeftMsgStyle = "p_full_left_msg_m";
            this.pRightMsgStyle = "p_full_right_msg_m";

			if(this.flagOldmsg==false)
			{
				this.msgList[this.msgNum].h = 124;
			}
			else
			{
				this.msgList[1].h = 124;
			}
            
        break;
        
        case  ChatMsgLength.L:
            this.tableStyle="table_l";
             if(this.flagSub==true)
            {
                this.tdMsgStyle ="td_sub_msg_l";
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_SUB_L_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_SUB_L_BG;
            }
            else
            {
                this.tdMsgStyle ="td_full_msg_l";
                this.imageLeft =g_Image.IMAGE_CHAT_MSG_LEFT_FULL_L_BG;
                this.imageRight =g_Image.IMAGE_CHAT_MSG_RIGHT_FULL_L_BG;
            }
            this.pLeftMsgStyle = "p_full_left_msg_l";
            this.pRightMsgStyle = "p_full_right_msg_l";

			if(this.flagOldmsg==false)
			{
				this.msgList[this.msgNum].h = 157;
			}
			else
			{
				this.msgList[1].h = 157;
			}
            
        break;
    
    }
}

ChatMsgBox.prototype.addReceiveMsg=function(message, date, avata_path, skype_name, display_name)
{
	this.AddChatMemberInfo(skype_name, avata_path, display_name);	
    var anchor_id =this.ChatMsgId+String(this.msgNum);
	
    var MsgHTML = "";
    MsgHTML += "<tr>"; 
    MsgHTML += "<td class ='"+this.tdThumbnailStyle+"'>";
	MsgHTML += "<div id ='"+anchor_id+"_"+skype_name+"' style =' overflow: hidden; white-space : nowrap; text-overflow : ellipsis;' class='td_full_left_name' ></div >";
	MsgHTML += "<div style=' background-image :url("+g_Image.IMAGE_CHAT_THUMBNAIL+")' class ='td_sub_left_thumbnail_bg'><img class ='td_full_thumbnail_image'/></div>";
	MsgHTML += "</td>";
	MsgHTML += "<td style=' background-image :url("+this.imageLeft+"); background-repeat: no-repeat;' class='"+this.tdMsgStyle+"' >";
    MsgHTML += "<p style ='word-break : break-all; ' class ='" +this.pLeftMsgStyle+"'></p>";
    MsgHTML += "<p class='"+this.pLeftDateStyle+"' >"+date+"</p >";
    MsgHTML += "</td>";
    MsgHTML += "<td class ='"+this.tdEmptyStyle+"'></td>";
    MsgHTML += "</tr>";
    
    var wrapHTML = "";    
    wrapHTML += "<div id = '"+anchor_id+"'>"
    wrapHTML += "<table cellpadding='0px' cellspacing='0px'  class ='"+this.tableStyle+"'>";
	wrapHTML += MsgHTML;
	wrapHTML += "</table>";
    wrapHTML += "</div>";
    
	if(this.flagOldmsg==false)
	{
		this.msgList[ this.msgNum].contents = wrapHTML; 
		this.msgList[ this.msgNum].msg =  message;
		this.msgList[ this.msgNum].id =  anchor_id;
		this.msgList[ this.msgNum].avata =  avata_path;
		this.msgList[ this.msgNum].skype_name =  skype_name;
		this.msgList[ this.msgNum].display_name =  display_name;
	}
	else
	{
		this.msgList[1].contents = wrapHTML; 
		this.msgList[1].msg =  message;
		this.msgList[1].id =  anchor_id;
		this.msgList[1].avata =  avata_path;
		this.msgList[1].skype_name =  skype_name;
		this.msgList[1].display_name =  display_name;
	}

}

ChatMsgBox.prototype.addSendMsg=function(message, date, avata_path, skype_name, display_name)
{
    var anchor_id =this.ChatMsgId+String(this.msgNum);	

    var MsgHTML = "";
    MsgHTML += "<tr>"; 
    MsgHTML += "<td class ='"+this.tdEmptyStyle+"'></td>";
    MsgHTML += "<td style=' background-image :url("+this.imageRight+"); background-repeat: no-repeat;' class='"+this.tdMsgStyle +"'>";
    MsgHTML += "<p style ='word-break : break-all; ' class ='" +this.pRightMsgStyle+"'></p >";
    MsgHTML += "<p class='"+this.pRightDateStyle+"' >"+date+"</p >";
    MsgHTML += "</td>";
    MsgHTML += "<td class ='"+this.tdThumbnailStyle+"'>";
	MsgHTML += "<div id ='"+anchor_id+"_"+skype_name+"' style =' overflow: hidden; white-space : nowrap; text-overflow : ellipsis;' class='td_full_right_name' ></div >";
	MsgHTML += "<div style=' background-image :url("+g_Image.IMAGE_CHAT_THUMBNAIL+")' class ='td_sub_right_thumbnail_bg'><img class ='td_full_thumbnail_image'/></div>";
	MsgHTML += "</td>";
	MsgHTML += "</tr>";
    
    var wrapHTML = "";    
	wrapHTML += "<div id = '"+anchor_id+"'>"
	wrapHTML += "<table cellpadding='0px' cellspacing='0px'  class ='"+this.tableStyle+"'>";
	wrapHTML += MsgHTML;
	wrapHTML += "</table>";
	wrapHTML += "</div>";

	if(this.flagOldmsg==false)
	{
		this.msgList[ this.msgNum].contents = wrapHTML; 
		this.msgList[ this.msgNum].msg =  message;
		this.msgList[ this.msgNum].id =  anchor_id;
		this.msgList[ this.msgNum].avata =  avata_path;
		this.msgList[ this.msgNum].skype_name =  skype_name;
		this.msgList[ this.msgNum].display_name =  display_name;
	}
	else
	{
		this.msgList[1].contents = wrapHTML; 
		this.msgList[1].msg =  message;
		this.msgList[1].id =  anchor_id;
		this.msgList[1].avata =  avata_path;
		this.msgList[1].skype_name =  skype_name;
		this.msgList[1].display_name =  display_name;
	}
}

ChatMsgBox.prototype.AddChatMemberInfo = function(skype_name, avata_path, display_name)
{
	alert("ChatMsgBox.prototype.AddChatMemberInfo: "+ this.chatMemberNum);
	
	var added = false;
	
	if(this.chatMemberNum >0)
	{
		var a =1;
		for(a=1 ; a <= this.chatMemberNum; a++)
		{
			if(this.chatMemberInfo[a].skype_name == skype_name)
			{
				added = true;
				break;
			}
		}
	}
	 
	if(added == false)
	{
		this.chatMemberNum++;
		this.chatMemberInfo[this.chatMemberNum] = new Object;
		this.chatMemberInfo[this.chatMemberNum].skype_name = skype_name;
		this.chatMemberInfo[this.chatMemberNum].avata = avata_path;
		this.chatMemberInfo[this.chatMemberNum].display_name = display_name;
	}
}

ChatMsgBox.prototype.UpdateChatMemberInfo = function()
{	
	alert("ChatMsgBox.prototype.UpdateChatMemberInfo: "+ this.chatMemberNum);
	
	if(this.chatMemberNum==0)
	{
		return;
	}
	
	var a=1;
	for(a=1; a <= this.chatMemberNum; a++)
	{
		this.chatMemberInfo[a].avata = PluginAPIMgr.GetChatMemberAvataImage(this.chatMemberInfo[a].skype_name);	
		this.chatMemberInfo[a].display_name = PluginAPIMgr.GetChatMemberDisplayName(this.chatMemberInfo[a].skype_name);	
	
	}
	
	for(a=1; a <= this.chatMemberNum; a++)
	{
		var b=1;
		for(b=1; b<= this.msgNum; b++)
		{
			if(this.chatMemberInfo[a].skype_name == this.msgList[b].skype_name )
			{
				this.msgList[b].avata = this.chatMemberInfo[a].avata;
				this.msgList[b].display_name = this.chatMemberInfo[a].display_name;
			}
		}
		
		var c=0;
		var displayed_num = this.bottomMsgNum - this.topMsgNum +1;
		for (c = 0; c < displayed_num; c++) 
		{
			if(this.chatMemberInfo[a].skype_name == this.msgList[this.topMsgNum + c].skype_name)
			{
				this.insertAvata(this.topMsgNum + c);
				this.insertDiaplayName(this.topMsgNum + c);
			}				
		}
	}	
}




////////////////////////////////////////////////////////////////////////////////////////////////////////

ChatMsgBox.prototype.addSpace = function(msg)
{
    var msg_size = msg.length;
    var offset =0;
    
    var new_msg= "";
    
    var i =0;
    for(i=0; i<msg_size; i++)
    {
        var ch = msg.substring(i,  i+1);
         offset++;
         
         new_msg = new_msg +ch;
         
        if(offset >=29)
        {
            new_msg = new_msg + " ";
            offset =0;
        }
        else
        {
            if(ch == " ")
            {
                offset =0;
            }
        }      
    }
    
    return new_msg;
}

ChatMsgBox.prototype.NormalizeMsg=function(message)
{
       if(message.length <=60)
   {
        if(message.indexOf(" ") == -1 ||  message.indexOf(" ") >29)
        {
            var message_1 = message.substring(0, 30) +" ";
            var message_2 = message.substring(30);
            
            message = message_1 + message_2;
        }
   }
    else
   {
        if(message.indexOf(" ") == -1 ||  message.indexOf(" ") >29)
        {
            var message_1 = message.substring(0, 60);
            var message_2 = message.substring(60);
            if(message_1.indexOf(" ") == -1 ||  message_1.indexOf(" ") >29)
            {
                    var message_3 = message_1.substring(0, 30) +" ";
                    var message_4 = message_1.substring(30);
                    
                    message_1 = message_3+message_4;
            }
            
            if(message_2.length <=30)
            {
                message = message_1 + " "+ message_2;
            }
            else
            {
                message = message_1;
            }
        }
   }
   
   alert("ChatMsgBox.prototype.NormalizeMsg : "+message);
   
   return message

}

