function getNextDate(dayStr) {
	var dd = new Date(dayStr);
	dd.setDate(dd.getDate() + 1);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期 
	var d = dd.getDate();
	return y + "/" + m + "/" + d + " 00:00:00";
};

var seckilling_end_time = '';
var is_start=1;
startSeckilling();
//console.log($(".nav").children('nav_block'))
function startSeckilling(){
	is_start=1;
	$('.time .p1').html(' 秒杀进行中……');
	$('.time .p2').html('距结束:<o id="_h"></o>:<o id="_m"></o>:<o id="_s"></o>');
}
function nextSeckilling(){
	is_start=0;
	$('.time .p1').html(' 下场更精彩……');
	$('.time .p2').html('距开始:<o id="_h"></o>:<o id="_m"></o>:<o id="_s"></o>');
}
function countTime() {

	//获取当前时间
	var date = new Date();
	var now = date.getTime();
	//设置截止时间
	var endDate = new Date(getNextDate(seckilling_end_time*1000));
	var end = endDate.getTime();
	//时间差
	var leftTime = seckilling_end_time*1000 - now;
	
	//定义变量 d,h,m,s保存倒计时的时间
	var h, m, s;
	if(leftTime >= 0) {
		h = Math.floor(leftTime / 1000 / 60 / 60);
		m = Math.floor(leftTime / 1000 / 60 % 60);
		s = Math.floor(leftTime / 1000 % 60);
		//将倒计时赋值到div中
		if(h < 10) {
			h = "0" + h;
		}
		if(m < 10) {
			m = "0" + m;
		}
		if(s < 10) {
			s = "0" + s;
		}
		document.getElementById("_h").innerHTML = h;
		document.getElementById("_m").innerHTML = m;
		document.getElementById("_s").innerHTML = s;
	}

	//递归每秒调用countTime方法,显示动态时间效果
	setTimeout(countTime, 1000);

}

function getData(type, pagesize) {
	//	console.log(type)
	var recommenddata = {
		typeId: type ? type : 1,
		pagesize: pagesize ? pagesize : 10
	}
	
	
	mui.ajax(ectUrl('addon/hyhsale-Goods-alreadyBuy'), { 
		data: recommenddata,
		dataType: 'json', //服务器返回json格式数据  
		type: 'post', //HTTP请求类型  
		timeout: 10000, //超时时间设置为10秒;  
		success: function(data) {           //服务器返回响应,根据响应结果,分析是否登录成功;  
			//				var data = toJson(data, 1);
			if(data.status == 1) {
				var html = '';
				var data = data.data;
				$.each(data.Rows, function() {
					if($(".nav").children('.nav_block').first().hasClass('on')){
						seckilling_end_time = this.endTime;
					}else{
						seckilling_end_time = this.startTime;
					}					
					html += '<div class="con_block shadown_wai" data-goodsId="' + this.goodsId + '" data-specsId="'+this.specsId+'"><img class="img" src="' + ectImgUrl(this.goodsImg) + '"/><p class="p1">' + this.goodsName + '</p><p class="p2" style="display:none;">买3赠1</p><p class="p3">¥<o>' + this.goodsPrice + '</o></p><del>¥<o>' + this.marketPrice + '</o></del>';
					if(1 == is_start){
						html +='<button>去抢购</button><p class="p4">已售</p><progress value=' + this.orderNum + ' max=' + this.totalNum + '></progress>';
					}else{
						html +='<button>原价购买</button>';
					}
					html+='</div>';
				});
				$('.con_').html(html);
				if(1 == is_start){
					$('progress').each(function(num) {
						$('.p4').eq(num).html('已售' + Math.round($(this).attr('value') / $(this).attr('max') * 100) + '%')
					})
				}
				
				if(type==1){
					
				}else{
					
				}
			} else {
				//								console.log(2)
			}
		},
		error: function(xhr, type, errorThrown) {           //异常处理;  
			//								mui.alert(errorThrown);      
		}  
	}); 

}

mui.plusReady(function() {
	getData(1);
	countTime()
	mui('.nav').on('tap', '.nav_block', function() {
		if($(".nav").children('.nav_block').first().hasClass('on')){
			startSeckilling();
		}else{
			nextSeckilling();
		}
		var type = $(this).attr('data-type');
		getData(type);
		$(this).addClass('on').siblings().removeClass('on');
		
		

	})

	mui("body").on('tap', '.con_block', function() {
		//			var tj = plus.webview.create('search.html');
		//			tj.show();
		//			mui.alert(e.target.attributes["data-id"].nodeValue);
		var data_id = this.attributes["data-goodsId"].nodeValue;
		var specsId = this.attributes["data-specsId"].nodeValue;
		//							console.log(this.attributes["data-id"].nodeValue);
		mui.openWindow({
			url: 'details.html',
			id: 'details.html' + data_id,
			styles: {
				top: '0px', //新页面顶部位置
				bottom: '0px', //新页面底部位置
				width: '100%', //新页面宽度,默认为100%
				height: '100%' //新页面高度,默认为100%
			},
			extras: {
				data_id: data_id,
				MiaoshaSaleSpec:specsId,
				isMiaosha:1
				//					..... //自定义扩展参数,可以用来处理页面间传值
			},
			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, //等待框背景区域高度,默认根据内容自动计算合适高度
					//						......
				}
			}
		})
	})

})