$(document).ready(function() {
	
	// ADD TO BASKET
	$('a.basket').click(function() {
		var product_id = $(this).attr('name');
		if($('#' + product_id).length == 0) {	// if product doesn't already exist
			$.ajax({
				url: 'shopping_basket/query_database.php',
				type: 'POST',
				data: 'action=add_to_basket&product_id=' + product_id,
				success: function(result) {
					var user_country = $('#your_basket div.country_selector select[name=country_selector]').val();
					if($('#products p.empty').length != 0) {
						$('#products p.empty').slideUp(500, function() {
							$(this).remove();
							$(result).appendTo('#products').hide().slideDown(500);
							$('#your_basket div.country_selector').slideDown(500);
							if(user_country != 'empty') {
								recalculate_order_totals();
								$('#your_basket div.totals').slideDown(500);
							$('<a href=\"store_checkout.php\" class=\"checkout\"><span>Checkout</span></a>').appendTo('#your_basket div.submit').hide().slideDown(500);
							}
						});
					} else {
						$(result).appendTo('#products').hide().slideDown(500);
						if(user_country != 'empty') {
							recalculate_order_totals();
							$('#your_basket div.totals').slideDown(500);
						}
					}
				}
			});
		}
	});
	
});


// RECALCULATE ORDER TOTALS
function recalculate_order_totals() {
	$.ajax({
		url: 'shopping_basket/query_database.php',
		type: 'POST',
		data: 'action=recalculate_order_totals',
		success: function(result) {
			var result_arr = result.split(" ");
			$('#your_basket div.totals span.subtotal').text(result_arr[0]);
			$('#your_basket div.totals span.postage').text(result_arr[1]);
			$('#your_basket div.totals span.vat').text(result_arr[2]);
			$('#your_basket div.totals span.total').text(result_arr[3]);
		}
	});
}


// REMOVE FROM BASKET
function delete_product(product_id) {
	$.ajax({
		url: 'shopping_basket/query_database.php',
		type: 'POST',
		data: 'action=remove_from_basket&product_id=' + product_id,
		success: function(result) {
			$('#' + product_id).slideUp(500, function() {
				$(this).remove();
				recalculate_order_totals();
				if(result < 1) {
					$('<p class=\"empty\">Your basket is empty.</p>').appendTo('#products').hide().slideDown(500);
					$('#your_basket div.country_selector').slideUp(500);
					$('#your_basket div.totals').slideUp(500);
					$('#your_basket div.submit a.checkout').slideUp(500, function() {
						$(this).remove();
					});
				}
			});
		}
	});
}


// UPDATE QUANTITY
function update_quantity(product_id) {
	var new_quantity = $('#' + product_id + ' td.quantity input').val();
	
	if(new_quantity < 1) {
		delete_product(product_id);
	} else {
		$.ajax({
			url: 'shopping_basket/query_database.php',
			type: 'POST',
			data: 'action=update_quantity&product_id=' + product_id + '&quantity=' + new_quantity,
			success: function(result) {
				$('#' + product_id + ' table td.footer span').text('\u00A3' + result);	// \u00A3 = unicode reference for a pound sign
				recalculate_order_totals();
			}
		});
	}
}


// UPDATE COUNTRY
function update_country() {
	var new_country = $('select[name=country_selector]').val();
	$.ajax({
		url: 'shopping_basket/query_database.php',
		type: 'POST',
		data: 'action=update_country&country_id=' + new_country,
		success: function(result) {
			
			recalculate_order_totals();
			
			if(new_country == 'empty') {
				$('#your_basket div.country_selector span.message').css('font-weight', 'bold').css('color', 'red');
				$('#your_basket div.totals').slideUp(500);
				$('#your_basket div.submit a.checkout').slideUp(500, function() {
					$(this).remove();
				});
			} else if($('#your_basket div.submit a.checkout').length == 0) {
				$('#your_basket div.country_selector span.message').css('font-weight', 'normal').css('color', '#333');
				$('#your_basket div.totals').slideDown(500);
				$('<a href=\"store_checkout.php\" class=\"checkout\"><span>Checkout</span></a>').appendTo('#your_basket div.submit').hide().slideDown(500);
			}
		}
	});	
}
