$(document).ready(function(){

	$(".continue_shopping").mouseover(function(element) {
		$("#continue_shopping_links").css({"top": (element.currentTarget.offsetTop+20)+'px', "left": (element.currentTarget.offsetLeft)+'px' });
		$("#continue_shopping_links").show();
	});
	
	$(".continue_shopping").mouseout(function() {
		$("#continue_shopping_links").hide();		
	});

	$('a.remove').livequery('click', function(e) {
		e.preventDefault();
		$(this).parent().parent().fadeOut(function() {
			$(this).remove();
		});

		$.get('?', { ajax: true, 'delete': $(this).attr('rel') }, function (responseJson) {
			$('.subtotal').html(responseJson[1]);

			$('#hidden_total_cost').val(responseJson[1]) 

			updateShippingOptions();

			$('#basket_data').html(responseJson[3]);

			if (responseJson[2] <= '0') {
				window.location = '/basket';
			}
		}, 'json');
	});

	$('a.update_basket').click(function(e) {
		e.preventDefault();

		$('#basket_form').submit();
	});
	
	$('#delivery_option').change(function() {
		updateDeliveryTime($('#delivery_option').val());
	});

	$('#crosssell_form').submit(function(e) {
		e.preventDefault();

		allFields = '';
		$('#crosssell_form input[type=checkbox]:checked').each(function(i) {
			allFields += $(this).val() + "&";
		});

		$.scrollTo($('#basket_form'));


		$.get('?', { ajax: true, crosssells: allFields }, function (responseJson) {
			$('#basket_data').fadeOut(function() {
				$('#basket_data').html(responseJson[0]);
				$('#basket_data').fadeIn();
			});

			$('.grandtotal').html(responseJson[1]);

		}, 'json');

	});

	$('#country').change(function() {
		updateShippingOptions();
	});
	
	$('#apply_loyalty_points').click(function(e) {
		e.preventDefault();
		useLoyaltyPoints();
	});

	$('#remove_loyalty_points').click(function(e) {
		e.preventDefault();
		removeLoyalty();
	});
		
	$('#remove_coupons').click(function(e) {
		e.preventDefault();
		removeCoupon();
	});		
});
	
function updateDeliveryTime (shippingMethod) {

	$.get ('?', { ajax: true, delivery_option: shippingMethod }, function (responseJson) {
			$('.shipping-info').fadeOut();
            
			$('#shipping_info_description').html(responseJson.name);
			$('#shipping_info_time').html(responseJson.time);
			$('#shipping_info_estimate').html(responseJson.estimate);
			$('.grandtotal').html(responseJson.totalCost);
			$('.discount').html(responseJson.discount);
			$('.shipping-info').fadeIn();

			}, 'json');
}

function updateShippingOptions ()
{
	var shippingCountry = $('#country').val();
	$.get('/basket', { ajax: true, shipping_country: shippingCountry, total_cost: $('#hidden_total_cost').val() }, function(responseJson)
    {
		var shippingOptionHtml = '';
		first = false;
		for (var i in responseJson)
        {
			shippingOptionHtml += '<option value="' + responseJson[i].id + '">' + responseJson[i].name + ' (' + responseJson[i].price + ')</option>';
			if (!first)
            {
			    updateDeliveryTime(responseJson[i].id);
    			first = true;
			}
		}
		$('#delivery_option').html(shippingOptionHtml);
	}, 'json');
}

function useLoyaltyPoints () {
	var loyalty_points = $('#loyalty_points').val()
	$.get('/basket', { ajax: true, apply_loyalty_points: loyalty_points }, function(responseJson) {
		$('#loyalty_display').fadeOut(function() {
			$('#loyalty_display .amount').html(responseJson[0]);
			$('#loyalty_applied').show();
			$('#loyalty_apply').hide();
			
			$('.discount').html(responseJson[1]);
			$('#total_discount').show();
			$('.grandtotal').html(responseJson[2]);
			$('#loyalty_display').fadeIn();
		});
	}, 'json');
}

function removeLoyalty() {
	$.get('/basket', { ajax: true, remove_loyalty_points: 1 }, function(responseJson) {
		$('#loyalty_display').fadeOut(function() {
			$('#loyalty_applied .amount').html('0');
			$('#loyalty_applied').hide();
			$('#loyalty_apply').show();
			
			$('.discount').html(responseJson[0]);
			if(responseJson[0].match(/(\£|\&pound\;)0\.00/))
				$('#total_discount').hide();
			else	
				$('#total_discount').show();

			$('.grandtotal').html(responseJson[1]);
			$('#loyalty_display').fadeIn();
		});
	}, 'json');
}

function removeCoupon() {
	$.get('/basket', { ajax: true, remove_coupons: 1 }, function(responseJson) {
		$('#coupons_applied').fadeOut()
			$('.discount').html(responseJson[0]);
			if(responseJson[0].match(/(\£|\&pound\;)0\.00/))
				$('#total_discount').hide();
			else	
				$('#total_discount').show();

			$('.coupon_notice').hide();
			$('.grandtotal').html(responseJson[1]);
	}, 'json');
	updateShippingOptions();
}
