var WSTHook_beforeStatCartMoney = [],WSTHook_beforeStatGoodsMoney = [];
$(document).ready(function(){
	WST.initFooter('cart');
	WST.imgAdapt('j-imgAdapt');
	
    statCartMoney();
    //选中商品
    $('.ui-icon-chooseg').click(function(){
        if( $(this).attr('class').indexOf('wst-active') == -1 ){
        	var checked = 1;
        	WST.changeIconStatus($(this), 1);//选中
        }else{
        	var checked = 0;
        	WST.changeIconStatus($(this), 2);//取消选中
        }
        var cid = $(this).attr('cartId');
        if(cid!=''){
		    WST.changeCartGoods(cid,$('#buyNum_'+cid).val(),checked);
		    statCartMoney();
	    }
    });
    //选中合计
    $('.ui-icon-choose').click(function(){
    	WST.changeIconStatus($(this), 1);
        var goodsCount = $('.ui-icon-chooseg').length;//商品个数
        if( $(this).attr('class').indexOf('wst-active') == -1 ){
        	//选中所有
            for(var i=0; i<goodsCount; i++){
            	WST.changeIconStatus($('.ui-icon-chooseg').eq(i), 2);
                var cid = $('.ui-icon-chooseg').eq(i).attr('cartId');
                WST.changeCartGoods(cid,$('#buyNum_'+cid).val(),0);
            }
        }else{
        	//取消选中所有
            for(var i=0; i<goodsCount; i++){
            	WST.changeIconStatus($('.ui-icon-chooseg').eq(i), 2, 'wst-active');
                var cid = $('.ui-icon-chooseg').eq(i).attr('cartId');
                WST.changeCartGoods(cid,$('#buyNum_'+cid).val(),1);
            }
        }
        statCartMoney();
    });
});
//合计
function statCartMoney(){
	var cartMoney = 0,goodsTotalPrice,id;
	$('.wst-active').each(function(){
		id = $(this).attr('cartId');
		if(WSTHook_beforeStatGoodsMoney.length>0){
            for(var i=0;i<WSTHook_beforeStatGoodsMoney.length;i++){
                delete window['callback_'+WSTHook_beforeStatGoodsMoney[i]];
                window[WSTHook_beforeStatGoodsMoney[i]](id); 
                if(window['callback_'+WSTHook_beforeStatGoodsMoney[i]]){
                    window['callback_'+WSTHook_beforeStatGoodsMoney[i]]();
                    return;
                 }
            }
        }
		goodsTotalPrice = parseFloat($(this).attr('mval'))*parseInt($('#buyNum_'+id).val());
		cartMoney = cartMoney + goodsTotalPrice;
	});
	for(var i=1; i<$('#totalshop').val(); i++){
		var shopMoney = 0,goodsTotalPrice2;
		$('.clist'+i).each(function(){
			id = $(this).attr('cartId');
			goodsTotalPrice2 = parseFloat($(this).attr('mval'))*parseInt($('#buyNum_'+id).val());
			shopMoney = shopMoney + goodsTotalPrice2;
		});
		$('#tprice_'+i).html('¥'+shopMoney.toFixed(2));
	 }
    var minusMoney = 0;
    //满就送减免
    if(WSTHook_beforeStatCartMoney.length>0){
        for(var i=0;i<WSTHook_beforeStatCartMoney.length;i++){
            delete window['callback_'+WSTHook_beforeStatCartMoney[i]];
            minusMoney = window[WSTHook_beforeStatCartMoney[i]](cartMoney); 
            if(window['callback_'+WSTHook_beforeStatCartMoney[i]]){
                window['callback_'+WSTHook_beforeStatCartMoney[i]]();
                return;
            }
            cartMoney = cartMoney - minusMoney;
            shopMoney = shopMoney - minusMoney;
        }
    }
	$('#totalMoney').html('¥'+cartMoney.toFixed(2));
	checkGoodsBuyStatus();
}
function checkGoodsBuyStatus(){
	var cartNum = 0,stockNum = 0,cartId = 0;
	$('.wst-active').each(function(){
		cartId = $(this).attr('cartId');
		cartNum = parseInt($('#buyNum_'+cartId).val(),10);
		stockNum = parseInt($('#buyNum_'+cartId).attr('data-max'),10);
		if(stockNum < 0 || stockNum < cartNum){
			if(stockNum < 0){
				msg = '库存不足';
			}else{
				msg = '购买量超过库存';
			}
			$('#noprompt'+cartId).show().html(msg);
			$(this).parent().parent().addClass('nogoods');
	        WST.changeIconStatus($(this), 2);//取消选中
			WST.changeCartGoods(cartId,$('#buyNum_'+cartId).val(),0);
			statCartMoney();
		}else{
			$('#noprompt'+cartId).hide().html('');
			$(this).parent().parent().removeClass('nogoods');
		}
	});
}
//编辑
function edit(type){
	if(type==0){
		WST.showHide('','#edit,#settlement,#total');
		WST.showHide(1,'#complete,#delete');
	}else{
		WST.showHide('','#complete,#delete');
		WST.showHide(1,'#edit,#settlement,#total');
	}
}
//删除
function deletes(){
    var goodsIds = '';
    var goodsIconCount = $('.ui-icon-chooseg').length;//商品个数
    for(var i=0; i<goodsIconCount; i++){
        if( $('.ui-icon-chooseg').eq(i).attr('class').indexOf('wst-active') != -1 ){
            goodsIds += $('.ui-icon-chooseg').eq(i).attr('cartId') + ',';
        }
    }
    if(goodsIds!=''){
    	WST.dialog('确定删除选中的商品吗？','del("'+goodsIds+'")');
    }else{
    	WST.msg('请选择要删除的商品','info');
    }
}
function del(goodsIds){
	$.post(WST.U('mobile/carts/delCart'),{id:goodsIds},function(data,textStatus){
	     var json = WST.toJson(data);
	     if(json.status==1){
			  WST.msg(json.msg,'success');
			  WST.dialogHide('prompt');
		      setTimeout(function(){
		    	  location.href = WST.U('mobile/carts/index');
		      },2000);
	     }else{
	    	 WST.msg(json.msg,'warn');
	     }
	});
}
//结算
function toSettlement(){
    var goodsIconCount = $('.ui-icon-chooseg').length;//商品个数
    var noGoodsSelected = true;
    for(var i=0; i<goodsIconCount; i++){
        if( $('.ui-icon-chooseg').eq(i).attr('class').indexOf('wst-active') != -1 ){
            noGoodsSelected = false;
        }
    }
    if(noGoodsSelected){
    	WST.msg('请勾选要结算的商品','info');
        return false;
    }
    location.href = WST.U('mobile/carts/settlement');
}
//导航
function inMore(){
	if($("#arrow").css("display")=='none'){
		$("#arrow").show();
		$("#layer").show();
	}else{
		$("#arrow").hide();
		$("#layer").hide();
	}
}