/* 
 * cart.js
 *
 * jQuery port of original PrototypeJS implementation
 */

CAYMANCHEM.cart = {
    messages: {
        error: "You have an error in one of the fields above.  Please correct this before updating your cart.",
        failed: 'Cart load failed',
        loading: 'Item loading\u2026',
        processing: "Your request is processing.",
        update: "You have changed the quantities of one or more items.  You must click update to save these changes.",
        dummy: true
    },

    /** Clear cart */
    clear: function(catalogNum){
        var inputsSelector = 'table#cart[data-itemid="'+ catalogNum + '"] input';
        $(inputsSelector).attr('value',0);
    },

    /* Event handler for quantity field change */
    change: function(input,table,cartUrl,cartFullUrl,cartEmptyUrl){
        var name = $(input).attr('name');
        var item = name.substring(4);

        //  Normalize value: positive integers only
        var val = parseInt($(input).val(),10);
        var value = isNaN(val) || val < 0 ? 0 : val;

        var original = parseInt($(input).attr('rel'),10);

        if( original !== value ) {
            var url = cartUrl;
            var data = {};
            data[name] = value;

            CAYMANCHEM.cart.get({
                url: url,
                data: data,
                context: input,
                success: function(cart,textStatus,xmlHttpRequest){
                    /*
                        Upon success, update the entire cart widget to reflect
                        current status of cart, not just the line we expected to
                        change. This will mitigate hopefully rare case where
                        user is simultaneously manipulating two tabs/windows
                        viewing same product page
                    */

                    $(table).find('tr.line').each(function(i,e) {
                        var item = $(e).attr('id').substring(4);
                        var quantity;
                        var formattedSubtotal;

                        if( !cart.isEmpty && typeof cart.cartItems[item] === 'object' ) {
                            quantity = cart.cartItems[item].quantity;
                            rel = quantity;
                            formattedSubtotal = cart.cartItems[item].formattedSubtotal;
                        } else {
                            quantity = '';
                            rel = 0;
                            formattedSubtotal = "$0.00";
                        }

                        $(e).find('input.quantity').attr('value',quantity).attr('rel',rel);
                        $(e).find('.formattedSubtotal').text(formattedSubtotal);
                    });

                    //  Cart total, across all sizes of all products
                    var formattedTotal = cart.isEmpty ? "$0.00" : cart.formattedTotal;
                    $(table).find('td.cartTotal').text(formattedTotal);

                    //  Finally and least critical, adjust the cart icon
                    $('#cartIcon').attr('src', cart.isEmpty ?
                        cartEmptyUrl :
                        cartFullUrl
                    );
                },
                dummy: true
            });
        }
    },

/** Retrieve cart for this session */
    get: function(options){
        $.ajax({
            url: options.url,
            context: options.context,
            data: options.data,
            dataType: 'json',
            success: options.success,
            error: function(xmlHttpRequest,textStatus,errorThrown){
                alert(textStatus);
            },
            complete: function(xmlHttpRequest,textStatus){
            }
        })
    },

    /** Adjust interface */
    adjust: function(){
    },

    dummy: true
}


