﻿/*****************************/
/*购物车，联合Cookies对象使用*/
/*****************************/
function Cart() { }
Cart.prototype = {
    //购物车模板
cartTemplate: "<div class=\"SelectedBd cs-clear {$SelectedBd-odd$}\"><p class=\"cs-fl ShopItem ShopItem2\"><a href=\"/g/{$id$}.aspx\"><strong>{$name$}</strong></a></p><p class=\"cs-fl ShopItem Dian-gray Dian-gray3\">{$comboName$}</p><p class=\"cs-fl ShopItem Dian-gray\">￥{$salePrice$}</p><p class=\"cs-fl ShopItem ShopAmount Dian-gray\"><span class=\"Shop-Sure\">{$quantity$}</span><span class=\"Shop-count cs-clear\"><input type=\"text\" value=\"{$quantity$}\" onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'').replace(/^[^1-9]/g, 1);\" onblur=\"if(this.value=='')this.value='1';cart.updateItem({$id$},{$comboId$},this.value);cart.buildCart();\" class=\"cs-fl Shop-count-ipt\"><em class=\"cs-fr Shop-count-a\"><a title=\"增加\" href=\"javascript:void(0);\" onclick=\"cart.updateItem({$id$},{$comboId$},parseInt($(this).parent().prev('input').val())+1);cart.buildCart();\"></a><a title=\"减少\" href=\"javascript:void(0);\" onclick=\"if(parseInt($(this).parent().prev('input').val())==1)return;cart.updateItem({$id$},{$comboId$},parseInt($(this).parent().prev('input').val())-1);cart.buildCart();\"></a></em></span></p><p class=\"cs-fl ShopItem Dian-gray\">￥{$totalSalePrice$}</p><p class=\"cs-fr ShopItem ShopItem1\"><a class=\"btn-simple btn-simple2\" href=\"javascript:cart.delItem({$id$},{$comboId$});cart.buildCart();\">删除</a></p></div>",

    items: [],         //购物车项组合

    itemsCount: 0,     //商品品种数量

    totalPrice: 0,     //商品原总价

    totalSalePrice: 0, //商品总价

    //初始化购物车
    initItem: function() {
        if ((new Cookies()).select("CART_ITEM") != "undefined") {
            var str = (new Cookies()).select("CART_ITEM");
            this.toObject(str); //对象化购物车项
        }
    },
    //添加购物车项
    addItem: function(id, quantity, number, name, comboId, comboName, price, salePrice, image) {
        var isExist = false;    //用于判断是否新增
        for (var j = 0; j < this.items.length; j++) {
            if (this.items[j].id == id && this.items[j].comboId == comboId) {
                isExist = true;
                this.items[j].quantity = parseInt(this.items[j].quantity) + parseInt(quantity);
                break;
            }
        }
        if (!isExist) {
            var item = new Item();
            item.id = id;
            item.number = number;
            item.quantity = quantity;
            item.name = name;
            item.comboId = comboId;
            item.comboName = comboName;
            item.price = price;
            item.salePrice = salePrice;
            item.image = image;
            this.items.push(item);
        }

        str = this.serializer(this.items);     //序列化items，然后写入cookies
        (new Cookies()).add("CART_ITEM", str, { expireHours: 336, path: "/" }); //加入Cookie，CART_ITEM，两星期后过期
    },

    //删除购物车项
    delItem: function(id, comboId) {
        if (confirm("确定删除？")) {
            var item;
            for (var i = 0; i < this.items.length; i++) {
                if (this.items[i].id == id && this.items[i].comboId == comboId) {
                    item = this.items[i];
                    this.items.splice(i, 1);
                    break;
                }
            }
            str = this.serializer(this.items);     //序列化items，然后写入cookies
            (new Cookies()).add("CART_ITEM", str, { expireHours: 336, path: "/" }); //加入Cookie，CART_ITEM
            return item;
        }
    },

    //修改购物车项
    updateItem: function(id, comboId, quantity) {
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].id == id && this.items[i].comboId == comboId) {
                this.items[i].quantity = quantity;
                break;
            }
        }

        str = this.serializer(this.items);     //序列化items，然后写入cookies
        (new Cookies()).add("CART_ITEM", str, { expireHours: 336, path: "/" }); //加入Cookie，CART_ITEM
    },

    //建造购物车
    buildCart: function() {
        var arr = this.items;
        var html = "";
        this.totalPrice = 0;     //计算商品原总额前先清零
        this.totalSalePrice = 0; //计算商品总额前先清零
        this.itemsCount = 0;     //计算商品品种数量前先清零
        for (var i = 0; i < arr.length; i++) {
            this.item = arr[i];
            var temp;
            temp = this.cartTemplate;
            temp = temp.replace(/\{\$id\$\}/g, this.item.id);
            temp = temp.replace(/\{\$name\$\}/g, this.item.name);
            temp = temp.replace(/\{\$comboName\$\}/g, this.item.comboName);
            temp = temp.replace(/\{\$comboId\$\}/g, this.item.comboId);
            temp = temp.replace(/\{\$salePrice\$\}/g, this.item.salePrice.toFixed(2));
            temp = temp.replace(/\{\$quantity\$\}/g, this.item.quantity);
            temp = temp.replace(/\{\$totalSalePrice\$\}/g, (this.item.salePrice * this.item.quantity).toFixed(2));
            if (i % 2 == 1)
                temp = temp.replace(/\{\$SelectedBd-odd\$\}/, "SelectedBd-odd");
            else
                temp = temp.replace(/\{\$SelectedBd-odd\$\}/, "");
            html += temp;

            //计算商品总原价
            this.totalPrice += this.item.price * this.item.quantity;
            //计算商品总价
            this.totalSalePrice += this.item.salePrice * this.item.quantity;
            //计算商品品种数量
            this.itemsCount += 1;

        }
        $("#shopCart").find(".SelectedCon").html(html);
        $("#totalPrice").html(this.totalPrice.toFixed(2));
        $("#totalSalePrice").html(this.totalSalePrice.toFixed(2));
        $("#savePrice").html((this.totalPrice - this.totalSalePrice).toFixed(2));
        $("#cartItems").html(this.items.length);
        this.hover();
    },

    hover: function() {
        $(".SelectedBd").hover(
		    function() {
		        $(this).addClass("SelectedBd-on");
		    }, function() {
		        $(this).removeClass("SelectedBd-on");
		    }
	    );
        $(".SelectedCon div:odd").addClass("SelectedBd-odd");
    },

    //清空购物车
    clearCart: function() {
        if (confirm("确定清空购物车？")) {
            (new Cookies()).del("CART_ITEM");
        }
    },

    //序列化items对象
    serializer: function(its) {
        var str = "";
        for (var i = 0; i < its.length; i++) {
            str += "{";
            str += "id:" + its[i].id + ",comboId:" + its[i].comboId + ",quantity:" + its[i].quantity + ",name:\"" + its[i].name + "\",comboName:\"" + its[i].comboName + "\",price:" + its[i].price + ",salePrice:" + its[i].salePrice + ",number:\"" + its[i].number + "\",image:\"" + its[i].image + "\"";
            str += "}|";
        }
        str = str.substring(0, str.lastIndexOf("|"));
        return str;
    },

    //对象化购物车项
    toObject: function(str) {
        if (str != "") {
            this.items = str.split("|");
            for (var i = 0; i < this.items.length; i++) {
                this.items[i] = eval("(" + this.items[i] + ")");
            }
        }
    }
}
//购物车单项
function Item() {
    this.id = 0;         //商品编号
    this.number = "";    //商品编号
    this.quantity = 0;   //购买数量
    this.name = "";      //商品名称
    this.comboId = 0;    //套餐编号 
    this.comboName = ""; //套餐名称
    this.price = 0;      //原价
    this.salePrice = 0;  //销售价格
    this.image = "";     //商品图片
}
var cart = new Cart();
