﻿(function($) {
    $.extend({
        expires: 1, //cookie 保存时间
        path: '/', //cookie 保存path
        domain: '',  //domain  114study.com
        jsonCart: '[{"ProductId":"11","Total":1,"Type":"2","Discount":"100","ResourceType":"hardware"}]',
        tempCart: null,
       
        createCookie: function(newCart) {
            $.cookie("YuanBoV3_Property", newCart, { expires: $.expires, path: $.path, domain: $.domain, secure: false }); //,{expires: 7, path: '/', domain: '114study.com', secure: false}
        },
       
        AddProduct: function(nid,Type,Discount,ResourceType) {
        
            $.expires = $.expires == -1 ? 1 : $.expires;
          
            var cart = $.cookie("YuanBoV3_Property");
            if (typeof cart === 'object') {
                var objJson;
                if (typeof $.jsonCart === 'string') {
                    objJson = $.parseJson($.jsonCart);
                } else {
                    objJson = $.jsonCart;
                }
                $(objJson).each(function() {
                    $(this)[0].ProductId = String(nid);
                    $(this)[0].Total = 1;
                    $(this)[0].Type = String(Type);
                    $(this)[0].Discount = Discount;
                    $(this)[0].ResourceType = String(ResourceType);
                })

                this.createCookie($.toJsonString(objJson));
            } else {
                $.tempCart = $.parseJson(cart);
                var IsExist=false;
                
                  $($.tempCart).each(function(){
                     if($(this)[0].ResourceType==ResourceType&&$(this)[0].ProductId==nid){
                         $(this)[0].Total = parseInt($(this)[0].Total)+1;
                         IsExist=true;
                     }                  
                  });
                  
                 if(!IsExist){
                    var objJson = $.parseJson($.jsonCart);
                    $(objJson).each(function() {
                        $(this)[0].ProductId = String(nid);
                        $(this)[0].Total = 1;
                        $(this)[0].Type = String(Type);
                        $(this)[0].Discount = Discount;
                        $(this)[0].ResourceType = String(ResourceType);
                    })
                    var strJson = $.toJsonString(objJson);
                    $.tempCart = cart.substr(0, cart.length - 1) + strJson.replace('[', ',');
                 }
                if (typeof $.tempCart === 'object') {
                    this.createCookie($.toJsonString($.tempCart));
                } else {
                    this.createCookie($.tempCart);
                }
            }    
        },         
        UpdateProduct: function(nid,txtId,controlname,addordel) {
            var cart = $.cookie("YuanBoV3_Property");
            var objJson = $.parseJson(cart);
            $(objJson).each(function() {
                if ($(this)[0].ProductId == nid) {
                    $(this)[0].Total = parseInt($("#"+txtId).val())+addordel;
                    }
            })
            this.createCookie($.toJsonString(objJson));
             $.fn.GetProductList(controlname);
        },
        DeleteProduct: function(controlname,nid) { 
            var cart = $.cookie("YuanBoV3_Property");
            var objJson = $.parseJson(cart);
            var strTxt = "你确定要删除当前产品吗？";
            if (window.confirm(strTxt)) {
            $(objJson).each(function(i) { 
                if ($(this)[0].ProductId == nid) { 
                    objJson.splice(i, 1);
                }
            })
            if (typeof objJson === 'object') {
                $.createCookie($.toJsonString(objJson));
            } else {
                this.createCookie(objJson);
            }
           $.fn.GetProductList(controlname);
          }
        },
        ClearProduct: function() {
            
            $.expires = -1;
            this.createCookie(null);

            //            $.fn.GetProductList();
            //            $.fn.GetTotalPrice();
        }
    })

    $.fn.GetProductList = function(controlname) {  
        $.ajax({
            Type: "POST",
            url: "ajaxProduct.aspx?radon="+Math.random(),
            data: "Type=getProductList",
            success: function(msg) { 
              //  alert(controlname);
                $("#"+controlname).html(msg);
               // alert(msg);
//                $.fn.GetTotalPrice();
            }
        });
        

    }
    $.fn.GetTotalPrice = function() {
        var price = 0;
        var TotalPrice = 0;
        var fee = 0;
        $("#productList > li").each(function() {
            var orinPrice = $(this).find("s").html().replace("￥", "");
            var realPrice = $(this).find("samp[@name=price]").html().replace("￥", "");
            var count = $(this).find("input[@Type=text]").val();
            fee = $(this).find("samp[@name=fee]").html().replace("￥", "");
            price = parseFloat(price) + parseFloat(orinPrice) * parseInt(count);
            TotalPrice = parseFloat(TotalPrice) + parseFloat(realPrice) * parseInt(count);
        });
        $("#savePrice").html("￥" + (parseFloat(price) - parseFloat(TotalPrice)).toFixed(2));
        $("#TotalPrice").html("￥" + (parseFloat(TotalPrice) + parseFloat(fee)).toFixed(2));
    }
})(jQuery);

