$(function() {
	$.ajaxSetup({
		cache: false,
		timeout: 10 * 1000,
		type: 'GET'
	});

	function showLoading() {
		$.facebox.loading();
	}
	function hideLoading() {
		$('#facebox .loading').remove();	
	}
	function updateBasket(html) {
		$('#miniBasket').html(html);
		updateCurrentDiscountGroup();
	}
	function updateCurrentDiscountGroup() {
		$.ajax({
			url: '/simple_shop/discount_groups/status',
			success: function(response) {
				$('#currentDiscountGroup').html(response);
			},
			error: function(request, error) {
				alert('Error: ' + error);
			}
		});		
	}
	
	$('.ajax a.addToBasket').click(function() {
		showLoading();
		$actions = $(this).parent('div').addClass('loading');
		$.ajax({
			url: this.href,
			success: function(response) {
				$actions.addClass('inBasket');
				updateBasket(response);
				$.facebox({ajax:'/simple_shop/basket/added_to_basket'});
				
			},
			error: function(request, error) {
				alert('Error: ' + error);
			},
			complete: function(request, status) {
				$actions.removeClass('loading');
			}
		});
		return false;
	});

	$('.ajax a.removeFromBasket').click(function() {
		showLoading();
		$actions = $(this).parent('div').addClass('loading');
		$.ajax({
			url: this.href,
			success: function(response) {
				$actions.removeClass('inBasket');
				updateBasket(response);
				$.facebox({ajax:'/simple_shop/basket/removed_from_basket'});
			},
			error: function(request, error) {
				alert('Error: ' + error);
			},
			complete: function(request, status) {
				$actions.removeClass('loading');
				$('#facebox #close').hide();
			}
		});
		return false;
	});
});