// var num = 1 / window.devicePixelRatio;
// // document.querySelector('meta').setAttribute('name', 'viewport')
// //document.querySelector('meta').setAttribute('content', 'width=device-width, initial-scale=' + num + ', maximum-scale=' + num + ', minimum-scale=' + num + ', user-scalable=no');
// // document.querySelector('meta').setAttribute('content', 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no');
// 
// // browserRedirect();
// function browserRedirect() {
// 	var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
// 	var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
// 	var width = w > h ? h : w;
// 	var sUserAgent = navigator.userAgent.toLowerCase();
// 	var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
// 	var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
// 	var bIsMidp = sUserAgent.match(/midp/i) == "midp";
// 	var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
// 	var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
// 	var bIsAndroid = sUserAgent.match(/android/i) == "android";
// 	var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
// 	var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
// 	var bIsWX = sUserAgent.match(/MicroMessenger/i) == "micromessenger";
// 	//				document.writeln("您的浏览设备为：");
// 	if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM || bIsWX) {
// 		width = width;
// 		//					document.writeln("phone");
// 	} else {
// 		width = 640;
// 		//					document.writeln("pc");
// 	}
// 	var fz = ~~(width * 64000 / 100) / 6400;
// 	document.getElementsByTagName("html")[0].style.cssText = 'font-size: ' + fz + "px";
// 	var realfz = ~~(+window.getComputedStyle(document.getElementsByTagName("html")[0]).fontSize.replace('px', '') * 6400) / 6400
// 	if(fz !== realfz) {
// 		document.getElementsByTagName("html")[0].style.cssText = 'font-size:' + fz * (fz / realfz) + "px";
// 	}
// 
// }
// window.onresize = function(){
// 	browserRedirect();
// }


var EARTH_RADIUS = 6378137.0; //单位M

var PI = Math.PI;

function getRad(d) {

	return d * PI / 180.0;

}

//						var dis = getFlatternDistance(118.515179,37.478422,118.51471,37.475623);

/**

 * approx distance between two points on earth ellipsoid

 * @param {Object} lat1

 * @param {Object} lng1

 * @param {Object} lat2

 * @param {Object} lng2

 */

function getFlatternDistance(lat1, lng1, lat2, lng2) {

	var f = getRad((lat1 + lat2) / 2);

	var g = getRad((lat1 - lat2) / 2);

	var l = getRad((lng1 - lng2) / 2);

	var sg = Math.sin(g);

	var sl = Math.sin(l);

	var sf = Math.sin(f);

	var s, c, w, r, d, h1, h2;

	var a = EARTH_RADIUS;

	var fl = 1 / 298.257;

	sg = sg * sg;

	sl = sl * sl;

	sf = sf * sf;

	s = sg * (1 - sl) + (1 - sf) * sl;

	c = (1 - sg) * (1 - sl) + sf * sl;

	w = Math.atan(Math.sqrt(s / c));

	r = Math.sqrt(s * c) / w;

	d = 2 * w * a;

	h1 = (3 * r - 1) / 2 / c;

	h2 = (3 * r + 1) / 2 / s;

	return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));

}
/**
 * 获取省份
 */
function get_province() {
	var url = '/index.php?m=Admin&c=Api&a=getRegion&level=1&parent_id=0';
	$.ajax({
		type: "GET",
		url: url,
		error: function(request) {
			alert("服务器繁忙, 请联系管理员!");
			return;
		},
		success: function(v) {
			v = '<option value="0">选择省份</option>' + v;
			$('#province').empty().html(v);
		}
	});
}


/**
 * 获取城市
 * @param t  省份select对象
 */
function get_city(t) {
	var parent_id = $(t).val();
	if (!parent_id > 0) {
		return;
	}
	$('#twon').empty().css('display', 'none');
	var url = '/index.php?m=Home&c=Api&a=getRegion&level=2&parent_id=' + parent_id;
	$.ajax({
		type: "GET",
		url: url,
		error: function(request) {
			alert("服务器繁忙, 请联系管理员!");
			return;
		},
		success: function(v) {
			v = '<option value="0">选择城市</option>' + v;
			$('#city').empty().html(v);
		}
	});
}

/**
 * 获取地区
 * @param t  城市select对象
 */
function get_area(t) {
	var parent_id = $(t).val();
	if (!parent_id > 0) {
		return;
	}
	var url = '/index.php?m=Home&c=Api&a=getRegion&level=3&parent_id=' + parent_id;
	$.ajax({
		type: "GET",
		url: url,
		error: function(request) {
			alert("服务器繁忙, 请联系管理员!");
			return;
		},
		success: function(v) {
			v = '<option value="0">选择区域</option>' + v;
			$('#district').empty().html(v);
		}
	});
}
// 获取最后一级乡镇
function get_twon(obj) {
	var parent_id = $(obj).val();
	var url = '/index.php?m=Home&c=Api&a=getTwon&parent_id=' + parent_id;
	$.ajax({
		type: "GET",
		url: url,
		success: function(res) {
			if (parseInt(res) == 0) {
				$('#twon').empty().css('display', 'none');
			} else {
				$('#twon').css('display', 'block');
				$('#twon').empty().html(res);
			}
		}
	});
}

// 点击收藏商品
function collect_goods(goods_id) {
	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/index.php?m=Home&c=goods&a=collect_goods&goods_id=" + goods_id, //+tab,
		success: function(data) {
			alert(data.msg);
		}
	});
}


//加载地图
function getMap(lng, lat) {
// 	var pcenter = new plus.maps.Point(lng,lat); // 地图中心点
// var map = new plus.maps.Map('allmap');
// map.centerAndZoom(pcenter, 15); // 设置地图的中心点和缩放级别
// map.showUserLocation( true ); // 设置是否显示用户的位置
//  还有很多属性可以设置，请参考官方文档
	var map = new BMap.Map("allmap");
	// var point = new BMap.Point("{$apply_info['lng']|default=118.514285}", "{$apply_info['lat']|default=37.478362}");
	var point = new BMap.Point(lng, lat);
	map.centerAndZoom(point, 16);
	var marker = new BMap.Marker(point);
	map.clearOverlays();
	map.addOverlay(marker);
	marker.setAnimation(BMAP_ANIMATION_BOUNCE);
	function showInfo(e) {
		parent.selectCallBack('data_lng', 'data_lat', document.getElementById('lng').value, document.getElementById('lat').value);
	}
	function showPoint(e) {
		
		document.getElementById('lat').value = e.point.lat;
		document.getElementById('lng').value = e.point.lng;
		var p = new BMap.Point(e.point.lng, e.point.lat);
		var mk = new BMap.Marker(p);
		map.clearOverlays();
		map.addOverlay(mk);
		mk.setAnimation(BMAP_ANIMATION_BOUNCE);
		// //console.log(BMAP_ANIMATION_BOUNCE);
	}
	map.enableScrollWheelZoom(true);
	map.addControl(new BMap.NavigationControl());
	map.addEventListener("click", showPoint);

	function G(id) {
		return document.getElementById(id);
	}

	var ac = new BMap.Autocomplete({
		"input": "shopAddress",
		"location": map
	});
	ac.addEventListener("onhighlight", function(e) {
		var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province + _value.city + _value.district + _value.street + _value.business;
		}
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province + _value.city + _value.district + _value.street + _value.business;
		}
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		// G("searchResultPanel").innerHTML = str;
	});
	var myValue;
	ac.addEventListener("onconfirm", function(e) {
		var _value = e.item.value;
		myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
		G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

		setPlace();
	});
	function setPlace() {
		map.clearOverlays();

		function myFun() {
			var pp = local.getResults().getPoi(0).point;
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp));
		}
		var local = new BMap.LocalSearch(map, {
			onSearchComplete: myFun
		});
		local.search(myValue);
	}
}



// 读取本地地址
function getLocation(callback) {
	// var object = obj;
	plus.geolocation.getCurrentPosition(function(res) {
		//成功回调
		var obj = new Object();
		obj.status = 1;
		obj.lat = res.coords.latitude; //纬度
		obj.lng = res.coords.longitude; //经度
		callback(obj);
	}, function(e) {
		//失败回调
		//console.log('Gelocation Error: code - ' + e.code + '; message - ' + e.message);
		switch (e.code) {
			case 10:
				obj.errStr = '请开启应用的定位权限';
				break;
			case 9:
				//mui.alert('请开启手机定位服务');
				obj.errStr = '请开启手机定位服务';
				break;
			case 2:
				if (e.message.indexOf("[geolocation:13]") > -1) {
					//如果网络开启，定位失败，提示检查定位权限
					obj.errStr = '请开启应用的定位权限';
				}
				if (e.message.indexOf("[geolocation:14]") > -1) {
					//如果应用的权限开了，提示网络异常
					obj.errStr = '请检查网络是否正常';
				}
				break;
			case e.PERMISSION_DENIED:
				obj.errStr = '请求定位被拒绝';
				break;
			case e.POSITION_UNAVAILABLE:
				obj.errStr = "位置信息不可用";
				break;
			case e.TIMEOUT:
				obj.errStr = "获取位置信息超时";
				break;
			case e.UNKNOWN_ERROR:
				obj.errStr = "未知错误";
				break;
		}

		var obj = new Object();
		obj.errCode = e.code;
		obj.status = 0;
		callback(obj);
	}, {
		//超时未获取到经纬度信息  执行失败回调  (默认为5秒)
		timeout: 3000
	});
	//JZL.saveItems('.user_pos', 'user_pos');
}
