var m_userids = "";//人员缓存
var m_NowSelectPid = "";//选中id
var m_MinTick = 936262421150398406;
var m_strNowDay = "";//当前时间
var m_NowSelectUin = "";//当前选择UIN
var m_NowSelectFriendHeadImgKey = "";//当前选择的好友头像KEY
var m_NowSelectFriendHeadImg = "";//当前选择的好友头像
var m_MsgMonth = "";//历史消息日期缓存
var m_MinTick = 936262421150398406;
var m_HisNowMsgList = [];//历史记录缓存
var intSendNum = 0;//WU_FILE_0 发送文件
var m_NowSelectUserid = "";//当前选择好友userid

$(function () {

	m_EditorControl = LREditor.load("LREditor");//初始化编辑器
	$("#LREditor").hide();//默认隐藏文本编辑器
	$("#LREditorSend").hide();//默认以藏发送消息按钮div
	//$("#TopBar div:first").hide();//表情选择器隐藏

	//选择图片扩展
	m_EditorControl.plugs_add({ "name": "image", "class": "image", "id": "image", "title": "图片", "showTip": true }, function (element) {
		element.onclick = function () {
			SelectImageMsg();
		};
	});
	//回车发送消息快捷键
	document.onkeydown = function () {
		//消息编辑框是否为焦点
		if (!$("#Content").is(":focus")) {
			return;
		}
		var currkeycode = "";
		if (window.event.ctrlKey) currkeycode = "17";
		if (window.event.altKey) currkeycode = "18";
		if (window.event.shiftKey) currkeycode = "10";
		currkeycode += window.event.keyCode;
		if (currkeycode == "1713") {
			m_EditorControl.addContent("<div><br/><div/>");
		}
		else if (currkeycode == "13") {
			SendMsgMointor();
			setTimeout(function () { m_EditorControl.setContent(""); }, 200);
		}
	}
	//获取左侧人员部门菜单
	$("#ChangeUserLoad").show();
	BindCus();
	m_strNowDay = monitorObj.gettoday();
	dialogcss();
});

//重置历史消息记录参数
function ResetHisMsgPara() {
	m_MinTick = 936262421150398406;
	//m_MsgPageIndex = 0;
	m_MsgPageSize = 20;
	m_MsgMonth = "";
	m_ShowTimeList = [];
	m_HisMsgList = [];
}
//获取左侧人员部门菜单
function BindCus() {
	monitorObj.getdepttree();
}

//绑定客服列表回调
function AsyncGetDeptTreeResult(jsonResult) {
	var strcontent = "";
	var num = 0;
	var nonewechatnin = "";
	var nonecus = "";
	var deptList = jsonResult.list;
	for (var i = 0; i < deptList.length; i++) {
		strcontent += "<ul class=\"yiji-m\">";
		strcontent += "<li class=\"\">";
		if (deptList[i].user.length > 0) {
			strcontent += "<a href=\"#divEditDept\" class=\"inactive-m inactives-m\">" + deptList[i].dgname + "(" + deptList[i].user.length + ")" + "</a>";
			strcontent += "<ul class=\"dialog-kefulist\" style=\"display: block;\">";
			for (var k = 0; k < deptList[i].user.length; k++) {
				var intalkcount = Number(deptList[i].user[k].nowtalkcount) > 99 ? "99" : deptList[i].user[k].nowtalkcount;
				var strnowtalkuseridcss = deptList[i].user[k].userid == m_NowSelectUserid ? "active" : "";
				strcontent += "<li class=\"CusClas " + strnowtalkuseridcss + "\" id=\"Cus" + deptList[i].user[k].userid + "\" onclick=\"QueryFriendMsg('" + deptList[i].user[k].userid + "')\">" + deptList[i].user[k].name + "<div class=\"pull-right\">" + "</span><i class=\"fa fa-commenting\" data-rel=\"popover\" data-trigger=\"hover\"data-placement=\"top\" data-content=\"对话\"></i><span>" + intalkcount + "</span>" + "<i class=\"round round-green\"  data-rel=\"popover\" data-trigger=\"hover\"data-placement=\"top\" data-content=\"在线\"></i><span>" + deptList[i].user[k].onlinecount + "</span><i class=\"round round-yellow \"  data-rel=\"popover\" data-trigger=\"hover\"data-placement=\"top\" data-content=\"掉线\"></i><span>" + deptList[i].user[k].droppedcount + "</div></li>";
				m_userids += deptList[i].user[k].userid + "|";
			}
		}
		strcontent += "</ul>";
		strcontent += "</li>";
		strcontent += "</ul>";
	}
	$("#divTree").html(strcontent);

	$('#divTree i[data-rel=popover]').popover();
	$("#ChangeUserLoad").hide();
	initTree();

	//赋值设置本人当前可见的USERIDS数据集
	SetThreadJa();
}

//赋值设置本人当前可见的USERIDS数据集
function SetThreadJa() {
	monitorObj.setthreadja(m_userids);
}

//点击客服绑定好友列表
function QueryFriendMsg(userid) {
	m_NowSelectUserid = userid;
	$("#divMonitorTalkContent").empty();
	$(".CusClas").removeClass("active");
	$("#Cus" + userid).addClass("active");
	$("#ChangeUserLoad").show();
	$("#LREditor").hide();//默认隐藏文本编辑器
	$("#LREditorSend").hide();//默认以藏发送消息按钮div
	$("#divWelcome").hide();
	m_NowSelectPid = "";
	var strParaJson = $.toJSON({
		userid: userid
	});
	monitorObj.mointorqueryfriends(strParaJson);
}

//点击客服绑定好友列表回调
function AsyncMonitorQueryFriendsResult(jsonResult) {
	if (jsonResult.success) {
		$("#ulMonitorFriendList").empty();
		var strContent = "";
		if (jsonResult.list.length > 0) {
			$("#divWelcome").hide();
		} else {
			$("#divWelcome").show();
		}
		for (var i = 0; i < jsonResult.list.length; i++) {
			var item = jsonResult.list[i];

			//未读消息数量
			var strNoReadHtml = "";
			var noreadcount = "";
			if (m_NowSelectPid != item.pid && Number(item.noreadcount) > 0) {
				noreadcount = item.noreadcount > 99 ? "..." : item.noreadcount;
				strNoReadHtml = "<font id=\"fontNRC" + item.pid + "\" class=\"infor-number\">" + noreadcount + "</font>";
			}
			//选中样式
			var strSelectHeadActive = "";
			if (m_NowSelectPid == item.pid && item.order < 1) {
				strSelectHeadActive = " class=\"fri active\"";
			}
			//备注名称+昵称
			var strFinalName = item.nickname;
			if (item.remarkname.length > 0) {
				strFinalName = item.remarkname;
			}
			if (strFinalName.indexOf("<span") > 0) {
				strFinalName = strFinalName.replace('"', "");
				var start = strFinalName.indexOf("<span");
				var end = strFinalName.indexOf("span>") + 4;
				var startstr = strFinalName.substring(0, start);
				var endstr = "";
				if (strFinalName.length > end - 1) {
					endstr = strFinalName.substring(end + 1, strFinalName.length);
				}
				strFinalName = startstr + endstr;
			}


			var lasttime = item.lastmsgsorttime == "00:00" ? "" : item.lastmsgsorttime;
			var strsexclass = "";
			if (item.sex == "1") {
				strsexclass = "man";
			} else if (item.sex == "2") {
				strsexclass = "woman";
			} else {
				strsexclass = "";
			}
			strContent += "<li id=\"liFriendHead" + item.pid + "\" " + strSelectHeadActive + " class=\"frid\" onclick=\"SelectMointorFriend('" + item.pid + "','" + item.wechatkey + "','" + item.username + "','" + item.uin + "');\">"
                            + "<a href=\"javascript:void(0)\">"
                                + "<img id=\"imgFlh" + item.pid + "\" src=\"" + item.headimgurl + "\" width=\"49\" height=\"49\" class=\"fanshead pull-left\"  onerror=\"nofind();\">"
                                + "<div class=\"talktext pull-left\">"
                                    + "<h2 id=\"hFriendFinalName" + item.pid + "\" class=\"" + strsexclass + "\">" + strFinalName + "</h2><h3 id=\"hFriendFinalName1" + item.pid + "\" class=\"" + strsexclass + "\">" + strFinalName + "</h3>"
                                    + "<p>" + item.lastmsgcontent + "</p>"
                                + "</div>"
                                + "<div class=\"talktime pull-right\">" + lasttime + "</div>"
                                + strNoReadHtml
                                + "<div class=\"clearfix\"></div>"
                            + "</a>"
                            + "<div class=\"right-hand-button\">"
                            + "</div>"
                        + "</li>";
		}
		$("#ulMonitorFriendList").html(strContent);
	}
	$("#ChangeUserLoad").hide();
}

//获取好友消息Msg
function SelectMointorFriend(pid, wechatkey, username, uin) {

	$("#hidDocWechatKey").val(wechatkey);
	$("#hidDocUserName").val(username);
	$("#hidDocUin").val(uin);
	$("#hidDocPid").val(pid);

	m_NowSelectPid = pid;
	$("#fontNRC" + pid).html("");
	$("#fontNRC" + pid).removeClass("infor-number");
	$("#divMonitorTalkContent").empty();
	$(".frid").removeClass("active");
	$("#ulMonitorFriendList li").each(function () {
		$(this).removeClass("active");
	});
	$("#liFriendHead" + pid).addClass("active");
	$("#divWelcome").hide();
	m_EditorControl.setContent("");
	m_EditorControl.clearContent();
	ResetHisMsgPara();
	var strParaJson = $.toJSON({
		pid: pid
	});
	monitorObj.getmonitorfriendsmsg(strParaJson);
}

//获取好友消息回调
function AsyncGetMonitorFriendsMsgResult(jsonResult) {
	if (jsonResult.success) {
	    var fInfo = jsonResult.info;
	    m_NowSelectFriendHeadImgKey = fInfo.headimgkey;
	    m_NowSelectFriendHeadImg = fInfo.headimgurl;
		m_NowSelectUin = fInfo.uin;
		//备注名称+昵称
		m_NowSelectFriendFinalName = fInfo.nickname;
		var strContent = "<p><a id=\"btnLookMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetHisMsg();\">更多聊天记录</a></p>";
		$("#divMonitorTalkContent").prepend(strContent);
		//追加消息
		AppendMsg(jsonResult.msgs);
	}
	if ($('#LREditor').css('display') == 'none') {
		$("#LREditor").show();//显示编辑器
		// Item is hidden. Write your code.
	}
	if ($('#LREditorSend').css('display') == 'none') {
		$("#LREditorSend").show();//显示发送消息按钮
		// Item is hidden. Write your code.
	}
}


//【已发送】向下插一条消息
function AppendMsg(msgList) {
	for (var i = 0; i < msgList.length; i++) {
		if ($("#divMsg" + msgList[i].msgid).length > 0) {
			$("#divMsg" + msgList[i].msgid).remove();
		}
		$("#divMonitorTalkContent").append(MakeNewMonitorMsgHtml(msgList[i]));
		//地图
		if (msgList[i].msgtype == "11") {
			initbaidumap(msgList[i], "width:230px;height:240px;", "Now");
		}
		//tick reset min value
		if (Number(msgList[i].tick) < m_MinTick) {
			m_MinTick = Number(msgList[i].tick);
		}
	}
	document.getElementById("divMonitorTalkContent").scrollTop = document.getElementById("divMonitorTalkContent").scrollHeight;
	$("#divMonitorTalkContent  i[data-rel=popover]").popover();

}

function MakeNewMonitorMsgHtml(msg, type) {
	var strContent = "";
	var strMsgBody = "";
	var strdiff = parseInt((Date.parse(m_strNowDay) - Date.parse(msg.createtime.substring(0, 10))));
	var strMsgCreatime = strdiff != 0 ? msg.createtime.replace("T", " ") : msg.createtime.substring(11, 16);
	//消息类型过滤
	if (msg.msgtype == "1") {
		if (msg.content.indexOf("/js/LREditor/Expression/") != -1) {
			//表情
			strMsgBody = msg.content;
		} else {

			strMsgBody = "<div class=\"MsgRigth\" id=\"MsgRight" + msg.msgid + "\">" + msg.content + "</div>";
		}
	}
	else if (msg.msgtype == "3" || msg.msgtype == "47") {
		if (msg.content.length == 0) {
			strMsgBody = "【微信表情】";
		} else {
			var strRandom = "";
			if (msg.msgsource != "0") {
				strRandom = "?t=" + monitorObj.getguid();
			}

			if (msg.msgtype == "47") {
				//收藏表情不可查看大图不可下载
				strMsgBody = "<img id=\"msgimg" + msg.msgid + "\" src=\"" + msg.content + strRandom + "\" style=\"width:100px;\"><i id=\"loadimg" + msg.msgid + "\" class=\"fa fa-spinner imgloading fa-pulse fa-3x fa-fw\" style=\"display:none;\"></i><div class=\"imgzhezhao\" id=\"msgzhezhao" + msg.msgid + "\" style=\"display:none\"></div>";
			} else {
				strMsgBody = "<img id=\"msgimg" + msg.msgid + "\" src=\"" + msg.content + strRandom + "\" style=\"width:100px;\" onclick=\"DownMsgImg('" + msg.uin + "','" + msg.msgid + "','" + msg.filename + "','" + msg.createtime + "');\"/><i id=\"loadimg" + msg.msgid + "\" class=\"fa fa-spinner imgloading fa-pulse fa-3x fa-fw\" style=\"display:none;\"></i><div class=\"imgzhezhao\" id=\"msgzhezhao" + msg.msgid + "\" style=\"display:none\"></div>";
			}
		}
	}
	else if (msg.msgtype == "34") {
		//语音消息
		if (msg.msgsource == "0") {
			strMsgBody = "<button class=\"speech\" style=\"cursor：Pointer;\" onclick=\"StartPlayVoice('" + msg.uin + "','" + msg.msgid + "','" + msg.content + "', 0 );\" ></button><span style=\"position:absolute; right:-30px; top:14px; font-size:12px; color:#999;\">" + msg.para + "''</span>";
		} else {
			strMsgBody = "<button class=\"speech speech01\" style=\"cursor：Pointer;\" onclick=\"StartPlayVoice('" + msg.uin + "','" + msg.msgid + "','" + msg.content + "', 0 );\" ></button><span style=\"position:absolute; left:-25px; top:14px; font-size:12px; color:#999;\">" + msg.para + "''</span>";
		}
	}
	else if (msg.msgtype == "10000") {
		if (msg.content.indexOf("开启了朋友验证") > 0 && msg.content.indexOf("请先发送朋友验证请求") && msg.content.indexOf("才能聊天")) {
			strContent = "<div class=\"talktip\">【您不是对方的好友，请先添加好友】</div>";
		} else
			if (msg.content.indexOf("查看异常") > 0 && msg.content.indexOf("我要投诉") > 0) {
				strContent = "<div class=\"talktip\">【与对方发生资金往来可能存在风险.请注意核实身份】</div>";
			} else {
				strContent = "<div class=\"talktip\">【" + msg.content + "】</div>";
			}
		return strContent;
	}
	else if (msg.msgtype == "999") {
		//消息撤回(其他客服聊天过程中撤销)
		if ($("#divMsg" + msg.msgid + "").length > 1) {
			$("#divMsg" + msg.msgid + "").removeClass("conversation").addClass("talktip");
			$("#divMsg" + msg.msgid + "").html("【你撤回了一条消息】");
		} else {
			//手机端撤回
			if (msg.msgsource == "0") {
				//好友撤回的消息
				strContent = "<div id=\"divMsg" + msg.msgid + "\" class=\"talktip\">" + msg.content + "</div><div class=\"clearfix\"></div>";
			} else {
				//客服撤回的消息
				strContent = "<div id=\"" + msg.msgid + "\" class=\"talktip\">【你撤回了一条消息】</div><div class=\"clearfix\"></div>";
			}
			return strContent;
		}
	} else if (msg.msgtype == "49") {
		//文件
		var filenameshow = "";
		filenameshow = msg.filename.length > 40 ? msg.filename.substring(0, 40) + "..." : msg.filename;
		strMsgBody = "<div style=\"line-height:40px; padding:10px; background:#fff; overflow:hidden; border:1px solid #ddd; width:210px; \"><span class=\"pull-left\" style=\"display:block;width:140px;line-height: 20px;\" >" + filenameshow + "</span><div class=\"pull-right talk-file\"><i class=\"icon iconfont icon-file\" style=\"font-size:20px; color:#fff;\"></i></div></div><div  style=\"line-height:20px; padding:10px; background:#fff; overflow:hidden; border:1px solid #ddd; border-top:0 none; width:210px;\"></div>";
	} else if (msg.msgtype == "50") {
		//大文件
		filenameshow = msg.filename.length > 40 ? msg.filename.substring(0, 40) + "..." : msg.filename;

		strMsgBody = "<div style=\"line-height:40px; padding:10px; background:#fff; overflow:hidden; border:1px solid #ddd; width:210px; \"><span class=\"pull-left\"  style=\"display:block;width:140px;\" >" + filenameshow + "</span><div class=\"pull-right talk-file\"><i class=\"icon iconfont icon-file\" style=\"font-size:20px; color:#fff;line-height: 20px;\"></i></div></div><div  style=\"line-height:20px; padding:10px; background:#fff; overflow:hidden; border:1px solid #ddd; border-top:0 none; width:210px;\"><div  class=\"msg-file\"><div class=\"msg-file-bg\" id=\"BigFile" + msg.msgid + "\" style=\"width:" + msg.para + "%\"></div></div></div>";
	} else if (msg.msgtype == "11") {
		//地图
		strMsgBody = "<div  style=\"width:100%;height:200px;border:#ccc solid 1px;\" id=\"dituContent" + msg.msgid + "\"></div>";
	} else if (msg.msgtype == "42") {
		//名片
		strMsgBody = GetCarteHtml(msg, type);
	} else if (msg.msgtype == "12") {
		//图文消息(分享文章)
		var strfilname = msg.filename;
		if (strfilname.indexOf("![CDATA[") != -1) {
			strfilname = msg.filename.replace("![CDATA[", "");
		}
		if (strfilname.indexOf("]]") != -1) {
			strfilname = msg.filename.replace("]]", "");
		}
		strMsgBody = "<div class=\"talk-link\" onclick=\"window.open('" + msg.para + "')\"; style=\"cursor：Pointer;\"><h2>" + msg.content + "</h2><div class=\"talk-link-text\"><p>" + strfilname + "</p></div></div>";
	} else if (msg.msgtype == "19") {
		//红包
		var strpacagemonet = Number(msg.para) > 0 ? "<font style=\"font-size:16px;\">￥</font>" + msg.para + "   " : "";
		strMsgBody = "<div class=\"talk-hongbao\"><div class=\"talk-hongbao-sign\"><img src=\"../img/hongbao.png\" width=\"40\"><div><h2>" + msg.content + "</h2><p>" + strpacagemonet + "请在手机查看</p></div></div><div class=\"talk-hongbao-text\"><p>微信红包</p></div></div>";
	} else if (msg.msgtype == "18") {
		//转账
		strMsgBody = "<div class=\"talk-hongbao\"><div class=\"talk-hongbao-sign\"><img src=\"../img/zhuanzhang.png\" width=\"40\"><div><h2>" + msg.content + "</h2><p>" + msg.para + "</p></div></div><div class=\"talk-hongbao-text\"><p>请在手机查看</p></div></div>";
	}
	if (msg.msgsource == "0") {
		//好友消息

		//右键复制
		var friendmsgcopy = "";
		if (msg.msgtype == "1" && msg.content.indexOf("/js/LREditor/Expression/") == -1 && type == null) {
			friendmsgcopy = "<i data-rel=\"popover\" data-trigger=\"hover\" data-placement=\"top\" data-content=\"复制\" class=\"icon iconfont icon-copy pull-left icon-talk1 icon-talk2\" onclick=\"CopyTextMsg('" + encodeURI(msg.content) + "')\">";
		}

		strContent += "<div id=\"divMsg" + msg.msgid + "\" class=\"conversation\">"
                + "<div class=\"pull-left nickname-m\">"
                + "<span><img src=\"" + m_NowSelectFriendHeadImg + "\" width=\"34\" height=\"34\"></span>"
                + "</div>"
                + "<div class=\"pull-left talkshow-m\"><div class=\"talk-date\">" + strMsgCreatime + "</div><div class=\"talkdetailed-m talkdetailedduihua-m\"style=\"background:#fff; padding:5px;\">"
                + "<img src=\"../img/talksign.png\" class=\"talksign-m\">"
                + "<p>" + strMsgBody + "</p>"
                + "</div>" + friendmsgcopy
                + "</div><div class=\"clearfix\"></div>"
                + "</div>";
	} else {
		//客服消息
		strContent += "<div id=\"divMsg" + msg.msgid + "\" class=\"conversation\">"
            + "<div class=\"pull-right nickname-m\">"
            + "<span><img src=\"" + msg.wechatheadimgurl + "\" width=\"34\" height=\"34\" onerror=\"nofind()\"></span>"
            + "</div>"
            + "<div class=\"pull-right talkshow-m\" ><div class=\"talk-date\" style=\"text-align:right;\">" + strMsgCreatime + "</div><div class=\"talkdetailed-m  talkdetailedduihua-m talkdetailedduihua1-m pull-right\"  style=\" padding:5px;\">"
            + "<p id=\"MsgRight" + msg.msgid + "\">" + strMsgBody + "</p>"
            + "<img src=\"../img/talksign2.png\" class=\"talksign-m\">"
            + "</div>";
		if (msg.msgtype == "1" && msg.content.indexOf("/js/LREditor/Expression/") == -1) {
			strContent += "<i data-rel=\"popover\" data-trigger=\"hover\" data-placement=\"top\" data-content=\"复制\" class=\"icon iconfont icon-copy pull-right icon-talk1 icon-talk2\" onclick=\"CopyTextMsg('" + encodeURI(msg.content) + "')\"></i>";
		}

		//发送失败的消息
		if (msg.isok == "0") {
			strContent += "<i class=\"fa fa-exclamation-circle pull-right\" id=\"ErrorSend" + msg.msgid + "\" style=\"width:15px;height:15px; font-size:17px; margin-top:8px; color:#ff0000; cursor：Pointer;\" onclick=\"SendErrorMsg('" + msg.msgid + "','" + msg.msgtype + "')\"></i>";
		}

		//机器人自动回复
		if (msg.para == "1") {
			strContent += "<i class=\"fa fa-android pull-right\" style=\"width:15px;height:15px; font-size:14px; margin-top:8px;\"  ></i>";
		}
		strContent += "</div>"
		strContent += "</div><div class=\"clearfix\"></div>"
		strContent += "</div>";
	}
	return strContent;
}

//名片Html
function GetCarteHtml(Cartemsgitem, type) {
	var strContent = "";
	var MsgContent = $.parseJSON(Cartemsgitem.content);
	var CarteSex = "";
	if (MsgContent.Sex == "1") {
		CarteSex = "男"
	} else if (MsgContent.Sex == "2") {
		CarteSex = "女"
	} else {
		CarteSex = "未知";
	}

	var AddPersonContent = "";
	var NowOrHis = "MsgRigth MsgRigthHis1";
	if (type == null && Cartemsgitem.msgsource == "0") {
		NowOrHis = "MsgRigth MsgRigth1";
	}
	strContent = "<div class=\"" + NowOrHis + "\" style=\"background:#fff; padding:5px;\">"
                   + "<h2 class=\"visiting-cardtit\">个人名片</h2>"
                   + "<div class=\"visiting-card\">"
                   + "<img src=\"" + MsgContent.bigheadimgurl + "\" class=\"pull-left\" width=\"48\" onclick=\"CarteHeadShowBig('" + Cartemsgitem.msgid + "','" + MsgContent.bigheadimgurl + "','" + Cartemsgitem.uin + "')\" style=\"cursor:pointer\" />"
                   + "<div class=\"pull-left visiting-cardtext\"><h2><p>" + MsgContent.NickName + "</p><span style=\"color:#46b6ef; font-size:12px;\">(" + CarteSex + ")</span></h2>"
                   + "<p class=\"card-city\">" + MsgContent.City + "-" + MsgContent.Province + "</p></div>"
                   + "<div class=\"clearfix\"></div>"
                   + AddPersonContent
                   + " </div>"
                   + "</div>"
	return strContent;
}
//名片点击头像显示大图
function CarteHeadShowBig(msgid, bigheadurl, uin) {

	var strParaJson = $.toJSON({
		uin: uin,
		msgid: msgid,
		url: bigheadurl
	});
	monitorObj.downpic(strParaJson);
	//show_big_img(bigheadurl);
}

function AsyncDownPicResult(jsonResult) {
	if (jsonResult.success) {
		monitorObj.showbigform(jsonResult.result.url);
	}


}
//播放语音
function StartPlayVoice(uin, msgid, filepathh, type) {
	$("#Message" + msgid).remove();
	var strParaJson = $.toJSON({
		uin: uin,
		msgid: msgid,
		filename: filepathh,
		type: type
	});
	monitorObj.startplayvoice(strParaJson);
}

function AsyncStartPlayVoiceResult(jsonResult) {
	if (jsonResult.success) {
	}
	else {
		ShowError("myPageAlert", "语音播放异常！");
	}
}

//下载原图片
function DownMsgImg(uin, msgid, filename, creattime) {
	//将原图片置为加载状态，并且不可再继续点击下载
	if ($("#msgimg" + msgid).length > 0) {
		//所点击放大图片加loading
		$("#loadimg" + msgid).show();
		$("#msgzhezhao" + msgid).show();
	}

	var strParaJson = $.toJSON({
		uin: uin,
		msgid: msgid,
		filename: filename,
		creattime: creattime

	});
	monitorObj.downmsgimg(strParaJson);
}

//下载原图片回调方法
function AsyncDownMsgImgResult(jsonResult) {
	var strMsgId = jsonResult.result.msgid;
	if (jsonResult.success) {
		if (strMsgId.length > 0) {
			//下载后的图片路径，显示放大效果
			//show_big_img(jsonResult.result.newurl);
			monitorObj.showbigform(jsonResult.result.fileurl);
		}
	}
	//所点击放大图片取消loading
	//$("#loadimg" + strMsgId).removeClass("fa fa-spinner fa-pulse fa-3x fa-fw imgloading");
	$("#loadimg" + strMsgId).hide();
	$("#msgzhezhao" + strMsgId).hide();
}

//获取历史消息
function GetHisMsg() {
	if (m_NowSelectPid.length == 0)
		return;

	//防连击
	$("#btnLookMoreMsg").html("正在获取,请稍候...<br />")
	$("#btnLookMoreMsg").attr("onclick", "javascript:void(0);");

	//m_MsgPageIndex++;//下一页
	var strParaJson = $.toJSON({
		pid: m_NowSelectPid,
		headimgkey: m_NowSelectFriendHeadImgKey,
		uin: m_NowSelectUin,
		month: m_MsgMonth,
		tick: m_MinTick,
		pagesize: m_MsgPageSize
	});
	monitorObj.getmonitorhistmessage(strParaJson);
}

//回执
function AsyncGetMonitorHistMessageResult(jsonResult) {
	if (jsonResult.success) {
		if ($("#btnLookMoreMsg").length > 0) {
			$("#btnLookMoreMsg").remove();
		}

		//聊天记录
		var msgList = jsonResult.result.msgs;
		if (msgList.length > 0) {
			for (var i = 0; i < msgList.length; i++) {
				if ($("#divMsg" + msgList[i].msgid).length > 0) {
					$("#divMsg" + msgList[i].msgid).remove();
				}
				if (IsWritePage(msgList[i])) {
					$("#divMonitorTalkContent").prepend(MakeNewMonitorMsgHtml(msgList[i]));
					if (msgList[i].msgtype == "11") {
						initbaidumap(msgList[i], "width:230px;height:240px;", "Now");
					}
					//时间显示处理，已经显示过，或者本次读取最后一条
					if (!IsShowTime(msgList[i].sorttime) || (i + 1) == msgList.length) {
						m_ShowTimeList.push(msgList[i].sorttime);
						if (msgList[i].msgtype == "11") {
							initbaidumap(msgList[i], "width:240px;height:240px;", "Now");
						}
					}
				}
			}
			var strContent = "<p><a id=\"btnLookMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetHisMsg();\">更多聊天记录</a></p>";
			$("#divMonitorTalkContent").prepend(strContent);

		} else {
			var strContent = "<span class=\"moremessage-m\">没有更多记录了...</span>";
			$("#divMonitorTalkContent").prepend(strContent);
		}
		m_MsgMonth = jsonResult.result.month;
		m_MinTick = jsonResult.result.mintick;
		//m_MsgPageIndex = Number(jsonResult.result.pageindex);
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}
	$("#divMonitorTalkContent  i[data-rel=popover]").popover();
}
function IsWritePage(item) {
	var bIsHave = true;
	for (var i = 0; i < m_HisNowMsgList.length; i++) {
		if (m_HisNowMsgList[i].msgid == item.msgid) {
			bIsHave = false;
		}
	}
	return bIsHave;
}
//是否已经显示过此时间
function IsShowTime(timemark) {
	for (var i = 0; i < m_ShowTimeList.length; i++) {
		if (m_ShowTimeList[i] == timemark)
			return true;
	}
	return false;
}
//清空当前对话
function ClearMointorTalk() {
	$("#divMonitorTalkContent").empty();
	$("#LREditor").hide();//默认隐藏文本编辑器
	$("#LREditorSend").hide();//默认以藏发送消息按钮div
}

//**********************************************************发送消息Start*************************************************
//发送消息
function SendMsgMointor() {
	var objhidDocWechatKey = $("#hidDocWechatKey");
	var objhidDocUin = $("#hidDocUin");
	var objhidDocUserName = $("#hidDocUserName");
	var objhidDocPid = $("#hidDocPid");

	var strTxtSendBody = m_EditorControl.getPlainTxt();

	strTxtSendBody = SuperReplace(strTxtSendBody, "&nbsp;", "");
	strTxtSendBody = SuperReplace(strTxtSendBody, "<div><br></div><div><br></div>", "");
	strTxtSendBody = $.trim(strTxtSendBody);

	if ($.trim(objhidDocWechatKey.val()).length == 0)
		return;
	if ($.trim(objhidDocUin.val()).length == 0)
		return;
	if ($.trim(objhidDocUserName.val()).length == 0)
		return;
	if ($.trim(objhidDocPid.val()).length == 0)
		return;
	if (strTxtSendBody.length == 0)
		return;

	var strParaJson = $.toJSON({
		wechatkey: objhidDocWechatKey.val(),
		uin: objhidDocUin.val(),
		username: objhidDocUserName.val(),
		pid: objhidDocPid.val(),
		msgtype: 1,
		isbatch: false,
		content: strTxtSendBody
	});
	monitorObj.sendmonitormsg(strParaJson);

	m_EditorControl.setContent("");
	m_EditorControl.clearContent();
}

//发送消息回调
function AsyncSendMonitorMsgResult(jsonResult) {
	if (jsonResult.success) {
		if ($("#divMsg" + jsonResult.msg.msgid).length > 0) {
			$("#divMsg" + jsonResult.msg.msgid).remove();
		}
		$("#divMonitorTalkContent").append(MakeNewMonitorMsgHtml(jsonResult.msg));
		document.getElementById("divMonitorTalkContent").scrollTop = document.getElementById("divMonitorTalkContent").scrollHeight;
	} else {
		ShowError("myPageAlert", jsonResult.error);
	}
}

//选择发送图片消息
function SelectImageMsg() {
	$("#HidUploadContentLength").val("");
	var strLocalImgUrlJson = $.parseJSON(monitorObj.selectpicmsgfile());
	if (strLocalImgUrlJson[3].length > 0) {
		ShowError("myPageAlert", strLocalImgUrlJson[3]);
		return;
	}
	var strImagePath = strLocalImgUrlJson[0];
	if (strImagePath.length > 0) {
		$("#spSendImageMsg").html(strImagePath);
		$("#imgSendImageMsg").attr("src", "data:image/png;base64," + strLocalImgUrlJson[1]);
		$("#HidUploadContentLength").val(strLocalImgUrlJson[2]);
		$("#sendImageModel").modal("show");
		$("#spSendqqcutshowMsg").hide();
		$("#spSendImageMsg").show();
	}
}
//确认发送图片消息(上传到服务端)
function SendImageMsg() {
	var strImageUrl = $("#spSendImageMsg").html();
	var objhidDocUin = $("#hidDocUin");
	if (strImageUrl.length == 0) {
		return;
	}
	var strParaJson = $.toJSON({
		uin: objhidDocUin.val(),
		file: strImageUrl
	});

	var vcontentlength = $("#HidUploadContentLength").val();
	if (vcontentlength == 0) {
		ShowMsg("myPageAlert", "所发送文件没有任何内容！");
		$("#HidUploadContentLength").val("");
	} else {
		monitorObj.sendimgmessage(strParaJson);
	}
	$("#spSendImageMsg").html("");
	$("#imgSendImageMsg").attr("src", "");
	$("#sendImageModel").modal("hide");
}

//发送文件(上传到服务端)
function SendfileMsg() {

	var strImageUrl = $("#spSendfileMsg").html();
	var objhidDocUin = $("#hidDocUin");
	if (strImageUrl.length == 0) {
		return;
	}
	var strParaJson = $.toJSON({
		uin: objhidDocUin.val(),
		file: strImageUrl
	});

	var vcontentlength = $("#HidUploadContentLength").val();


	monitorObj.sendimgmessage(strParaJson);
	$("#spSendImageMsg").html("");
	$("#imgSendImageMsg").attr("src", "");
	$("#sendImageModel").modal("hide");
}

//文件上传到服务端回调
function AsyncSendImgMessageResult(jsonResult) {

	if (jsonResult.success) {
		//发送消息
		if ($("#divMsg" + jsonResult.msgid).length > 0) {
			$("#divMsg" + jsonResult.msgid).remove();
		}
		// $("#divTalkContent").append(MakeNewMsgHtml(jsonResult));
		document.getElementById("divMonitorTalkContent").scrollTop = document.getElementById("divMonitorTalkContent").scrollHeight;
		SendImgMsg(jsonResult.msgid, jsonResult.filename, jsonResult.filename);//发送图片消息
	} else {
		ShowError("myPageAlert", "发送文件信息异常：" + jsonResult.error);
	}
}
//发送文件
function SendImgMsg(msgid, localfile, filename) {

	var objhidDocUin = $("#hidDocUin");
	var objhidDocWechatKey = $("#hidDocWechatKey");
	var objhidDocUserName = $("#hidDocUserName");
	var objhidDocPid = $("#hidDocPid");
	var strParaJson = $.toJSON({
		wechatkey: objhidDocWechatKey.val(),
		uin: objhidDocUin.val(),
		username: objhidDocUserName.val(),
		pid: objhidDocPid.val(),
		fileid: "WU_FILE_" + intSendNum,
		filename: filename,
		msgid: msgid,
		content: filename
	});
	monitorObj.sendmedia(strParaJson);
	intSendNum++;
}



//****************************************************发送消息End********************************************

//聊天文本复制
function CopyTextMsg(content) {
	var strParaJson = $.toJSON({
		content: decodeURI(content.replace(/%3Cbr%3E|%3Cbr\/%3E/g, '\n'))
	});
	var resultjson = monitorObj.copytextmsg(strParaJson);

	if (resultjson == true) {
		ShowMsg("myPageAlert", "复制成功！");
	} else {
		ShowError("myPageAlert", "复制失败！");
	}
}

//样式操作
function dialogcss() {
	$(".talklist-l").css({
		height: $(window).height()
	});
	$(".data-fanslist").css({
		height: $(window).height()
	});
	$(".list-m").css({
		height: $(window).height() - 45
	});
	$(".talk-content").css({
		height: $(window).height() - 155
	});
	$(".talk-default").css({
		height: $(window).height()
	});

}
$(window).resize(function () {
	dialogcss();
});
//刷新左侧人员部门菜单
function RefreshDisplay() {
	$("#ChangeUserLoad").show();
	BindCus();
	$("#refres").attr("onclick", "");
	$("#refres").text("10秒");
	$("#refres").css("color", "#000000");
	$("#refres").css("background-color", "#E8E8EC");
	$("#refres").css("cursor", "not-allowed");
	var i = 9;
	var time = setInterval(function () {
		if (i > 0) {
			$("#refres").text(i + "秒");
			i--;
		} else {
			$("#refres").text("刷新");
			$("#refres").css("color", "#ffffff");
			$("#refres").css("background-color", "#09bb07");
			$("#refres").css("cursor", "pointer");
			$("#refres").attr('onclick', "RefreshDisplay();");
			clearInterval(time);
		}
	}, 1000);
}
