jQuery(function() {
	jQuery('.shipping_method_box .radio').click(function() {
		var name = jQuery(this).parent().find('.radio').val();
		var amount = jQuery('#shipping_' + name).val();
		var amount_styled = jQuery('#shipping_' + name + '_styled').val();
		var shipping_method = jQuery(this).attr('title');
		var shipping_method_id = jQuery('#shipping_' + name + '_id').val();
		
		jQuery('#shipping_price').val(amount);
		jQuery('#shipping_row .value').text(amount_styled);
		hideShipping();
		
		// jQuery('#select_shipping_method .value').html(': ' + shipping_method);
		jQuery('#shipping_row .label span').text(' (' + shipping_method + ')');
		jQuery('#shipping_id').val(shipping_method_id);
		jQuery('#shipping_row').effect('highlight', {}, 1000);
		calculateTotal();
		
		var paymentCurrentValue = jQuery('#payment_id').val();
		if(paymentCurrentValue == 0) {
			jQuery('#select_payment_method .button').fadeIn(500);
			jQuery('.payment_method_box').slideDown(500);
		}
	});
	
	jQuery('.payment_method_box .radio').click(function() {
		var amount = jQuery('#payment_' + jQuery(this).val()).val();
		var amount_styled = jQuery('#payment_' + jQuery(this).val() + '_styled').val();
		var payment_method = jQuery(this).attr('title');
		var payment_method_id = jQuery(this).val();
		
		jQuery('#payment_price').val(amount);
		jQuery('#payment_row .value').text(amount_styled);
		hidePayment();
		
		calculateTotal();
		
		// jQuery('#select_payment_method .value').html(': ' + payment_method);
		jQuery('#payment_row .label span').text(' (' + payment_method + ')');
		jQuery('#payment_id').val(payment_method_id);
		jQuery('#payment_row').effect('highlight', {}, 1000);
		// jQuery('#payment_row .label span').fadeIn(500);
		
		// show next button
		jQuery('#big_submit').fadeIn(500);
	});
	
	jQuery('#select_shipping_method .button').click(function() {
		jQuery('.shipping_method_box').fadeIn(500);
	});
	
	jQuery('#select_payment_method .button').click(function() {
		jQuery('.payment_method_box').fadeIn(500);
	});
	
	jQuery('.cart_product_amount').change(function() {
		jQuery('.cart-control').fadeOut(500);
		var productId = jQuery(this).parent().find('.product_id').val();
		var productAmount = jQuery(this).val();
		jQuery('.product_change_amount_id').val(productId);
		jQuery('.product_change_amount_value').val(productAmount);
		jQuery('#calc-form').submit();
	});
	
	jQuery('#gift_certificate_submit').click(function() {
		var giftCode = jQuery('#gift_certificate').val();
		jQuery.ajax({
			type: 'POST',
			cache: false,
			url: baseUrl + '/ajax-cart/check-gift',
			dataType: 'json',
			data: 'code=' + giftCode,
			success: function(gift) {
				if(gift.msg == 'nomatch') {
					alert('De ingevoerde cadeaubon code kon niet worden gevonden of is al gebruikt.');
				} else if(gift.msg == 'success') {
					jQuery('.gift_box').slideUp(500);
					jQuery('.gift_box_used').slideDown(500);
					
					jQuery('#gift_certificate_id').val(gift.id);
					jQuery('#gift_certificate_amount').val(gift.amount);
					jQuery('#gift_row .label span').text(gift.name + '(' + gift.code + ')');
					jQuery('#gift_row .value').text(gift.amount_styled);
					
					jQuery('#gift_row').fadeIn(500, function() {
						jQuery('#gift_row').effect('highlight', {}, 1000);
						calculateTotal();
					});
				}
			}
		});
	});
	
	jQuery('#discount_code_submit').click(function() {
		var discountCode = jQuery('#discount_code').val();
		var amount = jQuery('#subtotal').val();
		jQuery.ajax({
			type: 'POST',
			cache: false,
			url: baseUrl + '/ajax-cart/check-discount',
			dataType: 'json',
			data: 'code=' + discountCode + '&amount=' + amount,
			success: function(discount) {
				if(discount.msg == 'nomatch') {
					alert('De ingevoerde kortingscode kon niet worden gevonden of is al gebruikt.');
				} else {
					jQuery('.discount_box').slideUp(500);
					jQuery('.discount_box_used').slideDown(500);
					
					jQuery('#gift_certificate_id').val(discount.msg.id);
										
					jQuery('#discount_id').val(discount.id);
					jQuery('#discount_amount').val(discount.amount);
					
					if(discount.type == 'percentage') {
						jQuery('#discount_row .label span').text(discount.description + '(' + discount.code + ') ' + discount.percentage + '%');
					} else {
						jQuery('#discount_row .label span').text(discount.description + '(' + discount.code + ')');
					}
					
					jQuery('#discount_row .value').text(discount.amount_styled);

					jQuery('#discount_row').fadeIn(500, function() {
						jQuery('#discount_row').effect('highlight', {}, 1000);
						calculateTotal();
					});
				}
			}
		});
	});
	
	jQuery('#big_submit').click(function() {
		jQuery('#faction').val('next');
		jQuery('#calc-form').submit();
	});
});

function calculateTotal() {
	var subtotal = parseFloat(jQuery('#subtotal').val());
	var shipping_price = parseFloat(jQuery('#shipping_price').val());
	var payment_price = parseFloat(jQuery('#payment_price').val());
	var gift_certificate_amount = jQuery('#gift_certificate_amount').val();
	var discount_amount = jQuery('#discount_amount').val();
	var total = subtotal + shipping_price + payment_price - gift_certificate_amount - discount_amount;
	total = number_format(total, 2, ',', '.');
	jQuery('#total_price .value').text('€ ' + total);
	jQuery('#total_price .value').effect('highlight', {}, 1000);
}

function hideShipping() {
	jQuery('#select_shipping_method .button').fadeIn(500);
	jQuery('#shipping_row').fadeIn(500, function() {
		jQuery('.shipping_method_box').fadeOut(250, function() {
			if(jQuery('#payment_row .label span').text().length == 0) {
				jQuery('.payment_method_box').fadeIn(500);
			}
		});
	});
}

function hidePayment() {
	jQuery('#select_payment_method .button').fadeIn(500);
	jQuery('#payment_row').fadeIn(500, function() {
		jQuery('.payment_method_box').fadeOut(250, function() {
			jQuery('#total_price').fadeIn(500);
		});
	});
}
