var CART = function() {
  this.myCart = 0;
  return {
  
    initialize: function(sCartID, data) {
      CART.myCart = new clsCart({ container: $(sCartID) }, data);
    },
    
    itemAdd: function(post_id, index) {
      CART.myCart.show();
      CART.reqAction(post_id, 'add', index);
    },
    
    itemRemove: function(post_id, index) {
      CART.reqAction(post_id, 'remove', index);
    },
    
    clear: function() {
      CART.reqAction(0, 'clear');
    },
    
    reqAction: function(post_id, action, index) {
      CART.myCart.control.title.load();
      new Ajax.Request('/global/web/execute/partsearch.v2/cart.execute.php', {
        method: 'get',
        parameters: $H({post_id: post_id, action: action, index: index }),
        onSuccess: function(transport) {
          if (action == 'add') {
            CART.myCart.addData(transport.responseJSON);
          } else {
            CART.myCart.data = transport.responseJSON;
            CART.myCart.setData();
          }
        },
        onComplete: function() {
          CART.myCart.control.title.done();
          if (action == 'remove' || action == 'clear') window.location.reload();
        }
      });
    }
    
  }
}();

