﻿cst.apply('store', {
    initPage: function() {
        cst.store.prepQuantityEls();
        cst.store.prepAddEls();
        cst.preppurchase.prepPurchaseQuote();
        cst.preppurchase.prepPurchaseInvoice();
    },
    prepQuantityEls: function () {
        var quantityEls = $('.product-quantity input');
        cst.makeInputNumericOnly(quantityEls);
        quantityEls.keyup(function () {
            return cst.store.quantityOnkeyup(this);
        });
        quantityEls.change(function () {
            return cst.store.quantityOnkeyup(this);
        });
        quantityEls.blur(function () {
            if ($(this).val() == '') {
                $(this).val(1);
            }
            return cst.store.quantityOnkeyup(this);
        });
        $.each(quantityEls, function (i, v) {
            cst.store.quantityOnkeyup(v);
        });
    },
    prepAddEls: function () {
        $('.product-price a').click(function () {
            var productId = $(this).attr('id').split('add-product-')[1];
            var product = cst.store.getProduct(productId);
            var quantity = $('#quantity-' + productId).val();
            $(this).attr('href', "/store/addproductstocart?productId=" + product.productId + '&quantity=' + quantity);
        });
    },
    getDiscountPercentage: function (p, quantity) {
        var percentage = 0;
        for (var i = 0; i < p.discounts.length; i++) {
            if (quantity >= p.discounts[i].quantity && p.discounts[i].percentage > percentage)  {
                percentage = p.discounts[i].percentage;
            }
        }
        return percentage;
    },
    getProduct: function (id) {
        return cst.store.getHelper(cst.store.productsJSON, 'productId', id);
    },
    getHelper: function (a, p, v) {
        for (var i = 0; i < a.length; i++) {
            if (a[i][p] == v) {
                return a[i];
            }
        }
    },
    getRelatedProduct: function (e) {
        return cst.store.getProduct(cst.store.getProduct(e).relatedProductId);
    },
    quantityOnkeyup: function (el) {
        var e = $(el).attr('id').split('quantity-')[1];
        var product = cst.store.getProduct(e);
        var quantityField = $('#quantity-' + e);
        if (quantityField.val() != product.quantity) {
            var priceField = $('#price-' + e);
            cst.store.updatePrice(quantityField, priceField, product);
        }
    },
    toggleDisplay: function () {
        var isPremierSupport = $('#premier-support').attr('checked');
        for(var i=0; i<cst.store.productsJSON.length; i++) {
            var item = cst.store.productsJSON[i];
            if (item.CategoryId == 1 && !item.IsPremierSupport) {
                var quantity = $('#quantity-' + item.productId);
                var product = isPremierSupport ? cst.store.getRelatedProduct(item.productId) : item;
                cst.store.updatePrice(quantity, $('#price-' + item.productId), product);
            }
        }
    },
    updatePrice: function (quantityField, priceField, currentProduct) {
        currentProduct.quantity = quantityField.val();
        var discountPercentage = cst.store.getDiscountPercentage(currentProduct, quantityField.val());
        var totalPrice = (discountPercentage > 0)
            ? (quantityField.val() * currentProduct.price) - (quantityField.val() * currentProduct.price * discountPercentage)
            : (quantityField.val() * currentProduct.price);
        var totalPriceDisplay = cst.usMoney(totalPrice);
        if (discountPercentage > 0) {
            priceField.html(totalPriceDisplay + ' <span class="volume-discount">(' + discountPercentage * 100 + '%)</span>');
        } else {
            priceField.html(totalPriceDisplay); 
        }
    }
});
