$('.recommend_title img').eq(0).attr('src', hyhImgUrl('static/app2/img/goods_title.png'));
$('.recommend_title img').eq(1).attr('src', hyhImgUrl('static/app2/img/coupon_title.png'));
$('.recommend_title img').eq(0).css('display', 'none');
$('.recommend_title img').eq(1).css('display', 'none');
 var shopId = localStorage.getItem('shopId');
 var shopLat = localStorage.getItem('shopLat');
 var shopLng = localStorage.getItem('shopLng');
 var shopName = localStorage.getItem('shopName');
 var phone = localStorage.getItem('phone');
 var userName = localStorage.getItem('userName');
mui.plusReady(function() {
	// var data = JSON.parse(localStorage.getItem('shop_data'));
var self = plus.webview.currentWebview();
from_id = self.from_id?self.from_id:0;
 var userLat='';
 var userLng='';
 var scheme = "";
 var arr = [];
 var title = {};
 var shopAddress="";
 // 加载地图
 getLocation(function(data) {
 	// console.log(data);
 	if (1 == data.status) {
 		$('#lng').val(data.lng);
 		$('#lat').val(data.lat);
 		getMap(shopLng, shopLat)
 		// //console.log(JSON.stringify(data));
 	} else {
 		mui.alert(data.errStr);
 		return;
 	}
 });
 $.getJSON('http://api.map.baidu.com/geocoder/v2/?ak=Zm7kae5pzZogp8a0uwIOlyyz&callback=?&location='+shopLat+','+shopLng+'&output=json&pois=1',function(res){
 	// //console.log(res);
 //addressComponent => {city: "广州市", district: "天河区", province: "广东省", street: "广州大道", street_number: "中922号-之101-128"} 
 // //console.log(res.result.addressComponent.city)
  $(".shopAddress").html(res.result.formatted_address);
  // $("#location-r").html(res.result.addressComponent.city);
 
 })
//  $.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js',function(){  
//  alert(remote_ip_info.country);//国家  
//  alert(remote_ip_info.province);//省份  
//  alert(remote_ip_info.city);//城市  
// });  
// 获取用户地址

plus.geolocation.getCurrentPosition(function (p) {
	//console.log(p.coords.latitude);
	//console.log(p.coords.longitude);
userLat=p.coords.latitude;
userLng=p.coords.longitude
},function (err) {
	
})
// 	mui.ajax(ectUrl('app/Shops/getHome'), {
// 		data: {
// 			shopId: shopId
// 		},
// 		dataType: 'json', //服务器返回json格式数据  
// 		type: 'post', //HTTP请求类型
// 		timeout: 10000, //超时时间设置为10秒;  
// 		success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  
// 
// 			if (data.status == 1) {
// 				var data = data.data;
// 				if (data.shop.shopAds.length > 0) {
// 					var html = '';
// 					$.each(data.shop.shopAds, function() {
// 						html += '<div class="swiper-slide"><img src="' + ectImgUrl(this.adImg) + '" /></div>'
// 					});
// 					$('#top_banner .swiper-wrapper').html(html);
// 					var swiper = new Swiper('#top_banner', {
// 						pagination: '#top_banner_pagination',
// 						spaceBetween: 0,
// 						loop: true,
// 						autoplay: 3500,
// 						autoplayDisableOnInteraction: false
// 					});
// 					$('#top_banner').height($('#top_banner').width() * 420 / 715);
// 					$('#top_banner img').height($('#top_banner img').width() * 420 / 715);
// 				} else {
// 					$('#top_banner').remove()
// 				}
// 
// 				if (data.couponList.coupons.length > 0) {
// 					var html_ = '';
// 					$.each(data.couponList.coupons, function(num) {
// 						html_ += '<div class="swiper-slide home_yhj"  data-couponId="' + this.couponId + '"><img src="  ' +
// 							ectImgUrl('static/app2/img/bg_' + (num % 3 + 1) + '.png') +
// 							'" alt="" /><p class="p1">点击领取</p><p class="p2">满' + this.useMoney + '使用</p><p class="p3">' + this.couponValue +
// 							'</p></div>'
// 					});
// 					$('#timer_swiper .swiper-wrapper').html(html_);
// 					$('.recommend_title img').eq(0).css('display', 'block');
// 					var swiper = new Swiper('#timer_swiper', {
// 						slidesPerView: 2.3,
// 						paginationClickable: true,
// 						spaceBetween: 0,
// 						freeMode: true
// 					});
// 					$('#timer_swiper .swiper-slide img').height($('#timer_swiper .swiper-slide img').width() * 140 / 300);
// 					$('#timer_swiper .swiper-slide').height($('#timer_swiper .swiper-slide').width() * 190 / 300);
// 				} else {
// 					$('.time').css('display', 'none');
// 				}
// 
// 				//				var html3 = '';
// 				//				$.each(data.shopcats, function() {
// 				//					html3 += '<div data-catId="' + this.catId + '" class="l_block">' + this.catName + '</div>';
// 				//				});
// 				//				$('.label').html(html3);
// 				//				$('.l_block').each(function(num) {
// 				//					if((num + 1) % 4 == 0) {
// 				//						$(this).addClass('lbrr');
// 				//					}
// 				//				})
// 				var html4 = '';
// 				if (data.rec == '') {
// 					$('.recommend_title').eq(1).css('display', 'none');
// 				} else {
// 					$.each(data.rec, function() {
// 						html4 += '<div class="recommend_con_block shadown_wai" data-goodsId="' + this.goodsId +
// 							'"><img class="rcb_img" src="' + ectImgUrl(this.goodsImg) +
// 							'" alt="" /><div class="rcb_con"><div class="rcb_title"><span style="display:none;">自营</span>' + this.goodsName +
// 							'</div><div class="rcb_pay">¥' + this.shopPrice +
// 							'<span style="display:none;">满减</span></div><div class="rcb_bottom"><span>可用木吉抵扣20%货款</span></div></div><img class="icon_icon" style="display:none;"src="../img/icon_sscl.png" alt="" /></div>'
// 					});
// 					$('.recommend_con').html(html4);
// 					$('.recommend_title img').eq(1).css('display', 'block');
// 					$('.rcb_img').height($('.rcb_img').width())
// 				}
// 			} else {
// 				mui.alert(data.msg)
// 			}
// 		},
// 		error: function(xhr, type, errorThrown) { //异常处理;  
// 			//					mui.alert(type);      
// 		}
// 	});
	//领取优惠券
	$('.time').on('tap', '.home_yhj', function() {
		var couponId = $(this).attr('data-couponId');
		mui.ajax(hyhUrl('addon/coupon-Coupons-receive'), {
			//			headers: { 
			//				"HYH-Token": token
			//			},
			data: {
				couponId: couponId
			},
			dataType: 'json', //服务器返回json格式数据  
			type: 'post', //HTTP请求类型  
			timeout: 10000, //超时时间设置为10秒;  
			success: function(data) { //服务器返回响应,根据响应结果,分析是否登录成功;  

				var data = toJson(data);
				mui.alert(data.msg);

			},
			error: function(xhr, type, errorThrown) { //异常处理;  
				//					mui.alert(type);    
			}
		});
	})
	$('.recommend_con').on('tap', '.recommend_con_block', function() {
		var good_id = $(this).attr('data-goodsId');
		mui.openWindow({
			url: 'details.html',
			id: 'details.html' + good_id,
			styles: {
				top: '0px', //新页面顶部位置
				bottom: '0px', //新页面底部位置
				width: '100%', //新页面宽度,默认为100%
				height: '100%' //新页面高度,默认为100%
			},
			extras: {
				data_id: good_id
				//					..... //自定义扩展参数,可以用来处理页面间传值
			},
			createNew: false, //是否重复创建同样id的webview,默认为false:不重复创建,直接显示
			show: {
				//					autoShow: true, //页面loaded事件发生后自动显示,默认为true
				//					aniShow: animationType, //页面显示动画,默认为”slide-in-right“;
				//					duration: animationTime, //页面动画持续时间,Android平台默认100毫秒,iOS平台默认200毫秒;
				//					event: 'titleUpdate', //页面显示时机,默认为titleUpdate事件时显示
				//					extras: {} //窗口动画是否使用图片加速
			},
			waiting: {
				autoShow: true, //自动显示等待框,默认为true
				title: '正在加载...', //等待对话框上显示的提示内容
				options: {
					//						width: waiting - dialog - widht, //等待框背景区域宽度,默认根据内容自动计算合适宽度
					//						height: waiting - dialog - height, //等待框背景区域高度,默认根据内容自动计算合适高度
					//						......
				}
			}
		})
	})
	localStorage.setItem('shop_data', '');


	setInterval(function() {
		$('.cnxh_block img').height($('.cnxh_block img').width());
		$('.cnxh_block').height($('.cnxh_block').width() * 266 / 198);
	}, 200)

	

	
	
	$('.go').on("tap", function() {
		isHasApp()
	})


	var maps = [{
			pname: 'com.baidu.BaiduMap',
			action: 'baidumap',
			name: '百度地图'
		}, {
			pname: 'com.autonavi.minimap',
			action: 'iosamap',
			name: '高德地图'
		}
		// , {
		// 	pname: 'com.google.android.apps.maps',
		// 	action: 'com.google.android.apps.maps',
		// 	name: '谷歌地图'
		// }
		// , {
		// 	pname: 'com.tencent.map',
		// 	action: 'com.google.android.apps.maps',
		// 	name: '腾讯地图'
		// },
	]
	// checkApp("com.baidu.BaiduMap", "baidumap", "百度地图")
	// checkApp("com.autonavi.minimap", "iosamap", "高德地图")
	// checkApp("com.google.android.apps.maps", "com.google.android.apps.maps", "谷歌地图")
	// 判断地图软件是否安装
	// maps= toJson(maps)
	// //console.log(maps)
	$.each(maps, function(i, v) {//不能写进点击事件函数里 不然点击一次增加一个title
				var that = this
				checkApp(that.pname, that.action, that.name)
			})
	function isHasApp() {
		if (0 == arr.length) {
// 			plus.nativeUI.confirm("没有地图软件??请安装", function(i) {
// 				if (0 == i.index) {
	                var startpos= bMapTransQQMap(userLng, userLat)
					var endpos = bMapTransQQMap(shopLng, shopLat)
						//console.log(userLng);				
					var urlStr = encodeURI('http://uri.amap.com/navigation?from='+startpos.lng+','+startpos.lat+',startpoint&to='+endpos.lng+','+endpos.lat+',endpoint&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0')
plus.runtime.openURL(urlStr)
// 			plus.runtime.openURL('http://uri.amap.com/navigation?from=116.43234,39.98871,当前位置&to=121,31,站点&mode=car&src=公司名称&callnative=0' // androidMarket('com.baidu.BaiduMap')	
// 				// }
// 
// 			)
			return false
		}
		openMapApp()
	}

	function openMapApp() {
		//底部弹出选择地图软件
		plus.nativeUI.actionSheet({
			title: "请选择!",
			cancel: "取消",
			buttons: arr
		}, function(e) {
		
			var title = arr[e.index - 1].title;
			// var   i1 = new Intent();
			switch (title) {
				case "百度地图":
					// if (plus.os.name == "Android") {
					// 				plus.runtime.openURL(
					// 					"baidumap://map/direction?origin=latlng:39.98871,116.43234|name:我的位置&destination=31.236244,121.480239&mode=driving&src=webapp.navi.hanguosoft.yinyijianghu",
					// 					function(error) {
					// 						mui.alert('打开' + title + '失败');
					// 					});
					// }
					goMaps(
						"baidumap://map/direction?origin=我的位置&destination="+shopLat+","+shopLng+"&mode=driving&src=cn.com.zgqlg.app"
					)
					
					break;
				case "高德地图":

					var endpos = bMapTransQQMap(shopLng, shopLat)
					// 				plus.runtime.openURL('androidamap://navi?sourceApplication=nyx_super&lat=' + endpos.lat + '&lon=' + endpos.lng +
					// 					'&mode=driving&region=www&output=html&src="全亮共"',
					// 					function(e) {
					// 						mui.alert('打开' + title + '失败');
					// 					})
					goMaps('androidamap://navi?sourceApplication=nyx_super&lat=' + endpos.lat + '&lon=' + endpos.lng +
						'&mode=driving&region='+shopName+'&output=html&src="全亮共"')
						
					break;
			}
		})
	}

	function goMaps(url) {
		var mContext = plus.android.runtimeMainActivity();
		var Uri = plus.android.importClass('android.net.Uri');
		var Intent = plus.android.importClass('android.content.Intent');
		var intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		mContext.startActivity(intent);
	}

	function checkApp(pn, ac, name) { //  pname:android 包名  action:ios包名   name:用作底部弹出

		if (plus.runtime.isApplicationExist({
				pname: pn, //包名
				action: ac //ios包名
			})) {
			// //console.log("微信应用已安装");
			title = {
				title: name
			};
			arr.push(title)
		} else {
			// //console.log("微信应用未安装");
			// 		title={};
			// 		arr.push(title)

		}
	}

	mui('.gothere').on("tap", ".gt_con", function(e) {
		var idx = $(this).index();
		var html = '';
		switch (idx) {
			case 0:
				break;
			case 1:
				break;
			case 2:
				// $('.gt_con_con1').css('display','none')

				$('.gt_con_con').toggle()
				html =
					'<div class="row shadown_wai"><div class="name"><label class="label-idx" for="">姓名:</label><label class="label-val" for="">'+userName+'</label></div><div class="name"><label class="label-idx" for="">电话:</label><label class="label-val" for=""><a href="tel:'+phone+'">'+phone+'</label></div></div>'
				$('.row .name').css("width", "60%")
				$('.gt_con_con').html(html)
				break;

		}
	})
})