var m_strCollectiontype = "1";//1 表情 2.图片 3.地图
var m_strNowSelectId = "";//当前选中收藏 ID
var m_strNowSelectMsgid = "";//当前选中收藏MsgId
var m_strNowSelectType = "";
var refrch = false;
var m_Page = 1;
var m_PageSize = 60;
var MaxCollection = 0;
//收藏show
function CollectionShow() {
	$("#divCollection").show();
	liCollcetionShow(m_strCollectiontype);
	refrch = false;
}
//收藏close
function BtnCloseCollection() {
	m_strNowSelectId = "";//当前选中收藏 ID
	m_strNowSelectMsgid = "";//当前选中收藏MsgId
	$(".collceimg").removeClass("active");
	$(".collceimg").removeClass("collect-map-histroy-active");
	$("#divCollection").hide();
}
//添加收藏Img
function CollectionMsg(type, wxmsgid, content, uin, para,filename,msgid) {
	var strCollectionContent = type == "3" ? "" : decodeURIComponent(content);
	if (type == "3") {
		strcollectionMag = decodeURIComponent(filename);
	} else if (type == "4") {
		strcollectionMag = decodeURIComponent(filename);
	} else {
		strcollectionMag = decodeURIComponent(content);
	}
	var FileSize = Number (Number(para) / 1024 /1024);
	if (FileSize > 3) {
		ShowMsg("myPageAlert", "文件过大，不能加入到收藏中!");
		return;
	}
	var strParaJson = $.toJSON({
		type: type,
		uin: uin,
		md5: para,
		wxmsgid: wxmsgid,
		content: strcollectionMag,
		url: strCollectionContent,
		msgid: msgid
	});
	talkObj.addcollection(strParaJson);
}
//添加收藏回调
function AsyncAddCollectionResult(jsonResult) {
	if (jsonResult.success) {
		ShowMsg("myPageAlert", "操作成功！");
	} else {
		if (jsonResult.error == "Collectioned!") {
			ShowMsg("myPageAlert", "已经存在在收藏中!");
		} else {
			ShowError("myPageAlert", "操作失败：" + jsonResult.error);
		}
		
	}
}
//获取收藏
function liCollcetionShow(type) {
	m_strCollectiontype = type;
	if (type == "1") {
		m_strNowSelectType = "Collecexpression";
		m_Page = 1;
		m_PageSize = 60;
	} else if (type == "2") {
		m_strNowSelectType = "CollecImg";
		m_Page = 1;
		m_PageSize = 60;
	} else if (type == "3") {
		m_strNowSelectType = "CollecMap";
		m_Page = 1;
		m_PageSize = 6;
	} else {
		m_strNowSelectType = "CollecFile";
		m_Page = 1;
		m_PageSize = 12;
	}
	BindCollection();
}

function BindCollection() {
	var strParaJson = $.toJSON({
		type: m_strCollectiontype,
		pageindex: m_Page,
		pagesize: m_PageSize
	});
	talkObj.getcollection(strParaJson);
}
//获取收藏回调
function AsyncGetCollectionResult(jsonResult) {
	var strcontent = "";
	if (jsonResult.success) {
		var jsonitem = jsonResult.result.list;
		if (m_strCollectiontype == "1") {
			MaxCollection = Math.ceil(Number(jsonResult.result.total) / 60);
			for (var i = 0; i <jsonitem.length; i++) {
			    strcontent += "<li class=\"col-md-1 collceimg\" id=\"Collecexpression" + i + "\" ><img src=\"" + jsonitem[i].url + "\" width=\"50\" height=\"50\" onclick=\"SelectCollection('" + i + "','" + jsonitem[i].id + "','" + jsonitem[i].msgid + "')\" /><i class=\"glyphicon glyphicon-minus\" onclick=\"DeleteCollection('" + jsonitem[i].id + "')\"></i></li>";
			}
			$("#CollectionExpression").html(strcontent);
		} else if (m_strCollectiontype == 2) {
			MaxCollection = Math.ceil(Number(jsonResult.result.total) / 60);
			for (var i = 0; i < jsonitem.length; i++) {
			    strcontent += "<li class=\"col-md-1 collceimg\" id=\"CollecImg" + i + "\" ><img src=\"" + jsonitem[i].url + "\" width=\"50\" height=\"50\"onclick=\"SelectCollection('" + i + "','" + jsonitem[i].id + "','" + jsonitem[i].msgid + "')\" /><i class=\"glyphicon glyphicon-minus\" onclick=\"DeleteCollection('" + jsonitem[i].id + "')\"></i><i class=\"glyphicon glyphicon-zoom-in\" onclick=\"ShowCollectionImgBig('" + encodeURI(jsonitem[i].url) + "');\"  style=\"cursor:pointer;\"></i></li>";
			}
			$("#CollectionImg").html(strcontent);
		} else if (m_strCollectiontype == 3) {
			MaxCollection = Math.ceil(Number(jsonResult.result.total) / 6);
			for (var i = 0; i < jsonitem.length; i++) {
				strcontent += "<div class=\"map-histroy collect-map-histroy collceimg\" id=\"CollecMap" + i + "\" >";
				strcontent += "<div class=\"col-md-12\">";
				strcontent += "<i class=\"icon iconfont icon-position\"></i><p onclick=\"SelectCollection('" + i + "','" + jsonitem[i].id + "','" + jsonitem[i].msgid + "')\">" + jsonitem[i].content + "</p><button class=\"btn btn-danger pull-right\" onclick=\"DeleteCollection('" + jsonitem[i].id + "')\"><i class=\"icon iconfont icon-delete1\"></i></button>";
				strcontent += "</div>";
				strcontent += "</div>";
			}
			$("#CollectionAddress").html(strcontent);
		} else if (m_strCollectiontype == 4) {
			MaxCollection = Math.ceil(Number(jsonResult.result.total) / 12);
			for (var i = 0; i < jsonitem.length; i++) {
				var strfilename = jsonitem[i].content.length > 18 ? jsonitem[i].content.substring(0, 15) + "..." : jsonitem[i].content;
				strcontent += "<li style=\"min-height:115px\" class=\"col-md-2 collceimg\" id=\"CollecFile" + i + "\" ><img src=\"../img/file01.png\" width=\"80\" height=\"80\"onclick=\"SelectCollection('" + i + "','" + jsonitem[i].id + "','" + jsonitem[i].msgid + "')\" style=\"margin:5px 10px;\" /><p>" + strfilename + "</p><i class=\"glyphicon glyphicon-minus\" onclick=\"DeleteCollection('" + jsonitem[i].id + "')\"></i></li>";
			}
			$("#CollectionFile").html(strcontent);
		}
	}
}
//选中收藏中的一张图片
function SelectCollection(num, id, msgid) {
	$(".collceimg").removeClass("active");
	$(".collceimg").removeClass("collect-map-histroy-active");
	//collect - map - histroy - active
	if(m_strNowSelectType == "CollecMap"){
		$("#" + m_strNowSelectType + num).addClass("collect-map-histroy-active");
	} else {
		$("#" + m_strNowSelectType + num).addClass("active");
	}
	
	m_strNowSelectId = id;
	m_strNowSelectMsgid = msgid;
}

//发送选中的收藏
function SendCollection() {
	
	var objhidDocUserName = $("#hidDocUserName");
	var objhidDocPid = $("#hidDocPid");
	var objhidDocUin = $("#hidDocUin");
	if (m_strNowSelectId.length > 0 && m_strNowSelectMsgid.length > 0 && objhidDocPid.val().length > 0) {
		if (m_strCollectiontype == "2") {
			$("#SendImgCollectionBgLoad").show();
			$("#SendImgCollectionTurnLoad").show();
		} else if (m_strCollectiontype == "1") {
			$("#SendbqCollectionBgLoad").show();
			$("#SendbqCollectionTurnLoad").show();
		} else if (m_strCollectiontype == "3") {
			$("#SendMapCollectionBgLoad").show();
			$("#SendMapCollectionTurnLoad").show();
		} else {
			$("#SendFileCollectionBgLoad").show();
			$("#SendFileCollectionTurnLoad").show();
		}
		var strParaJson = $.toJSON({
			id: m_strNowSelectId,
			pid: objhidDocPid.val(),
			uin:objhidDocUin.val(),
			msgid: m_strNowSelectMsgid,
			tousername: objhidDocUserName.val(),
		});
		talkObj.sendcollectionmsg(strParaJson);
		$("#CollectionLoad").show();
		
		//发送
	} else {
		ShowMsg("myPageAlert", "请选择！");
		return;
	}
}

function AsyncSendCollectionMsgResult(jsonResult) {
	if (!jsonResult.success) {
		ShowError("myPageAlert", "发送失败！" + jsonResult.error);
	}
	m_strNowSelectId = "";//当前选中收藏 ID
	m_strNowSelectMsgid = "";//当前选中收藏MsgId
	m_strNowSelectType = "";
	$(".collceimg").removeClass("active");
	$(".collceimg").removeClass("collect-map-histroy-active");
	if (m_strCollectiontype == "2") {
		setTimeout(function () {
			$("#SendImgCollectionBgLoad").hide();
			$("#SendImgCollectionTurnLoad").hide();
			$("#divCollection").hide();
		}, 2000);
	} else if (m_strCollectiontype == "1") {
		setTimeout(function () {
			$("#SendbqCollectionBgLoad").hide();
			$("#SendbqCollectionTurnLoad").hide();
			$("#divCollection").hide();
		}, 2000);
	} else if (m_strCollectiontype == "3") {
		setTimeout(function () {
			$("#SendMapCollectionBgLoad").hide();
			$("#SendMapCollectionTurnLoad").hide();
			$("#divCollection").hide();
		}, 2000);
	}
	else if (m_strCollectiontype == "4") {
		setTimeout(function () {
			$("#SendFileCollectionBgLoad").hide();
			$("#SendFileCollectionTurnLoad").hide();
			$("#divCollection").hide();
		}, 2000);
	}
	
}
//删除收藏
function DeleteCollection(id) {
	var strParaJson = $.toJSON({
		id: id,
	});
	talkObj.deletecollection(strParaJson);
}

function AsyncDeleteCollectionResult(jsonResult) {
	if (jsonResult.success) {
		BindCollection();
	} else {
		ShowError("myPageAlert", "操作失败！" + jsonResult.error);
	}
}

function UpPage() {
	if (m_Page > 1) {
		m_Page--;
		BindCollection();
	} else {
		ShowMsg("myPageAlert", "当前已是第一页！");
	}

}

function NextPage() {
	if (m_Page == MaxCollection) {
		ShowMsg("myPageAlert", "当前已是最后第一页！");
	} else {
		m_Page++;
		BindCollection();
	}
}

//预览收藏图片
function ShowCollectionImgBig(url) {
	$("#SendImgCollectionBgLoad").show();
	$("#SendImgCollectionTurnLoad").show();
	var strParaJson = $.toJSON({
		url:decodeURI(url),
		uin: m_NowSelectUin
	});
	talkObj.downloadcollectionimg(strParaJson);
}

//预览图片回调
function AsyncDownLoadCollectionImgResult(jsonResult) {
	if (jsonResult.success) {
		homeObj.showbigform(jsonResult.result.fileurl)
	} else {
		ShowError("myPageAlert", "预览图片失败！" + jsonResult.error);
	}
	$("#SendImgCollectionBgLoad").hide();
	$("#SendImgCollectionTurnLoad").hide();
}


