$(function(){
	$('.goodsImg2').lazyload({ effect: "fadeIn",failurelimit : 10,skip_invisible : false,threshold: 100,placeholder:window.conf.RESOURCE_PATH+'/'+window.conf.GOODS_LOGO});//商品默认图片
	WST.dropDownLayer(".item",".dorp-down-layer");
	$('.item-more').click(function(){
		if($(this).attr('v')==1){
			$('.hideItem').show(300);
			$(this).find("span").html("收起");
			$(this).find("i").attr({"class":"drop-up"});
			$(this).attr('v',0);
		}else{
			$('.hideItem').hide(300);
			$(this).find("span").html("更多选项");
			$(this).find("i").attr({"class":"drop-down-icon"});
			$(this).attr('v',1);
		}
	});
	
	$(".item-more").hover(function(){
		if($(this).find("i").hasClass("drop-down-icon")){
			$(this).find("i").attr({"class":"down-hover"});
		}else{
			$(this).find("i").attr({"class":"up-hover"});
		}
		
	},function(){
		if($(this).find("i").hasClass("down-hover")){
			$(this).find("i").attr({"class":"drop-down"});
		}else{
			$(this).find("i").attr({"class":"drop-up"});
		}
	});
});
//对比商品
function contrastGoods(show,id,type){
	if(show==1){
		$.post(WST.U('home/goods/contrastGoods'),{id:id},function(data,textStatus){
			var json = WST.toJson(data);
			if(json.status==1){
				if(type==2 && json.data)$("#j-cont-frame").addClass('show');
				var gettpl = document.getElementById('colist').innerHTML;
				laytpl(gettpl).render(json, function(html){
					$('#contrastList').html(html);
				});
				$('.contImg').lazyload({ effect: "fadeIn",failurelimit : 10,skip_invisible : false,threshold: 200,placeholder:window.conf.RESOURCE_PATH+'/'+window.conf.GOODS_LOGO});//商品默认图片
			}else{
				WST.msg(json.msg,{icon:2});
			}
			if(type==1)$("#j-cont-frame").addClass('show');
		});
	}else{
		$("#j-cont-frame").removeClass('show');
	}
}
//删除
function contrastDels(id){
	$.post(WST.U('home/goods/contrastDel'),{id:id},function(data,textStatus){
		var json = WST.toJson(data);
		if(json.status==1){
			contrastGoods(1,0,1);
		}
	});
}
//对比商品列表
//滑动
function fixedGoods(){
    var offsetTop = $("#goodsTabs").offset().top;
    $(window).scroll(function() {  
        var scrollTop = $(document).scrollTop();  
        if (scrollTop > offsetTop){
            $("#goodsTabs").addClass('goods-fixed');
            $("#goodsTabs2").show();
        }else {  
            $("#goodsTabs").removeClass('goods-fixed');
            $("#goodsTabs2").hide();
        }  
    });   
}
//删除
function contrastDel(id){
	$.post(WST.U('home/goods/contrastDel'),{id:id},function(data,textStatus){
		var json = WST.toJson(data);
		if(json.status==1){
			location.href=WST.U('home/goods/contrast');
		}
	});
}
function goodsMore(n){
	var num = $("#more_"+n).val();
	if(6<num && num<13){
		if($("#open_"+n).attr('class').indexOf('more1')==-1){
			$("#open_"+n).addClass('more1');
		}else{
			$("#open_"+n).removeClass('more1');
		}
	}else if(12<num && num<19){
		if($("#open_"+n).attr('class').indexOf('more2')==-1){
			$("#open_"+n).addClass('more2');
		}else{
			$("#open_"+n).removeClass('more2');
		}
	}else if(18<num && num<25){
		if($("#open_"+n).attr('class').indexOf('more3')==-1){
			$("#open_"+n).addClass('more3');
		}else{
			$("#open_"+n).removeClass('more3');
		}
	}else if(24<num && num<31){
		if($("#open_"+n).attr('class').indexOf('more4')==-1){
			$("#open_"+n).addClass('more4');
		}else{
			$("#open_"+n).removeClass('more4');
		}
	}else if(30<num && num<37){
		if($("#open_"+n).attr('class').indexOf('more5')==-1){
			$("#open_"+n).addClass('more5');
		}else{
			$("#open_"+n).removeClass('more5');
		}
	}else if(36<num && num<43){
		if($("#open_"+n).attr('class').indexOf('more6')==-1){
			$("#open_"+n).addClass('more6');
		}else{
			$("#open_"+n).removeClass('more6');
		}
	}else if(42<num && num<49){
		if($("#open_"+n).attr('class').indexOf('more7')==-1){
			$("#open_"+n).addClass('more7');
		}else{
			$("#open_"+n).removeClass('more7');
		}
	}else if(48<num && num<55){
		if($("#open_"+n).attr('class').indexOf('more8')==-1){
			$("#open_"+n).addClass('more8');
		}else{
			$("#open_"+n).removeClass('more8');
		}
	}
}

function goodsFilter(obj,vtype){
	if(vtype==1){
		$('#brand').val($(obj).attr('v'));
	}else if(vtype==2){
		var price = $(obj).attr('v');
		price = price.split('_');
		$('#sprice').val(price[0]);
		$('#eprice').val(price[1]);
	}else if(vtype==3){
		$('#v_'+$(obj).attr('d')).val($(obj).attr('v'));
		var vs = $('#vs').val();
		vs = (vs!='')?vs.split(','):[];
		vs.push($(obj).attr('d'));
		$('#vs').val(vs.join(','));
	}
	var ipts = WST.getParams('.sipt');
	if(vtype==4){
		var name = $(obj).attr('v');
		if($('#'+name).val()==1){
			ipts[name]='0';
		}else{
			ipts[name]='1';
		}
		ipts['order']='1';
	}
	var params = [];
	for(var key in ipts){
		if(ipts[key]!='')params.push(key+"="+ipts[key]);
	}
	location.href=WST.U('home/goods/lists',params.join('&'));
}
function goodsOrder(orderby){
	if($('#orderBy').val()!=orderby){
		$('#order').val(1);
	}
	$('#orderBy').val(orderby);
	goodsFilter(null,0);
}
/*筛选条件下搜索商品名称*/
function fsGoods(){
	$('#goodsName').val($('#sGoodsName').val());
	goodsOrder();
}



function removeFilter(id){
	if(id!='price'){
		$('#'+id).val('');
		if(id.indexOf('v_')>-1){
			id = id.replace('v_','');
			var vs = $('#vs').val();
			vs = (vs!='')?vs.split(','):[];
			var nvs = [];
			for(var i=0;i<vs.length;i++){
				if(vs[i]!=id)nvs.push(vs[i]);
			}
			$('#vs').val(nvs.join(','));
		}
	}else{
		$('#sprice').val('');
		$('#eprice').val('');
	}
	var ipts = WST.getParams('.sipt');
	var params = [];
	for(var key in ipts){
		if(ipts[key]!='')params.push(key+"="+ipts[key]);
	}
	location.href=WST.U('home/goods/lists',params.join('&'));
}
/*搜索列表*/
function searchFilter(obj,vtype){
	if(vtype==1){
		$('#brand').val($(obj).attr('v'));
	}else if(vtype==2){
		var price = $(obj).attr('v');
		price = price.split('_');
		$('#sprice').val(price[0]);
		$('#eprice').val(price[1]);
	}else if(vtype==3){
		$('#v_'+$(obj).attr('d')).val($(obj).attr('v'));
		var vs = $('#vs').val();
		vs = (vs!='')?vs.split(','):[];
		vs.push($(obj).attr('d'));
		$('#vs').val(vs.join(','));
	}
	var ipts = WST.getParams('.sipt');
	if(vtype==4){
		var name = $(obj).attr('v');
		if($('#'+name).val()==1){
			ipts[name]='0';
		}else{
			ipts[name]='1';
		}
		ipts['order']='1';
	}
	var params = [];
	for(var key in ipts){
		if(ipts[key]!='')params.push(key+"="+ipts[key]);
	}
	location.href=WST.U('home/goods/search',params.join('&'));
}
function searchOrder(orderby){
	if($('#orderBy').val()!=orderby){
		$('#order').val(1);
	}
	$('#orderBy').val(orderby);
	searchFilter(null,0);
}

/*加入购物车*/
$('.goods').hover(function(){
	$(this).find('.sale-num').hide();
	$(this).find('.p-add-cart').show();
},function(){
	$(this).find('.sale-num').show();
	$(this).find('.p-add-cart').hide();
})

//商品滑动
function swiperGoods(n){
	var mySwiper = new Swiper ('.goods-'+n, {
		speed:600,
	    slidesPerView: 5,
	    slidesPerGroup : 5,
	    spaceBetween: 14,
	    //分页
	    pagination: '.swiper-pagination',
	    paginationClickable :true,
        onSlideChangeEnd: function(swiper){
        	$('.goodsImg-'+n).lazyload({failurelimit : 10,skip_invisible : false,threshold: 100,placeholder:window.conf.RESOURCE_PATH+'/'+window.conf.GOODS_LOGO});//商品默认图片
        }
	})
}

/* 商品筛选【多选】 */
var _preObj;
function multibox_show(obj,type){
	var _topParent = $(obj).parent().parent();
	// 显示被隐藏的选项
	_topParent.find('div.term').css({height:'auto'});
	// 给每个item绑定事件
	_topParent.find('div.term span').each(function(){
		$(this).attr('_onclick',$(this).attr('onclick'));
		$(this).removeAttr('onclick');
		$(this).click(function(){
			$(this).toggleClass('selected');
			($('span.selected').length>0)?_topParent.find('a.confirm_btn').css({visibility:'visible'}):_topParent.find('a.confirm_btn').css({visibility:'hidden'});
		})
	})
	// 隐藏右侧按钮
	$(obj).parent().hide();
	// 显示多选盒子
	_topParent.addClass('multi_on');
	_topParent.append('<div class="multi_btns"><a class="confirm_btn" href="javascript:void(0)" onClick="multi_done(this,\''+type+'\')">确定</a><a onClick="multibox_hide(this)" href="javascript:void(0)">取消</a></div>')
	// 隐藏上一个多选盒子
	if($('.multi_on').length>1){multibox_hide(_preObj)}
	_preObj = obj;
}
// 完成多选
function multi_done(obj,type){
	var ids = [],attrId=0;
	// 获取选中的值
	$(obj).parent().parent().find('span.selected').each(function(){
		ids.push($(this).attr('v'));
		if(type=='attr')attrId = $(this).attr('d');
	})
	if(ids.length==0){
		WST.msg('请选择要筛选的选项');
		return false;
	}
	//console.log('ids',ids.join(','));

	if(type=='brand'){
		$('#brand').val(ids.join(','));
	}else if(type=='attr'){
		$('#v_'+attrId).val(ids.join('、'));// 属性名称 -> 双卡双4G_电信4G_移动4G
		var vs = $('#vs').val();// 属性id【不需要再拼凑】
		vs = (vs!='')?vs.split(','):[];
		vs.push(attrId);
		$('#vs').val(vs.join(','));

	}
	var ipts = WST.getParams('.sipt');
	var params = [];
	for(var key in ipts){
		if(ipts[key]!='')params.push(key+"="+ipts[key]);
	}
	//console.log('params',params.join('&'));
	location.href=WST.U('home/goods/lists',params.join('&'),true);


}
// 多选隐藏
function multibox_hide(obj){
	var _topParent = $(obj).parent().parent();
	// 显示被隐藏的选项
	_topParent.find('div.term').removeAttr('style');
	// 给每个li绑定事件
	_topParent.find('div.term span').each(function(){
		$(this).unbind('click');
		$(this).attr('onclick',$(this).attr('_onclick'));
		$(this).removeAttr('_onclick');
		$(this).removeClass('selected');
	})
	// 显示右侧按钮
	_topParent.find('div.extra').show();
	// 隐藏多选盒子
	_topParent.removeClass('multi_on');
	_topParent.find('div.multi_btns').remove();
}
//筛选条件
function screenContrast(obj,id){
	if ($(obj).is(':checked')) {
		$(".identical_"+id).addClass('active');
	}else{
		$(".identical_"+id).removeClass('active');
	}
}
//筛选规格
function choiceContrast(obj,itemId,catId,goodsId){
	$(obj).addClass('active').siblings('.list-box li').removeClass('active');
	$("#defaultSpec_"+goodsId+"_"+catId).val(itemId);
	var specIds = [];
	$(".defaultSpec_"+goodsId).each(function(){
		specIds.push(parseInt($(this).val(),10));
	});
	specIds.sort(function(a,b){return a-b;});
	if(saleSpec.sku[goodsId][specIds.join(':')]){
		stock = saleSpec.sku[goodsId][specIds.join(':')].specStock;
		marketPrice = saleSpec.sku[goodsId][specIds.join(':')].marketPrice;
		goodsPrice = saleSpec.sku[goodsId][specIds.join(':')].specPrice;
	}
	$('#goods-price-'+goodsId).html('¥ <span>'+goodsPrice+'</span>');
}