var m_MsgPageSize = 20;
var m_MsgMonth = "";
var m_ShowTimeList = [];
var m_MinTick = 936262421150398406;
var m_HisMsgList = [];//聊天记录缓存

var m_NowSelectPid = "";
var m_NowSelectUin = "";
var m_NowSelectFriendHeadImgKey = "";
//重置历史消息记录参数
function ResetHisMsgPara() {
	m_strNowDay = homeObj.gettoday();
	m_MinTick = 936262421150398406;
	//m_MsgPageIndex = 0;
	m_MsgPageSize = 20;
	m_MsgMonth = "";
	m_ShowTimeList = [];
	m_HisMsgList = [];
	$("#divTalkContent").html("");
}

//获取今日对话消息内容
function GetTodayTalk(uin, pid, headkey) {
	m_NowSelectUin = uin;
	m_NowSelectPid = pid;
	m_NowSelectFriendHeadImgKey = headkey;
	ResetHisMsgPara();

	//消息获取
	var strJson = $.toJSON({
		uin: m_NowSelectUin,
		pid: m_NowSelectPid
	});
	homeObj.getcachelist(strJson);
}
//回调
function AsyncGetCacheListResult(jsonResult) {
	if (jsonResult.success) {
		if (jsonResult.list.length > 0) {
			BindCacheMsgs(jsonResult.list);
		} else {
			GetHisMsg();
		}
		$("#DivHistoryMsg").css('display', 'block');
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}

}

function BindCacheMsgs(msgList) {
	//缓存记录自上往下加载
	if (msgList.length > 0) {
		for (var i = 0; i < msgList.length; i++) {
			$("#divTalkContent").append(MakeNewMsgHtml(msgList[i], '1'));
			//地图
			if (msgList[i].msgtype == "11") {
				initbaidumap(msgList[i], "width:180px;height:200px;", "His");
			}
			//时间显示处理，已经显示过，或者本次读取最后一条
			if (!IsShowTime(msgList[i].sorttime) || (i + 1) == msgList.length) {
				m_ShowTimeList.push(msgList[i].sorttime);
				//$("#divTalkContent").append("<div class=\"talktip\">" + msgList[i].sorttime + "</div>");
			}
		}

		var strContent = "<a id=\"btnLookMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetHisMsg();\">更多聊天记录</a><br />";
		$("#divTalkContent").prepend(strContent);

	} else {
		GetHisMsg();//获取历史记录
	}
}
//获取历史消息
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
	});
	talkObj.getfriendhismessage(strParaJson);
}
//回执
function AsyncGetFriendHisMessageResult(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 ($("#divHisMsg" + msgList[i].msgid).length > 0) {
					$("#divHisMsg" + msgList[i].msgid).remove();
				}
				$("#divTalkContent").prepend(MakeNewMsgHtml(msgList[i], '1'));
				if (msgList[i].msgtype == "11") {
					initbaidumap(msgList[i], "width:169px;height:99px;", "His");
				}
				//时间显示处理，已经显示过，或者本次读取最后一条
				if (!IsShowTime(msgList[i].sorttime) || (i + 1) == msgList.length) {
					m_ShowTimeList.push(msgList[i].sorttime);
					//$("#divTalkContent").prepend("<div class=\"talktip\">" + msgList[i].sorttime + "</div>");
					//地图

				}
			}

			var strContent = "<a id=\"btnLookMoreMsg\" href=\"javascript:void(0);\" class=\"moremessage-m\" onclick=\"GetHisMsg();\">更多聊天记录</a><br />";
			$("#divTalkContent").prepend(strContent);

		} else {
			$("#spNoMoreHis").remove();
			var strContent = "<span class=\"moremessage-m\" id=\"spNoMoreHis\">没有更多记录了...</span><br />";
			$("#divTalkContent").prepend(strContent);
		}

		m_MsgMonth = jsonResult.result.month;
		m_MinTick = jsonResult.result.mintick;
		//m_MsgPageIndex = Number(jsonResult.result.pageindex);
	} else {
		ShowError("myPageAlert", "获取数据异常：" + jsonResult.error);
	}
}
//关闭对话记录div
function BtnCloseDivHis() {
	$("#DivHistoryMsg").css('display', 'none');
}
//是否已经显示过此时间
function IsShowTime(timemark) {
	for (var i = 0; i < m_ShowTimeList.length; i++) {
		if (m_ShowTimeList[i] == timemark)
			return true;
	}
	return false;
}