Kwo.Cart = {

  "addPurchase": function(product_id, quantity) {
    var args = {"product_id": product_id};
    args["quantity"] = quantity === undefined ? 1 : quantity;
    Kwo.exec("/shop/cart.add_purchase", args, {"async": false});
    Kwo.Cart.updateWidget();
  },
  
  "confirmPurchase": function(msg) {
    if ("purchaseConfirm" in window) {
      window.purchaseConfirm.call(this, msg);
    }
    else {
      Kwo.warn(msg);
    }
  },

  "empty": function(arg) {
    Kwo.exec("/shop/cart.empty", null, {"async": false, "confirm": arg});
  },


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

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

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

  "compose": function() {
    Kwo.go("/shop/order.compose");
  },

  "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"});
  },

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

});