try {zipf = zipf || {};} catch(e){zipf = {};}

zipf.shop = {
    SERVICE_URL : '/shop/rpc',
    getRPC : function(callback) {
        var rpc = new ls.JsonRPC(this.SERVICE_URL);
        rpc.addEventListener(ls.JsonRPC.events.SUCCESS, callback);
        return rpc;
    }
};
zipf.shop.Cart = {
    events : {
        ITEM_ADDED : 'cartItemAdded',
        ITEM_REMOVED : 'cartItemRemoved',
        REFRESH : 'cartRefresh'
    },
    refresh : function() {
        this.dispatchEvent(new ls.AjaxEvent(this.events.REFRESH, {}));
    },
    itemAdded : function(response) {
        this.dispatchEvent(new ls.AjaxEvent(this.events.ITEM_ADDED, response));
    },
    itemRemoved : function(response) {
        this.dispatchEvent(new ls.AjaxEvent(this.events.ITEM_REMOVED, response));
    },
    add : function(id, amount, callback) {
        var rpc = zipf.shop.getRPC(function(response) {
            zipf.shop.Cart.itemAdded(response);
            zipf.shop.Cart.refresh();
        });
        rpc.call('addToCart', [id, amount]);
    },
    remove : function(id, amount) {
        var rpc = zipf.shop.getRPC(function(response) {
            zipf.shop.Cart.itemRemoved(response);
            zipf.shop.Cart.refresh();
        });
        rpc.call('removeFromCart', [id, amount]);
    }
};
makeDispatchable(zipf.shop.Cart);
