var cart = {};

$(function() {
	init_cart();
	update_cart_display();

	$('#international-order-checkbox').click(function() {
		if($(this).attr('checked')) {
		    cart['international'] = true;
		} else {
		    cart['international'] = false;
		}

		update_cart_display();
	    });
    
	$('#poster-add-to-cart').click(function() {
		cart['poster_quantity'] = parseInt($('#poster-quantity-select').val());
		cart['extra_donation'] += parseInt($('#poster-additional-donation').val());

		scrollTo(0,0);
        
		update_cart_display();
	    });
    
	$('#button-add-to-cart').click(function() {
		cart['button_quantity'] = parseInt($('#button-quantity-select').val());
		cart['extra_donation'] += parseInt($('#button-additional-donation').val());

		scrollTo(0,0);
        
		update_cart_display();        
	    });

	$('#remove-poster-button').click(function() {
		cart['poster_quantity'] = 0;

		update_cart_display();        
	    });

	$('#remove-extra-donation-button').click(function() {
		cart['extra_donation'] = 0;

		update_cart_display();        
	    });

	// Set up select menu widgets
	$('select.narrow').selectmenu({width: 84, style: 'dropdown'});
	$('select.wide').selectmenu({width: 155, style: 'dropdown'});    
    
    });

// Initialize the cart object to a blank state.
function init_cart() {
    var keys = ['button_quantity', 'poster_quantity', 'extra_donation'];
    for(var i = 0; i < keys.length; i++) {
        if($.cookie('tww_haiti_cart_' + keys[i])) {
            cart[keys[i]] = parseInt($.cookie('tww_haiti_cart_' + keys[i]));
        } else {
            cart[keys[i]] = 0;
        }
    }
}

function calculate_button_shipping(quantity) {
    if(quantity <= 10) {
	    return 1;
    } else if(quantity < 15) {
	    return 1.5;
    } else {
	    return 2;
    }
    
    
}

function update_cart_display() {
    // update cookies
    var keys = ['button_quantity', 'poster_quantity', 'extra_donation'];
    for(var i = 0; i < keys.length; i++) {
        $.cookie('tww_haiti_cart_' + keys[i], cart[keys[i]]);
    }
    
    // update html quantities
    $('#poster-quantity').html(cart['poster_quantity']);
    $('#button-quantity').html(cart['button_quantity']);
    $('#extra-donation-amount').html('$' + cart['extra_donation'] + '.00');

    // reset paypal hidden form elements
    for(var i=1; i <= 3; i++) {
        $('#paypal-checkout-form input[name=item_name_' + i + ']').remove();
        $('#paypal-checkout-form input[name=amount_' + i + ']').remove();
        $('#paypal-checkout-form input[name=quantity_' + i + ']').remove();
    }

    var cart_item_count = 0;
    
    if(cart['button_quantity'] > 0) {
        $('#button-row').show();

        cart_item_count += 1;
        
        $('#paypal-checkout-form').append(
					  '<input type="hidden" name="item_name_' + cart_item_count + '" value="Tangible Worldwide 2010 Haiti Earthquake Relief Button" />' +
					  '<input type="hidden" name="amount_' + cart_item_count + '" value="3.00" />' +
					  '<input type="hidden" name="quantity_' + cart_item_count + '" value="' + cart['button_quantity'] + '" />'
					  );

	if(cart['international']) {
	    if(cart['poster_quantity'] == 0) {
		$('#paypal-checkout-form').append(
						  '<input type="hidden" name="shipping_' + cart_item_count + '" value="15" />' +
						  '<input type="hidden" name="shipping2_' + cart_item_count + '" value="0" />'

						  );		
	    }
	} else {
	    $('#paypal-checkout-form').append(
					      '<input type="hidden" name="shipping_' + cart_item_count + '" value="' + calculate_button_shipping(cart['button_quantity']) + '" />' +
					      '<input type="hidden" name="shipping2_' + cart_item_count + '" value="0" />'
					      
					      );
	}
        
    } else {
        $('#button-row').hide();        
    }

    if(cart['poster_quantity'] > 0) {
        $('#poster-row').show();
        cart_item_count += 1;
        
        $('#paypal-checkout-form').append(
					  '<input type="hidden" name="item_name_' + cart_item_count + '" value="Tangible Worldwide 2010 Haiti Earthquake Relief Poster" />' +
					  '<input type="hidden" name="amount_' + cart_item_count + '" value="15.00" />' +
					  '<input type="hidden" name="quantity_' + cart_item_count + '" value="' + cart['poster_quantity'] + '" />'
					  );
	if(cart['international']) {
	    $('#paypal-checkout-form').append(
					      '<input type="hidden" name="shipping_' + cart_item_count + '" value="25" />' +
					      '<input type="hidden" name="shipping2_' + cart_item_count + '" value="0" />');	    
	} else{
	    $('#paypal-checkout-form').append(
					      '<input type="hidden" name="shipping_' + cart_item_count + '" value="6" />' +
					      '<input type="hidden" name="shipping2_' + cart_item_count + '" value="0" />');
	}
    } else {
        $('#poster-row').hide();        
    }

    if(cart['extra_donation'] > 0) {
        $('#extra-donation-row').show();
        cart_item_count += 1;
        
        $('#paypal-checkout-form').append(
					  '<input type="hidden" name="item_name_' + cart_item_count + '" value="Tangible Worldwide 2010 Haiti Earthquake Relief Contribution" />' +
					  '<input type="hidden" name="amount_' + cart_item_count + '" value="' + cart['extra_donation'] +  '" />' +
					  '<input type="hidden" name="shipping_' + cart_item_count + '" value="0" />'	    
					  );
    } else {
        $('#extra-donation-row').hide();        
    }

    var total = (cart['poster_quantity'] * 15) + (cart['button_quantity'] * 3) + cart['extra_donation'];
    
    $('#cart-total').html('$' + total + '.00');

    var suffix = '';
    if(cart_item_count > 1 || cart_item_count == 0) {
        suffix = 's';
    }

    if(cart_item_count) {
	$('#h-cart-container').show();
	$('#h-cart-container').removeClass('empty');	
	$('#empty-cart-message').hide();
	$('#cart-total-row').show();
	$('#check-out-link').show();
	$('#international-order-info').show();
    } else {
	$('#h-cart-container').addClass('empty');
	$('#empty-cart-message').show();
	$('#cart-total-row').hide();
	$('#check-out-link').hide();
	$('#international-order-info').hide();
    }
    $('#cart-item-count').html(cart_item_count + ' item' + suffix);

}