//  購物車
var ShopCar = function(init_dt) {}

// 初始化資料
ShopCar.prototype.init = function() {
	var shopcar = this;
	// 移除購物車暫存物件
	this.curr_rate = 1;
	this.delShopcarItem = null;
	this.delShopcarpageItem = null;
	
	// 新增商品至購物車
	$(".btn-cart").unbind("click").click(function(){
		if(fuid == ""){
			$(".btn-login").click();
			return ;
		}
		var paras=new Object();
		paras.pid = $(this).attr("pd");
		if($(this).attr("num") == "1")paras.n = $(".number").val();
		var jsonObj=new Object();
		jsonObj.action="103";
		jsonObj.paras=paras;
		shopcar.addShopCar(jsonObj,0);
		swal_(lang_txt.pleasewait,lang_txt.addtocared,1);
	});
};
ShopCar.prototype.changeRate = function(rate){
	this.curr_rate = rate;
}
ShopCar.prototype.getRate = function(){
	return this.curr_rate;
}

ShopCar.prototype.addShopCar = function(jsonObj,m){
	var shopcar = this;
	json = JSON.stringify(jsonObj);
	$.ajaxSetup({async:true});
	$.post(ajaxurl,
		{
			data:json
		},
		function(data){
			data = JSON.parse(data);
			switch(m) {
				case 0:
					if(data.errCode == "0"){
						swal_(lang_txt.addtocared,"",0);
				   		if($(".car_"+data["ncar"].cid).length == 0){
				   			$(".nav-cart ul").append('<li class="car_'+data["ncar"].cid+'">'+
		                            '<figure style="background-image: url(\'../upload_img/product_img/'+data["ncar"].picB+'\');"></figure>'+
		                            '<hgroup>'+
		                                '<h2>'+data["ncar"]["lang"][langcode].proname+'</h2>'+
		                                '<h3><span>QTY：<font class="car_qty">'+data["ncar"].number+'</font></span><span>$<font class="total_money">'+trimzero((data["ncar"].saleprice*1+data["ncar"].style_price*1)*data["ncar"].number,shopcar.curr_rate)+'</font></span></h3>'+
		                            '</hgroup>'+
		                            '<button class="nav-btn-del" pid="'+data["ncar"].pid+'">'+lang_txt.delete+'</button>'+
		                        '</li>');
				   		}else{
				   			$(".car_"+data["ncar"].id).find(".car_qty").html(data["ncar"].number);
				   			$(".car_"+data["ncar"].id).find(".total_money").html(trimzero((data["ncar"].saleprice*1+data["ncar"].style_price*1)*data["ncar"].number,shopcar.curr_rate));
				   		}
				   		$(".nav-cart-bottom").show();
				        $(".nav-cart-null").removeClass("active");
				        if($("html").hasClass("cart")){
				        	location.reload();
				        }else{
				        	shopcar.count_price();
				        }
					}else if(data.errCode == "3031"){
						swal_(lang_txt.fail,lang_txt.prostockno,2);
					}else{
						swal_(lang_txt.fail,lang_txt.checkproduct,2);
					}
					break;
			    case 1:
			    	if(data.errCode == "0"){
						swal_(lang_txt.success,lang_txt.productdel,0);
				   		$(".car_num").html(data.car_num);
				   		if(shopcar.delShopcarpageItem != null)shopcar.delShopcarpageItem.remove();
				   		if(shopcar.delShopcarItem != null)shopcar.delShopcarItem.remove();
				   		if($("html").hasClass("cart")){
				        	location.reload();
				        }else{
				        	shopcar.count_price();
				        }
					}else{
						swal_(lang_txt.fail,lang_txt.checkproduct,2);
    				}
			    	shopcar.delShopcarItem = null;
			    	shopcar.delShopcarpageItem = null;
					break;
				default:
					break;
			}				
		},"text"
	);
};

// reCount
ShopCar.prototype.count_price = function(){	
	var shopcar = this;
	var sum_item = 0;
	var sum_price = 0;
	$(".total_money").each(function(){
		sum_price += $(this).text().replace(/,/g, "")*1;
	});
	$(".car_qty").each(function(){
		sum_item += $(this).text().replace(/,/g, "")*1;
	});
	$(".sum_price").html(trimzero(sum_price));
	if($(".nav-btn-del").length == 0){
		$(".nav-cart-bottom").hide();
        $(".nav-cart-null").addClass("active");
	}else{
		$(".nav-btn-del,.cart-btn-del").unbind("click").click(function(){
			if(fuid == ""){
				$(".btn-login").click();
				return ;
			}
			var pid = $(this).attr("pid");
			shopcar.delShopcarItem = $(".nav-cart").find("button[pid="+pid+"]").closest("li");
			shopcar.delShopcarpageItem = $(".cart-list").find("button[pid="+pid+"]").closest(".cart-item");
			var paras=new Object();
			paras.pid = pid;
			var jsonObj=new Object();
			jsonObj.action="104";
			jsonObj.paras=paras;
			shopcar.addShopCar(jsonObj,1);
		});			
	}
	$(".car_num").html(sum_item);
};

ShopCar.prototype.rebulid = function(){
	this.init();
}