var map = "";
var m_selectHisUrl = "";//选择历史发送过的地址缓存
var m_selectHisConrent = "";
$(function () {
	MapLoad();
});

function MapLoad() {
	// 百度地图API功能
	var map = new BMap.Map("allmap", { enableMapClick: false });

	var point = new BMap.Point(121.505781, 38.924538);
	map.centerAndZoom(point, 17);


	var geoc = new BMap.Geocoder();

	//定位到当前城市
	//function myFun(result) {
	//    var cityName = result.name;
	//    map.setCenter(cityName);
	//}
	//var myCity = new BMap.LocalCity();
	//myCity.get(myFun);


	map.setZoom(13);
	map.enableScrollWheelZoom(true);
	//地图缩放End

	//地图选中获取地址start
	map.addEventListener("click", function (e) {
		var pt = e.point;
		geoc.getLocation(pt, function (rs) {
			var addComp = rs.addressComponents;
			var encodefulladdresstitle = "";
			var encodefulladdresscontent = "";
			var fulladdresstitle = "";
			var fulladdresscontent = "";
			if (rs.surroundingPois.length > 0) {
				encodefulladdresstitle = rs.surroundingPois[0].title == null ? "" : rs.surroundingPois[0].title;
				fulladdresstitle = rs.surroundingPois[0].title == null ? "" : rs.surroundingPois[0].title;
			} else {
				encodefulladdresstitle = rs.business;
				fulladdresstitle = rs.business;
			}

			if (encodefulladdresstitle.length > 0) {
				fulladdresstitle = encodefulladdresstitle;
				encodefulladdresstitle = encodeURIComponent(encodefulladdresstitle);
			}
			if (rs.address.length > 0) {
				encodefulladdresscontent = encodeURIComponent(rs.address);
				fulladdresscontent = rs.address;
			}
			map.clearOverlays();
			var marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat)); // 创建点
			map.addOverlay(marker);    //增加点
			m_selectHisUrl = "";
			$(".hisadd").removeClass("active");
			$("#txtMapSelectAddress").html(fulladdresscontent + fulladdresstitle);
			var address = "http://api.map.baidu.com/marker?location=" + e.point.lat + "," + e.point.lng + "&title=" + encodefulladdresstitle + "&content=" + encodefulladdresscontent + "&output=html";
			$("#hidMapSelectAddressURl").val(address);

			//alert(address);
		});
	});
}

//百度地图modalShow
function MapAddress() {
	//MapLoad();
	$("#mapStateModel").modal("show");
	talkObj.getcommonaddressmsg();
}
//获取所有历史发送过的地址
function AsyncGetCommonAddressMsgResult(jsonResult) {
	var strcotent = "";
	var jsonitem = jsonResult.list[0];
	for (var i = 0; i < jsonitem.length; i++) {
		strcotent += "<div class=\"col-md-12 hisadd\" id=\"HisAddress" + i + "\" onclick=\"SendHisAddress('" + i + "','" + jsonitem[i].maphissendaddressurl + "','"+jsonitem[i].maphissendcontent +"')\">";
		strcotent +="<i class=\"fa fa-map-marker\"></i>";
		strcotent += "<p>" + jsonitem[i].maphissendcontent + "</p>";
		strcotent +="</div>";
	}
	$("#divHisSendAddress").html(strcotent)
}

function SendHisAddress(num,url,content) {
	$(".hisadd").removeClass("active");
	$("#HisAddress" + num).addClass("active")
	m_selectHisUrl = url;
	m_selectHisConrent = content;
	$("#hidMapSelectAddressURl").val("");
	$("#txtMapSelectAddress").html("");
}

function BtnMapSendSelectAddress() {

	var sendaddress = "";
	var sendurl = "";
	var strmapselecturl = $("#hidMapSelectAddressURl").val();
	var strmapselectaddress = $("#txtMapSelectAddress").html();
	//保存到本地
	if (strmapselecturl.length > 0 || m_selectHisUrl.length > 0) {

		//发送地理位置
		if (m_selectHisUrl.length > 0) {
			sendurl = m_selectHisUrl;
			sendaddress = m_selectHisConrent;
		} else if (strmapselecturl.length > 0) {
			var strParaJson = $.toJSON({
				maphissendcontent: strmapselectaddress,
				maphissendaddressurl: strmapselecturl
			});
			talkObj.setcommonaddressmsg(strParaJson);
			sendurl = strmapselecturl;
			sendaddress = strmapselectaddress;
		}
		SendMapUrlMsg(sendurl, sendaddress);
	} else {
		ShowError("myPageAlert", "未选中任何地理位置！");
	}
}

//发送地理位置
function SendMapUrlMsg(mapurl, address) {

	var objhidDocWechatKey = $("#hidDocWechatKey");
	var objhidDocUin = $("#hidDocUin");
	var objhidDocUserName = $("#hidDocUserName");
	var objhidDocPid = $("#hidDocPid");
	var strTxtSendBody = mapurl;
	var strParaJson = $.toJSON({
		longurl: mapurl,
	});
	var resulturl = $.parseJSON(homeObj.longurlchangeshort(strParaJson));
	if (resulturl.success) {
		strTxtSendBody = "【地理位置】\n" + address + "\n点击查看详细\n" + resulturl.result;
	} else {
		strTxtSendBody = "【地理位置】\n" + address + "\n点击查看详细\n" + mapurl;
	}

	if (objhidDocWechatKey.val().length == 0)
		return;
	if (objhidDocUin.val().length == 0)
		return;
	if (objhidDocUserName.val().length == 0)
		return;
	if (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: m_IsBatch,
		content: strTxtSendBody
	});
	talkObj.sendmsg(strParaJson);

	m_EditorControl.setContent("");
	m_EditorControl.clearContent();
	m_selectHisUrl = "";
	$("#hidMapSelectAddressURl").val("");
	$("#txtMapSelectAddress").html("");
}


