brand.checkout.checkoutItemHandler = Class.create( { 
    _removeIsEnabled: true,
    _updateQtyIsEnabled: true, 
    selectQtyId: "",
    skuPath: "",

    initialize: function(/* Object */args) {
        //console.log("brand.checkout.checkoutItemHandler.init");
        this.selectQtyId = args.selectQty;
        this.skuPath = args.skuPath; 
    
        var btnRemoveNode = $(args.buttonRemove);
        var selectQtyNode = $(this.selectQtyId);
        if (btnRemoveNode) {
            btnRemoveNode.observe("click", this._remove.bind(this));
            this.progress = new generic.progressOverlay({
                containerId: args.itemContainer,
                progressId: args.progressIndicator,
                offset: args.progressOffset
            });
        }
        if (selectQtyNode) { 
           selectQtyNode.observe("change", this._updateQty.bind(this)); 
        } 
   },

    _remove: function() {
        // summary:
        //      On click of remove link:
        //      - set enabled state for this method to false, so user can't submit twice
        //      - replace button w/ loading message 
        if (!this._removeIsEnabled) return;
        console.log("brand.checkout.checkoutItemHandler._remove removing sku "+this.skuPath);
        
        var self = this;   
        this.progress.start();
        this._removeIsEnabled = false;  
        brand.checkout.canContinueCheckout(false);
        
        var sku = this.skuPath.split("SKU")[1]; 
        $("checkout_cart_sku").value = sku;
        $("checkout_cart_qty").value = 0;
        $("checkout_cart").submit(); 
    },  
    
    _updateQty: function() {
        // summary:
        //      On change of qty select:
        //      - set enabled state for this method to false, so user can't submit twice
        //      - replace drop down w/ loading message
        
        if (!this._updateQtyIsEnabled) return;
        console.log("brand.checkout.checkoutItemHandler._updateQty");
        
        var self = this;   
        this.progress.start();
        this._updateQtyIsEnabled = false;         
        brand.checkout.canContinueCheckout(false);
        
        var sku = this.skuPath.split("SKU")[1];        
        var qty = this._getQty();            
        $("checkout_cart_sku").value = sku;
        $("checkout_cart_qty").value = qty;
        $("checkout_cart").submit();
    },
    
    _getQty: function() { 
        console.log("brand.checkout.checkoutItemHandler._getQty"); 
        var selectQtyNode = $(this.selectQtyId);
        var qty = selectQtyNode.options[selectQtyNode.selectedIndex].value; 
        return qty;
    }
    
});