Kwo.Cart = {

  "addItem": function(product_id, quantity) {
    var args = {"product_id": product_id};
    args["quantity"] = (quantity === undefined) ? 1 : quantity;
    args["options"] = ($('options') === null) ? '' : $F('options');
    Kwo.exec("/shop/cart.add_item", args, {"async": false});
    Kwo.Cart.updateWidget();
  },

  "clearPurchases": function(args) {
    Kwo.exec("/shop/cart.empty", args, {"async": false});
  },

  "prepareOrder": function() {
    Kwo.go("/shop/cart.prepare_order");
  },
  
  "update": function(args) {
    Kwo.exec("/shop/cart.update", args, {"async": false});
  },
  
  "updateAmount": function() {
    Kwo.exec("/shop/cart.amount", 
             $("_order_form"), 
             {"container": "_order_amount_box", "signal": true});
  },
  
  "view": function() {
    Kwo.go("/shop/cart.view");
  },

  "updateWidget": function() {
    if ($("kwo-cart-box")) {
      Kwo.exec("/shop/cart.widget", null, {"container": "kwo-cart-box"});
    }
  }
  
};

Kwo.Order = {
  
  "create": function(args) {
    Kwo.exec("/shop/order.create", args);
  },
  
  "payment": function(id) {
    Kwo.go("/shop/order.payment", {"id": id});
  }
  
};

Kwo.Coupon = {
  
  "check": function(args) {
    Kwo.exec("/shop/coupon.check", args);
  }
  
};

Kwo.Couponpicker = Class.create(Kwo.Dialog, {

  "initialize": function($super, input) {
    this.input = input;
    $super(this.refresh, {}, {"height": 210});
    Kwo.setDialog(this);
  },
  
  "refresh": function(args) {
    Kwo.exec("/shop/dialog.couponpicker", args, {"container": "support"});
  },

  "setValue": function(code, id) {
    this.close();
    $("coupon-link-box", "coupon-code-box").invoke("toggle");
    $("coupon_id").setValue(id);
    $("coupon-code-box").update(code);
    Kwo.Cart.updateAmount();
  }

});