/* jquery.spinner v1.0 copyright (c) 2014 by:loyaoo taobao:http://isseven.taobao.com */
(function($) {
$.fn.spinner = function (opts) {
var defaults = {value:1, min:1, len:3, max:1000000}
var options = $.extend(defaults, opts)
var keycodes = {up:38, down:40}
return this.each(function() {
var a = $(''); f(a,0,"decrease","-"); //加
var c = $(''); f(c,0,"increase","+"); //减
var b = $('');f(b,1,"amount"); //值
$(this).append(a).append(b).append(c);
a.click(function() {
var parent = $(this).closest(".ordnum");
var id=$(this).parents(".ord_ul").attr('data-id');
cv(-1,id,parent);
settotal(b.val(),parent);
shopcart.countamount();
});
b.keyup(function() {
var parent = $(this).closest(".ordnum");
var id=$(this).parents(".ord_ul").attr('data-id');
cv(0,id,parent);
settotal(b.val(),parent);
shopcart.countamount();
});
c.click(function() {
var parent = $(this).closest(".ordnum");
var id=$(this).parents(".ord_ul").attr('data-id');
cv(+1,id,parent);
settotal(b.val(),parent);
shopcart.countamount();
});
b.bind('keyup paste change',function(e){
e.keycode==keycodes.up&&cv(+1);
e.keycode==keycodes.down&&cv(-1);
});
function cv(n,id,parent){
b.val(b.val().replace(/[^\d]/g,''));
bv=parseint(b.val()||options.min)+n;
bv>=options.min&&bv<=options.max&&b.val(bv);
$(parent).find('.overtip').remove();
$.post("/index.php?s=/cart/checknum",{'num':b.val(),'id':id},function(data){
if(data.status)
{
layer.msg('商品数量不能大于'+data.stock);
$(parent).find('.amount').val(data.stock);
settotal(b.val(),parent);
shopcart.countamount();
//alert(newnum);
//$('#ordernum').text(newnum);
}
},'json');
if(bv<=options.min){b.val(options.min);f(a,2,"disde","decrease");}else{f(a,2,"decrease","disde");}
if(bv>=options.max){b.val(options.max);f(c,2,"disin","increase");}else{f(c,2,"increase","disin");}
}
});
function f(o,t,c,s){
t==0&&o.addclass(c).attr("href","javascript:void(0)").append("").find("i").append(s);
t==1&&o.addclass(c).attr({"value":options.value,"autocomplete":"off","maxlength":options.len});
t==2&&o.addclass(c).removeclass(s);
}
//计算单行的价格
function settotal(n,par){
var s=0;
var price = par.prev(".ordprice").find(".price2 em");
s = s + parseint(n)*parsefloat(price.text());
var total =par.nextall(".ordtotal").find(".ordspan");
total.html(math.round(s*100)/100);
}
}
})(jquery);