$(function(){
    $('input').each(function(){
        $(this).attr("readonly", "true");
    });
    spec_a();
})
function spec_a(){
     /*规格选择*/
    $('.buy-attribute dl').each(function(){
        var obj=$(this);
        obj.find('dd a').bind('click',function(){
            obj.find('dd a').removeClass('on');
            $(this).addClass('on');
        });
    });
    /*规格加减*/
    $('.specifications-btm .addSubtract').each(function(){
        var obj=$(this);
        obj.find('.reduce_n').click(function(){add_add(obj,0);});
        obj.find('.add_a').click(function(){add_add(obj,1);});
    });
    /*规格加入购物车*/
    $('.specifications-btm .join-buy').each(function(){
        var obj=$(this);
        obj.bind('click',function(){spec_add_cart(obj);});
    });
    //329
    $('.join-buy-num .computing_act').each(function(index, el) {
        var obj=$(this);
        obj.find('.reduce').click(function(){apec_add_add(obj,0);});
        obj.find('.add').click(function(){apec_add_add(obj,1);});
    });
    /*规格加入购物车*/
    $('.join-buy-num .no_nows').each(function(){
        var obj=$(this);
        obj.bind('click',function(){specs_add_cart(obj,0);});
    });
    /*规格立即购买*/
    $('.join-buy-num .buy_nows').each(function(){
        var obj=$(this);
        obj.bind('click',function(){specs_add_cart(obj,1);});
    });
}
/**
 * 加减号的隐藏显示
 * @param obj
 * @param is_add
 */
function add_add(obj,is_add){
    var cart_num=obj.find('input');
    var cart_reduce_n=obj.find('a.reduce_n');
    var num=cart_num.val();num=parseInt(num?num:0);if(num<=0)num=0;
    if(is_add==1){
        num=num+1;
        if(cart_reduce_n.hasClass('hide')){cart_num.removeClass('hide');cart_reduce_n.removeClass('hide');}
    }else{
        num=num-1;
        if(num<=0 && !cart_reduce_n.hasClass('hide')){num=0;cart_num.addClass('hide');cart_reduce_n.addClass('hide');}
    }
    cart_num.val(num);
}
function apec_add_add(obj,is_add){
    var cart_num=$('#product_amount');
    var cart_reduce_n=obj.find('reduce');
    var num=cart_num.val();num=parseInt(num?num:1);if(num<=0)num=1;
    if(is_add==1){
        num=num+1;
        if(cart_reduce_n.hasClass('hide')){cart_num.removeClass('hide');cart_reduce_n.removeClass('hide');}
    }else{
        num=num-1;
        num=parseInt(num?num:1);if(num<=0)num=1;
        if(num<=0 && !cart_reduce_n.hasClass('hide')){num=0;cart_num.addClass('hide');cart_reduce_n.addClass('hide');}
    }

    cart_num.val(num);
    $("#number").html(num);
}
/**
 * 规格加入购物车
 */
function spec_add_cart(obj){
    var addSubtract=obj.siblings('.addSubtract');
    var goods_id=$('#gid').val();
    var num=addSubtract.find('input').val();

	var spec_id = checkarrkey_str;
    var spec=checkarrkey_str;
	var attrs=new Array();
    attrs=spec.split("_");
    var names='';
    for(i=0;i<attrs.length;i++){
        var namea=$("#attr"+attrs[i]+"").html();
        var parent_name= $("#attr"+attrs[i]+"").parent().siblings("dt").html();
        var allname=parent_name+":"+namea;
        names+=allname+' ';
    }

    add_cart(goods_id,1,spec,spec_id,num,names);
}
function specs_add_cart(obj,k){
    var goods_id=$('#gid').val();
    var num=$('#product_amount').val();

    var spec_id = checkarrkey_str;
    var spec=checkarrkey_str;
    var attrs=new Array();
    attrs=spec.split("_");
    var names='';
    for(i=0;i<attrs.length;i++){
        var namea=$("#attr"+attrs[i]+"").html();
        var parent_name= $("#attr"+attrs[i]+"").parent().siblings("dt").html();
        var allname=parent_name+":"+namea;
        names+=allname+' ';
    }
    if(k==1){
        addcart(goods_id,1,spec,spec_id,num,names);
    }else{
        add_cart(goods_id,1,spec,spec_id,num,names);
    }
    
}
function body_css(){
	var body = $('body').css("position");
	if(body=='relative'){
		$('body').css("position","");
	}
}
/**
 * 加入购物车
 * @param goods_id
 * @param is_add
 * @param spec
 * @param num   加入的数量
 */
function add_cart(goods_id,is_add,spec,spec_id,num,names){
    spec=spec?spec:'';
    spec_id=spec_id?spec_id:0;
    num=num?num:1;
    var query = new Object;
    query.goods_id=goods_id;
    // query.shop_id=shop_id;
    query.is_add=is_add;
    query.spec=spec;
    query.num=num;
	query.name=names;
    $.ajax({
        url:APP + "Mobile/Takeout/add_cart",
        data:query,
        type:"post",
        success:function(data){
            if(data.status==0) {
                if(data.url) {
                    // $.showErr(data.info, 0, 'hc', function () {
                    //     window.location.href = data.url;
                    // });
                    showtoast(data.info);body_css();
                    setTimeout(function(){
                        window.location.href = data.url;
                    },1000)
                }else{
                    // $.showErr(data.info, 3, 'hc');
					showtoast(data.info);body_css();return false;
                }
            } else {
				claseattr();
                var cart_id=parseInt(data.cart_id);//购物车id
                var cart_num=parseInt(data.cart_num);//购物车返回的数量
                var goods_price=parseFloat(data.goods_price);
                if(spec) {
                    spec_cart(cart_id,goods_id,cart_num,query.name,spec_id,num,goods_price);
                }else{
                    if (is_list == 1) {list_cart(cart_id,goods_id, is_add,cart_num)}
                    else {cont_cart(cart_id,goods_id,cart_num)}
                }
            }
        }
    });
}
//立即购买
function addcart(goods_id,is_add,spec,spec_id,num,names){
    var url='';
    var arr=spec;
    var number=num;
    var act='';
    var names=names;
    var query = new Object;
    query.goods_id = goods_id;
    if(arr){
        query.goods_attr = arr;
    }else{
        showtoast('请选择商品规格');body_css();return false;
        query.goods_attr=0;
    }
    query.type=14;
    url=APP+"Mobile/Goods/buy_now";
    // query.money=price;
    query.name=names;
    query.number = number;
    query.act = act;
    // query.score = score;
    $.ajax({
        url:url,
        data:query,
        type:"get",
        success:function(data) {
            if(data.status==1){
                window.location.href=APP+"Mobile/Order/index/goods_id/"+data.goods_id+"/number/"+data.number+"/goods_attr/"+data.goods_attr+"/type/"+data.type;
            }else{
                showtoast(data.msg);body_css();return false;
            }
            
        }
    })
}
/**
 * 规格购物车变动
 * @param goods_id
 * @param num
 * @param spec
 * @param spec_id
 */
function spec_cart(cart_id,goods_id,num,spec,spec_id,num,goods_price){
    footer_cart(cart_id,goods_id,num,spec,spec_id,goods_price);
}
/**
 * 列表购物车变动
 * @param goods_id
 * @param is_add
 */
function list_cart(cart_id,goods_id,is_add,cart_num){
    add_add($('#cart_'+goods_id),is_add);
    footer_cart(cart_id,goods_id,cart_num);
}
/**
 * 规格底部购物车变动
 * @param goods_id
 * @param is_add
 */
function list_spec_cart(cart_id,goods_id,is_add){
	var num = 1;
    var query = new Object;
    query.cart_id=cart_id;
    query.goods_id=goods_id;
    // query.shop_id=shop_id;
    query.is_add=is_add;
    $.ajax({
        url:APP + "Mobile/Takeout/add_spec_cart",
        data:query,
        type:"post",
        success:function(data){
            if(data.status==0) {
                if(data.url) {
                    $.showErr(data.info, 0, 'hc', function () {
                        window.location.href = data.url;
                    });
                }else{
                    $.showErr(data.info, 0, 'hc');
                }
            } else {
				claseattr();
                var cart_id=parseInt(data.cart_id);
                if(cart_id) {
                    add_add($('#cart_'+cart_id),is_add);
					var num=$('#cart_'+cart_id).find('input').val();
					footer_cart(cart_id,goods_id,num);
                }else{
                    if (is_list == 1) {list_cart(cart_id,goods_id, is_add)}
                    else {cont_cart(cart_id,goods_id)}
                }
            }
        }
    })
}
/**
 * 内容购物车变动
 * @param goods_id
 * @param num
 */
function cont_cart(cart_id,goods_id,num){
    var cart_num=$('#cart_'+goods_id).find('input');
	if (num <= 0) {//如果购物车数量小于等于0，去掉加减号
		num=0;$('#cart_'+goods_id).remove();
		var cart_html = '<a href="javascript:;" id="add_cart" onclick="add_cart('+goods_id+',1)" class="f_r">加入购物车</a>';
		$('#market_price_'+goods_id).before(cart_html);
	}else if(cart_num.length<=0) {//如果购物车数量大于0，并且页面没有加减号
		$('#add_cart').remove();
		var cart_html = '<div class="addSubtract clearfix f_r" id="cart_'+goods_id+'">'
		+'<a href="javascript:;" onclick="add_cart('+goods_id+',0)" class="hc-iconfont reduce_n">&#xe671;</a>'
		+'<input type="text" value="'+num+'">'
		+'<a href="javascript:;" onclick="add_cart('+goods_id+',1)" class="hc-iconfont add_a">&#xe670;</a>'
		+'</div>';
		$('#market_price_'+goods_id).before(cart_html);
	}else{//如果购物车数量大于0，并且页面已有加减号
		cart_num.val(num);
	}
    footer_cart(cart_id,goods_id,num);
}
/**
 * 底部购物车变动
 * @param goods_id
 * @param num 购物车返回的数量
 */
function footer_cart(cart_id,goods_id,num,spec,spec_id,goods_price){
    var footer_cart=$('#buy_cart_'+cart_id);
    if(footer_cart.length>0){
        if(num==0){
            footer_cart.remove();
            if($('dl#shop-buy-item dd').length<=0){
                if($('#areaMasks').css('display')!='none'){$('#areaMasks').click();}
            }
        }else {
            footer_cart.find('div input').val(num);
            var unit_price=footer_cart.find('div input').attr('un');
            var total_price=parseFloat(unit_price)*num;
            footer_cart.find('span.wm-shop-buy-price').html('￥'+foramtmoney(total_price));
            footer_cart.find('div input').attr('to',total_price);
        }
    }else{
        var cart=$('#market_price_'+goods_id);
        var unit_price=cart.attr('v');
        var goods_name=cart.attr('n');
        if(goods_price){
            unit_price=goods_price;
        }
        if(spec){goods_name += '('+spec+')';}
        var total_price=parseFloat(unit_price)*num;
        var add_html='<dd id="buy_cart_'+cart_id+'">'
        +'<span class="wm-shop-buy-name">'+goods_name+'</span>'
        +'<span class="wm-shop-buy-price">￥'+foramtmoney(total_price)+'</span>'
        +'<div class="addSubtract clearfix" id="cart_'+cart_id+'">';
        if(spec) {
            add_html+='<a href="javascript:;" onclick="list_spec_cart(' + cart_id + ',' + goods_id + ',0)" class="hc-iconfont">&#xe671;</a>'
            + '<input type="text" to="' + total_price + '" un="' + unit_price + '" name="num" value="' + num + '">'
            + '<a href="javascript:;" onclick="list_spec_cart(' + cart_id + ',' + goods_id + ',1)" class="hc-iconfont add_a">&#xe670;</a>';
        }else{
            add_html+='<a href="javascript:;" onclick="add_cart('+goods_id+',0)" class="hc-iconfont">&#xe671;</a>'
            +'<input type="text" to="'+total_price+'" un="'+unit_price+'" name="num" value="'+num+'">'
            +'<a href="javascript:;" onclick="add_cart('+goods_id+',1)" class="hc-iconfont add_a">&#xe670;</a>';
        }
        add_html+='</div>'
        +'</dd>';
        $('dl#shop-buy-item').append(add_html);
    }
    cart_total_price();
}
/**
 * 计算购物车总价和总数，并且判断是否可以去结算(总价和起送价比较)
 */
function cart_total_price(){
    var total_count=total_price=cart_price=cart_minus_price=0;
    var start_price=foramtmoney($('#go_balance').attr('sp'));
    $('dl#shop-buy-item dd').each(function(){
        var unit_count=parseInt($(this).find('div input').val());
        var price=parseFloat($(this).find('div input').attr('to'));
        total_count+=unit_count;total_price+=price;
    });
    if(total_count>0){
		total_price=parseFloat(total_price);
        $('#cart_icon').before('<i class="wm-bottom-nums">'+total_count+'</i>');
    }else{
        $('#open_cart').removeAttr('onClick');
        $('#open_cart').find('.wm-bottom-nums').remove();
    }
    cart_price=total_price;var minus_html='';
    cart_price=foramtmoney(cart_price);
    if(total_price>0){
        if($('#open_cart span.total').length>0){
            $('#open_cart span.total').html('共<em>￥'+cart_price+'</em>');
        }else{
            $('#open_cart span.wm-bottom-icon').after('<span class="wm-bottom-text total">共<em>￥'+cart_price+'</em></span>');
        }
        if($('#open_cart span.sale').length>0){$('#open_cart span.sale').remove();}
        $('#open_cart span.total').after(minus_html);
    }else{
        if($('#open_cart span.wm-bottom-text').length>0){
            $('#open_cart span.wm-bottom-text').remove();
        }
    }
    if(parseFloat(total_price)!=0 && parseFloat(total_price)>=parseFloat(start_price)){
        $('#go_balance').attr('onClick','go_balance()').addClass('on').html('选好了');
    }else{
        $('#go_balance').removeAttr('onClick').removeClass('on').html(start_price+'元起送');
    }
}
/**
 * 去结算
 */
function go_balance(){
    var url=$('input[name="url"]').val();
    window.location.href = url;
}
/**
 * 关闭属性框
 */
function claseattr(){
	$('.specifications_shops').hide();
	$('.specifications').hide();
	$('.reveal-modal-bg').hide();
}
/**
 * 清空购物车
 */
function clear_cart(){
    var query = new Object;
    query.shop_id=shop_id;
    $.ajax({
        url:APP + "Mobile/Takeout/clear_cart",
        data:query,
        type:"post",
        success:function(data){
            if(data.status==0) {
                if(data.url) {
                    $.showErr(data.info, 0, 'hc', function () {
                        window.location.href = data.url;
                    });
                }else{
                    $.showErr(data.info, 0, 'hc');
                }
            } else {
                if(is_list==1){//列表页
                    $('dd.wm-shop-item-num').each(function(){
                        var item=$(this).find('div');
                        item.find('input').val(0);
                        var cart_reduce_n=item.find('a.reduce_n');
                        if(!cart_reduce_n.hasClass('hide')){item.find('input').addClass('hide');cart_reduce_n.addClass('hide');}
                    })
                }else if(is_list==2){//其他页

                }else{//内容页
                    var cont_item=$('div.wm-shop-cont-price');
                    if(cont_item.find('div.addSubtract').length>0){
                        cont_item.find('div.addSubtract').remove();
                        var goods=$('#goods_info');
                        var cart_html = '<a href="javascript:;" id="add_cart" onclick="add_cart('+goods.val()+',1)" class="f_r">加入购物车</a>';
                        $('#market_price_'+goods.val()).before(cart_html);
                    }
                }

                /*底部购物车清空*/
                $('dl#shop-buy-item dd').each(function(){
                    $(this).remove();
                })
                if($('#areaMasks').css('display')!='none'){$('#areaMasks').click();}

                var start_price=foramtmoney($('#go_balance').attr('sp'));
                $('#open_cart').removeAttr('onClick');
                $('#open_cart').find('.wm-bottom-nums').remove();
                if($('#open_cart span.wm-bottom-text').length>0){
                    $('#open_cart span.wm-bottom-text').remove();
                }
                if ($('#open_cart span.sale').length > 0) {
                    $('#open_cart span.sale').remove();
                }
                $('#go_balance').removeAttr('onClick').removeClass('on').html(start_price+'元起送');
                /*底部购物车清空*/
            }
        }
    })
}
function noshop(){
    /* $.showErr(data.info, 0, 'hc', function () {
                        window.location.href = data.url;
                    });
                }else{*/
                    // $.showErr(data.info, 3, 'hc');
	showtoast('请先定位您最近的门店!');
    window.location.href =APP+'mobile';
}