﻿
var AjaxTemplate = {
    createProductid: function (id1) {
        var id = id1; ShowMiniShopping
        var queryString = { productQueryId: id }; return queryString;
    },
    createProductMessage: function (proudctVariantId1, productMessage1, Quantity1) {
        var queryString = { productVariantIdAtrr: proudctVariantId1, productAttributeMessage: productMessage1, Quantity: Quantity1 }; return queryString;
    },
    createQueryString: function (pvId) {
        pvId = encodeURIComponent(pvId); var queryString = { productVariantId: pvId }; return queryString;
    },
    createMessage: function () {
        var id1 = "", id2 = "", id3 = ""; var queryString = { productQueryId: id1, productVariantId: id2, productId: id3 }; return queryString;
    },
    showShoppingCart: function (data, actionDeal) {
        var dataProduct = decodeURIComponent(data).toString();
        dataProduct = dataProduct + "";
        var strItem = new Array(); var str = new Array(); strItem = dataProduct.split("<!>");
        if (strItem[0] == "error") alert(strItem[1]);
        else {
            $(".itemsClmn").empty(); var num, allMoney = 0, allCount = 0;
            for (var i = 0; i < strItem.length - 1; i++) {
                str = strItem[i].toString().split("|"); allMoney = allMoney + parseInt(str[3].toString()) * parseFloat(str[2].toString()); allCount = allCount + parseInt(str[3]);
                var send = "<div class='item false'><a href='" + str[5] + "'><img alt='' class='thumb' width='90' height='115' src='" + str[1] + "' /></a> <div class='detail'> <p class='price'><span class='price'> " + str[6] + str[2] + "</span></p><p class='title'>" + str[0] + "</p><p class='quantity'>Qty " + str[3] + "</p><p><span> " + str[7] + "</span></p></div></div> ";
                send = send + "<p><a onclick='AjaxTemplate.delProduct(" + str[4] + ")' class='replace remove'  href='#'><span></span></a></p>";
                $(".itemsClmn").append(send);
            }
            num = strItem.length - 1;
            var ws = Math.pow(10, 2); allMoney = Math.round(allMoney * ws) / ws;
            if (allMoney != 0) { $("#MiniShoppingShow").html(ShowMiniShopping(allCount, str[6] + String(allMoney))); }
            else { $("#MiniShoppingShow").html(ShowMiniShopping(allCount, "")); }
            $("#totalMoney").html(str[6] + allMoney);

            if (num == 0) { $('#miniBasket .full').hide(); $('#miniBasket .empty').show(); }
            else { $('#miniBasket .empty').hide(); $('#miniBasket .full').show(); }

            if (num == 1) { $('#miniBasket .full.body .items').css({ "height": "150px" }); }
            if (num == 2) { $('#miniBasket .full.body .items').css({ "height": "280px" }); }
            if (num == 3) { $('#miniBasket .full.body .items').css({ "height": "411px" }); }

            mini_basket_holder.doPageButtonVisibilities();
            if (actionDeal == 1) {
                var pageSign;
                if (num < 3) pageSign = 1;
                else {
                    if (num % 3 == 0) { pageSign = num / 3; }
                    else { pageSign = parseInt(num / 3) + 1; }
                }

                var countTrans = pageSign - mini_basket_holder.currPage; mini_basket_holder.currPage = pageSign;

                var currentTop = parseInt($('#miniBasket .itemsClmn').css('top'), 10);
                if (isNaN(currentTop)) { currentTop = 0; }
                var newTop = currentTop - mini_basket_holder.itemContainerHeight * parseInt(countTrans);

                $('#miniBasket .itemsClmn').animate({ top: newTop + 'px' }, 500, function () { self.paging = false; });
                mini_basket_holder.doPageButtonVisibilities();
            }
            else {
                if (actionDeal == 2) {
                    if (num % 3 == 0 && mini_basket_holder.currPage != 1) {
                        var currentTop = parseInt($('#miniBasket .itemsClmn').css("top"), 10); mini_basket_holder.currPage--;
                        if (isNaN(currentTop)) { currentTop = 0; }
                        var newTop = currentTop + mini_basket_holder.itemContainerHeight;
                        $('#miniBasket .itemsClmn').animate({ top: newTop + 'px' }, 500, function () { self.paging = false; });
                        mini_basket_holder.doPageButtonVisibilities();
                    }
                }
            }
        }


    },

    InitializationCart: function () {
        jQuery.ajax({
            url: "/Ps/AjxPs/CartGet.aspx?" + new Date().getTime(), type: "Get", data: this.createMessage(),
            success: function (data, statue) { AjaxTemplate.showShoppingCart(data, 0); }
        });
    },

    delProduct: function (id) {
        var productVariantId = id;
        jQuery.ajax({
            url: "/Ps/AjxPs/CartGet.aspx?" + new Date().getTime(), type: "Get", data: this.createProductid(productVariantId),
            success: function (data, statue) { AjaxTemplate.showShoppingCart(data, 2); }
        });
    },

    getProductMessage: function (proudctVariantId, attributeMessage, Quantity) {
        jQuery.ajax({
            url: "/Ps/AjxPs/CartGet.aspx?" + new Date().getTime(), type: "Get", data: this.createProductMessage(proudctVariantId, attributeMessage, Quantity),
            success: function (data, statue) { AjaxTemplate.showShoppingCart(data, 1); mini_basket_holder.maximizeBasket(true); }
        });

    },
    getPageContent: function (proudctVariantId) {
        jQuery.ajax({ url: "/Ps/AjxPs/CartGet.aspx?" + new Date().getTime(), type: "Get", data: this.createQueryString(proudctVariantId),
            success: function (data, statue) { AjaxTemplate.showShoppingCart(data, 0); mini_basket_holder.maximizeBasket(true); }
        });
    }
};


