
var Shop_Cart = {

  moduleUrl: "/shop/modules/cart.php",

  refresh: function(obj, data) {
    var itemQuantityObj = obj.find(".table_count .input_count");
    var itemCostObj = obj.find(".table_all_price .cost");

    var topBasketCostObj = $("#basket_price span");
    var orderBtnObj = $("#makeorder_btn, #makeorder_btn2");
    
    
    if (data.newQuantity == 0) {
      obj.find(".table_all_price span.in_cart").removeClass("active");
    } else if (data.newQuantity > 0) {
      obj.find(".table_all_price span.in_cart").addClass("active");
    }

    itemQuantityObj.val(data.newQuantity);
    itemCostObj.text(data.newCost);

    topBasketCostObj.text(data.totalCost);

    if (data.totalQuantity == 0) {
      orderBtnObj.attr("disabled", true);
    } else if (data.totalQuantity > 0) {
      orderBtnObj.attr("disabled", false);
    }
    
  },

  addItem: function(obj) {
    var id = obj.attr("id");

    $.post(Shop_Cart.moduleUrl, {
        action: "addItem",
        productId: id
      }, function(response) {
        var data = JSON.parse(response);

        Shop_Cart.refresh(obj, data);
      }
    );
  },

  removeItem: function(obj) {
    var id = obj.attr("id");

    $.post(Shop_Cart.moduleUrl, {
        action: "removeItem",
        productId: id
      }, function(response) {
        var data = JSON.parse(response);
        
        $("#cart tr#" + id).remove();
        RefreshCartColors();
        
        Shop_Cart.refresh(obj, data);
      }
    );
  },

  incItemQuantity: function(obj) {
    var id = obj.attr("id");

    $.post(Shop_Cart.moduleUrl, {
        action: "incItemQuantity",
        productId: id
      }, function(response) {
        var data = JSON.parse(response);

        Shop_Cart.refresh(obj, data);
      }
    );
  },

  decItemQuantity: function(obj) {
    var id = obj.attr("id");

    $.post(Shop_Cart.moduleUrl, {
        action: "decItemQuantity",
        productId: id
      }, function(response) {
        var data = JSON.parse(response);

        Shop_Cart.refresh(obj, data);

      }
    );
  },

  setItemQuantity: function(obj, quantity) {
    var id = obj.attr("id");

    $.post(Shop_Cart.moduleUrl, {
        action: "setItemQuantity",
        productId: id,
        quantity: quantity
      }, function(response) {
        var data = JSON.parse(response);

        Shop_Cart.refresh(obj, data);
      }
    );
  }
}
