$('.getquan').css('display', 'none');
// $('#search').css('width', '80%');
$('.saoyisao').hide();
$('.class_block').eq(0).attr('data-classifyId', 5);
$('.class_block').eq(1).attr('data-classifyId', 6);
$('.class_block').eq(2).attr('data-classifyId', 7);
$('.class_block').eq(3).attr('data-classifyId', 8);
$('.class_block').eq(4).attr('data-classifyId', '');
$('.zya_block').addClass('add_');
mui.init({
	beforeback: function() { //获得父页面的webview
		var list = plus.webview.currentWebview().opener(); //触发父页面的自定义事件(refresh),从而进行刷新
		mui.fire(list, 'refresh');
		//返回true,继续页面关闭逻辑
		return true;
	},
});
var data = '';
var addr = '';
var lat = '',
	lat1 = '',
	lng = '';
var page = 1;
var pageSize = 5;
var count = 1;
var isjiazai = 1;
var nowPage = 1;
var isLoading = false;
mui.plusReady(function() {	
	getLocation();
	function getLocation() {
		if (navigator.geolocation) {
			plus.geolocation.getCurrentPosition(showPosition, showError);

		} else {
			mui.alert("手机不支持定位")
		}
	}
	function showPosition(position) {
		
		lat = position.coords.latitude;
		lng = position.coords.longitude;
		localStorage.setItem('lat', lat);
		localStorage.setItem('lng', lng);
		getShopList(nowPage, pageSize);		
		addr = position.address.city;		
		localStorage.setItem('addr', addr);		
		$('#location').html(addr);
		$('#location-r').html(addr);
	}

	function showError(error) {	
		switch (error.code) {
			case error.TIMEOUT:
				mui.alert('请求超时，请重试')
				break;
			case error.POSITION_UNAVAILABLE:
				mui.alert('定位不到你的地址')
				break;
			case error.PERMISSION_DENIED:
			// case 22:
				mui.alert('您拒绝了地理位置请求')
				break;
			case error.UNKNOWN_ERROR:
				mui.alert('未知错误')
				break;
			default:
		        mui.alert(error.message)
				break;
		}			
		getShopList(nowPage, pageSize);
	}
	//bannerTop	
	mui.ajax(qlgUrl('app/shopping/getHelpCarousel'), {
		dataType: 'json', //服务器返回json格式数据  
		type: 'post', //HTTP请求类型  
		timeout: 10000, //超时时间设置为10秒；  
		success: function(data) { //服务器返回响应，根据响应结果，分析是否登录成功；  
			if (data.status == 1) {
				var html = '';
				var data = data.data;
				$.each(data, function() {
					html += '<div class="swiper-slide add_" data-adURL="' + this.adURL + '" data-targetType="' + this.targetType +
						'"><img src="' + hyhImgUrl(this.adFile) + '" alt="" /></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
				});
				var topBannerheight = localStorage.getItem('topBannerheight')

				$('#top_banner').height(topBannerheight)
				// $('#top_banner').height(($('#top_banner').width() )* 460 / 750);
			} else {
				mui.alert(data.msg)
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；  
			//								mui.alert(errorThrown);      
		}
	});

	mui('.con').on('tap', '.bc_img', function() {		
		var goodsId = $(this).attr('data-goodsId');
		JZL.openWindow('details.html', 'details.html' + goodsId, {data_id: goodsId});		
	})
	function getShopList(nowPage, pageSize) {

		lat = localStorage.getItem('lat');
		lng = localStorage.getItem('lng');
		
		if (null == lat) {
			lat = 36.659565;
			lng = 117.125824;
		}
		var recommenddata = {
			shopType: 1,
			page: nowPage ? nowPage : 1,
			perPage: pageSize ? pageSize : 10,
			lat: lat,
			lng: lng,
		}
		if (true == isLoading) return;
		isLoading = true;
		//console.log(lat);
		JZL.ajax(hyhUrl('app/shopping/getHelpShops'), recommenddata, function(data) {

			var html = '';
			var shopLevel
			if (1 == data.status) {
				var data = data.data;
				// console.log(data);
				if ('' == data.Rows) {
					$('.shoplist').append(
						'<p style="float: left;width: 100%;text-align: center;padding-bottom: 9px;">没有更多数据</p>');
					// isLoading = false;
					return;
				}
				$.each(data.Rows, function(index, element) {
					 shopLevel = this.shopLevel
					 if(0==shopLevel){
						 shopLevel =''
					 }
					html +=
						'<div class="con shadown_wai" data-shopId="' + this.shopId +
						'"><div class="shops-content1-title" data-shopId="' + this.shopId +
						'" data-shopName="'+this.shopName +'" ><span class="logoimg"><img src="' + hyhImgUrl(this.shopImg) +
						'"alt=""></span><div class="shop-name">' +
						this.shopName + '<img class ="aixinImg"  src = "../img/aixin.png"><span class="aixinlevel">'+ shopLevel + '</span> </div><span class="gogogo">进店逛逛</span></div><div class="t_con">'
					if (0 != this.goods.length) {
						$.each(this.goods, function(idx, ele) {

							html += '<div class="t_con_ bc_img"  data-goodsId="' + this.goodsId +
								'" data-goodsType = 2 ><div class="t_con_img"><img src="' + hyhImgUrl(this.goodsImg) + '" data-goodsId="' + this.goodsId +
								'" alt=""></div><span>￥' + this.shopPrice + '</span></div>'
						})
					}

					html += '</div><div class="pos clearfix"><img src="../img/dingwei1.png"><span>距离：' + this.distance / 1000 +
						'公里</span></div></div>'
				})
				if (nowPage == 1) {
					$('.shoplist').html(html);
				} else {
					$('.shoplist').append(html);
				}
				isLoading = false;
			} else {
				mui.alert(data.msg)
			}
		})
	}
	//加载
	document.querySelector('.mui-scroll-wrapper').addEventListener('scroll', function(e) {
		if (scroll.y == scroll.maxScrollY) {
			if (isLoading == false) {
				nowPage++;
				// getShops(nowPage, pageSize);
				getShopList(nowPage, pageSize)
			}
		}
	})
	mui('.shoplist').on('tap', '.shops-content1-title', function() {
		// var goodsId = $(this).attr('data-goodsId');
		var shopName = $(this).attr("data-shopName");
		var shopId = $(this).attr("data-shopId");		
		JZL.openWindow('storeout.html', 'storeout.html', {shopName: shopName,shopId: shopId});			
	})
})

//获取推荐页
function getRecommend(page, pagesize) {
	var recommenddata = {
		type : 2 ,
		page: page ? page : 1,
		pagesize: pageSize ? pageSize : 10
	}
	if (isjiazai == 0) {
		return;
	} else {
		isjiazai = 0;
	}
	JZL.ajax(qlgUrl('app/shopping/getGoods'), recommenddata, function(data) {
		 // console.log(data);
		// if (data.status == 1) {
			var html = '';
			// var data = data.data;
			if (data.Rows == '') {
				$('.self_shop_rem').append(
					'<p style="float: left;width: 100%;text-align: center;padding-bottom: 9px;">没有更多商品</p>');
				isjiazai = 0;
				return;
			}
			$.each(data.Rows, function() {
				
				html += '<div class="recommend_con_block  shadown_wai" data-goodsId="' + this.goodsId +
					'" data-goodsType = 2><img class="rcb_img" src="' + ectImgUrl(this.goodsImg) +
					'" alt="" /><div class="rcb_con"><div class="rcb_title"><span style="display:inline-block;" data-shopId="' +
					this.shopId + '"></span>' + this.goodsName + '</div><div class="rcb_pay">￥' + this.shopPrice +
					' <span style="display:none;">满减</span></div><div class="rcb_bottom"><div><span>' + this.saleNum +
					'人购买</span></div><div style="display:none"><span>优惠率&nbsp;&nbsp;' + this.discountRate +
					'</span></div></div></div><img style="display:none;" class="icon_icon" src="../img/icon_sscl.png" alt="" /></div>';
			});

			if (page == 1) {
				$('.recommend_con').html(html);
			} else {
				$('.recommend_con').append(html);
			}
			isjiazai = 1;
			$('.rcb_title span').each(function() {
				if ($(this).attr('data-goodsId') == 1) {
					$(this).css('display', 'none');
				}
			})
			$('.rcb_img').height($('.rcb_img').width());

		// } else {
		// 	mui.alert(data.msg)
		// }
	})
}
$('.bcon_left').height($('.bcon_left').width() * 345 / 185);
$('.bcon_left img').height($('.bcon_left img').width() * 345 / 185);
$('.bcr_block').height($('.bcr_block').width());
$('.addsct_block').height($('.addsct_block').width() * 225 / 355);
$('.addscb_block').height($('.addscb_block').width() * 260 / 180);
$('.zya_block').height($('.zya_block').width() * 185 / 355);

var issx = 0;

// 轮播图跳转
$('#slider').on('tap', '.swiper-slide', function() {
	openAds($(this));
})

//导航栏
document.querySelector('.mui-scroll-wrapper').addEventListener('scroll', function(e) {
	if (-scroll.y < 0) {
		num = 0;
	} else if ((-scroll.y > 0 || scroll.y == 0) && -scroll.y < 150) {
		num = -scroll.y / 150;
	} else {
		num = 1;
	}
	$('.header').css('background-color', 'rgba(255,255,255,' + num + ')')

	if (-scroll.y < 75) {
		$('.saoyisao').attr('src', '../img/saoyisao.png');
		$('.msg').attr('src', '../img/icon_msg.png');
		$('.search').attr('src', '../img/icon_search.png');
		$('#search').css('background-color', 'white');
		$('.header').removeClass('shadown_wai');
	} else {
		$('.saoyisao').attr('src', '../img/saoyisao1.png');
		$('.msg').attr('src', '../img/icon_msg1.png');
		$('.search').attr('src', '../img/icon_search1.png');
		$('#search').css('background-color', '#d8d8d8');
		$('.header').addClass('shadown_wai');
	}

	if (scroll.y > 80 && issx == 0) {
		issx = 1;
		setTimeout(function() {
			location.reload()
		}, 1000)
	}
})
mui('.classify').on('tap', '.class_block', function() {
	var classifyId = $(this).attr('data-classifyId');
	var url = 'orSupermarket.html';

	if (classifyId == '') {
		url = 'ac3.html';
	}
	JZL.openWindow(url, url, {classifyId: classifyId});	
})

//为你推荐
getRecommend(page, pageSize);
document.querySelector('.mui-scroll-wrapper').addEventListener('scroll', function(e) {
	if (scroll.y == scroll.maxScrollY) {
		if (isjiazai == 1) {
			page++;
			getRecommend(page, pageSize);
		}
	}
})
//搜索
$('.header').on('focus', '#keyword', function() {
	$(".search button").css('display', 'block');
})
$('.header').on('blur', '#keyword', function() {
	$(".search button").css('display', 'none');
})
$(".header").on('tap', '.search button', function(e) {
	var searchName = $('#keyword').val();	
	// JZL.openWindow('shopsList.html', 'shopsList.html', {data_keyword: searchName,goodsType:2});
	JZL.openWindow('goodslist.html', 'goodslist.html', {data_keyword: searchName,goodsType :2});
	$('#keyword').val("")
});
//跳转商品详情页
mui("body").on('tap', '.bc_img', function() {
	var goodsId = $(this).attr('data-goodsId');
	var shopId = $(this).parent().parent().attr('data-shopId');
	var goodsType=$(this).attr('data-goodsType')
	JZL.openWindow('details.html', 'details.html' + goodsId, {data_id: goodsId,shopId: shopId,goodsType : goodsType});	
})
