$(document).ready(function() {

	$('#loading').hide();
	setDeliveryAddressVisibility() 

	$('#custom_deliveryAddress').click(function() {
		setDeliveryAddressVisibility();
	});
	

	$(':input[id^=quantity-]').change(function() {
		$('#loading').show();
		var q = $(this).attr('id').split('-');
		var cartItemId = q[1];
		var quantity = $(this).val();

		$.getJSON(baseUrl + "shoppingcart/ajax-update-quantity/",
			{
				cartItemId: cartItemId,
				quantity: quantity
			}, function(json) { 
				onQuantityUpdated(cartItemId,json);
			}
		);
	});

	$(':radio[name=payment]').click(function() {
		var id = $(this).val();
		$.getJSON(baseUrl + 'shoppingcart/ajax-payment-transfer/',
			{paymentId: id}, 
			function(json) {
				$('span#description').html(json.paymentText);
			}
		);
	});

});


function setDeliveryAddressVisibility() 
{
	if($('#custom_deliveryAddress').attr('checked') != true) {
		$('#deliveryAddressDiv').hide();	
	} else {
		$('#deliveryAddressDiv').show();	
	}
}


function onQuantityUpdated(cartItemId,json)
{
	$('#totalPrice').text(number_format(json.totalPrice,0,'.',' ')+',- Kč');
	$('#price-'+cartItemId).text(number_format(json.price,0,'.',' ')+',- Kč');

	$('#loading').hide();
}
