mui.init({
	pullRefresh: {
		container: '#pullrefresh_chanpin_list',
		down: {},
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
var search_str = "",
	pageNumber = 0,
	pagesize = 10,
	proType = "13";

proType = getParam("proType");

function pullupRefresh() {
	pageNumber += 1;
	mui.ajax(base_path + '/product/productIndex?dates=' + new Date().getTime(), {
		data: {
			"productName": search_str,
			"pageSize": pagesize,
			"pageNO": pageNumber,
			"productType": proType
		},
		dataType: 'json',
		type: 'post',
		timeout: 15000,
		success: function(dt) {
			if(dt.status) {
				var chanpin_data = [];
				chanpin_data = dt.data.productList;	
				mui('#pullrefresh_chanpin_list').pullRefresh().endPullupToRefresh(chanpin_data.length < pagesize);
				var table = document.body.querySelector('.my-tab-view-pull');
				search_str == "" ? search_str = "" : search_str;
				mui(chanpin_data).each(function(index) {
					var pro_color = this.productColor;
					var li = document.createElement('li');
					var yuanjia = '<s class="gwc-pp">原价：¥' + accDiv(this.productPrice, 100) + '</s>';
					if(marquee != '' && this.productDiscount == '328000') {
						yuanjia = '<span class="mui-h6">购买即赠3年滤芯</span>'
					}
					if(proType==1){
					var img='';
					switch (this.colorId){
					case 1:
						img='http://static.iamberry.com/tooth/images/shop/cp-blue.png'
						break;
					case 2:
						img='http://static.iamberry.com/tooth/images/shop/cp-black.png'
						break;
					case 3:
						img='http://static.iamberry.com/tooth/images/shop/cp-green.png'
						break;
					case 4:
						img='http://static.iamberry.com/tooth/images/shop/cp-red.png'
						break;
					default:
						break;
					}}else{
						img=this.productIntroduceImg;
					}
					if($(".box-list-card .iconfont").hasClass("icon-list")) {
						li.className = 'mui-table-view-cell mui-media';
						li.innerHTML = '<a proid="' + this.productId + '" colorid="'+this.colorId+'" class="go_chanpin_details"><img class="mui-media-object mui-pull-left wh-100" src="' + img + '"><div class="mui-media-body gwc-tit">' + this.productName + ' <small class="gwc-pp">(' + pro_color + ')</small></div><div class="my-ellipsis">' + this.productColorDes + '</div><div class="shop-price">' + marquee + '<span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span>' + yuanjia + '</div></div></a>'
					} else {
						li.className = 'mui-table-view-cell mui-media mui-col-xs-6';
						li.innerHTML = '<a proid="' + this.productId + '" colorid="'+this.colorId+'" class="go_chanpin_details"><img class="mui-media-object" src="' + img + '"><div class="mui-media-body gwc-tit">' + this.productName + ' <small class="gwc-pp">(' + pro_color + ')</small></div><div class="my-ellipsis">' + this.productColorDes + '</div><div class="shop-price">' + marquee + '<span class="price-sp">¥' + accDiv(this.productDiscount, 100) + '</span>' + yuanjia + '</div></a>'
					}
					table.appendChild(li)	
				})
			} else {
				mui('#pullrefresh_chanpin_list').pullRefresh().endPullupToRefresh(true)
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr)
		}
	})
};

function search_pullrefresh(str) {
	if(str != null && str != '') {
		if(!checkParameter(str)) {
			mui.alert('您输入的格式有误,请重新输入!')
		} else {
			pageNumber = 0;
			$('.my-tab-view-pull').empty();
			search_str = str;
			mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
			mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
		}
	} else {
		pageNumber = 0;
		$('.my-tab-view-pull').empty();
		search_str = str;
		mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
		mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
	}
};
mui.os.ios ? listener_ipt() : keyup_fn();

function keyup_fn() {
	$('body').on('keyup', '#chanpin-search', function(e) {
		var currKey = 0,
			e = e || event;
		currKey = e.keyCode || e.which || e.charCode;
		var keyName = String.fromCharCode(currKey);
		if(currKey == "13") {
			var str = $.trim($('#chanpin-search').val());
			search_pullrefresh(str)
		}
	})
};
document.getElementById('chanpin-search').addEventListener('input', function(e) {
	var value = e.target.value
});

function listener_ipt() {
	document.getElementById('chanpin-search').onblur = function() {
		var str = $.trim($('#chanpin-search').val());
		search_pullrefresh(str)
	}
};
mui.ready(function() {
	proType = getParam("proType");
	proType == null ? proType = "13" : proType;
	//springFestivalPop();
	marquee_Show();
	/*if(proType != 12) {
		$(".tab-with-chanpin-list .muying").parent().removeClass('mui-active');*/
		switch(proType) {
			/*case "1":
				$(".tab-with-chanpin-list .dingzhi").parent().addClass('mui-active');
				break;*/
            case "13":
                $(".tab-with-chanpin-list .jingying").parent().addClass('mui-active');
                break;
            default:
				$(".tab-with-chanpin-list .jingying").parent().removeClass('mui-active') //去除精英款的样式
                $(".tab-with-chanpin-list .filter").parent().addClass('mui-active');
                break;
			/*case "2":
				$(".tab-with-chanpin-list .filter").parent().addClass('mui-active');
				break;*/

			/*default:
				$(".tab-with-chanpin-list .muying").parent().addClass('mui-active');
				break;*/
		}
	/*}*/
	mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading();
	getCartNum();
	document.querySelector('.loading-bg').style.display = 'none'
});

function checkParameter(remark) {
	if(!/^[\u4e00-\u9fa5_a-zA-Z0-9-]+$/.test(remark)) {
		return false
	}
	return true
};
$(document).on('tap', '.box-list-card', function() {
	var listcard = $(this).children().first();
	if(listcard.hasClass("icon-list")) {
		listcard.removeClass("icon-list").addClass("icon-list-card");
		$(".my-tab-view-pull").removeClass("mui-table-view-chevron").addClass("mui-grid-view");
		$(".mui-media-object").removeClass("mui-pull-left wh-100");
		$(".my-tab-view-pull .mui-table-view-cell").addClass("mui-col-xs-6")
	} else {
		listcard.removeClass("icon-list-card").addClass("icon-list");
		$(".my-tab-view-pull").removeClass("mui-grid-view").addClass("mui-table-view-chevron");
		$(".mui-media-object").addClass("mui-pull-left wh-100");
		$(".my-tab-view-pull .mui-table-view-cell").removeClass("mui-col-xs-6")
	}
});
mui.each(document.querySelectorAll('.tab-with-chanpin-list .mui-control-item'), function(index, el) {
	el.addEventListener('tap', function() {
		pageNumber = 0;
		/*switch(index) {
			case 0:
				proType = 12;
				break;
			case 1:
				proType = 13;
				break;
			case 2:
				proType = 1;
				break;
            case 3:
                proType = 2;
                break;
			default:
				proType = 12;
				break;
		}*/
        switch(index) {
            case 0:
                proType = 13;
                break;
            case 1:
                proType = 2;
                break;
            default:
                proType = 13;
                break;
        }
		$('.my-tab-view-pull').empty();
		mui('#pullrefresh_chanpin_list').pullRefresh().refresh(true);
		mui('#pullrefresh_chanpin_list').pullRefresh().pullupLoading()
	}, false)
});