$('.basket_continue_shopping').livequery(function() {
	$(this).hover(
		function () {
			$(".basket_continue_links").show();
		},
		function() {
			$(".basket_continue_links").hide();
		}
		);
});

$(function() {
	$('.find_your_nearest_store_popup').click(function() {
		window.open('/google/store-locator/?window=1', "Store Finder", "scrollbars = auto , height = 700, width = 1000");
/*		$.get('/google/store-locator', { popup: 1 }, function(response) {
			$.colorbox(
				{
					width: 700,
					height: 500,
					html: response['html'],
					scrolling: true,
					onOpen:function() { $("body").css("overflow", "hidden"); },
					onClosed:function() { $("body").css("overflow", "auto"); }
				}
			);
		},
		'json');*/
		return false;
	});
});

$('.basket_shopping_info').livequery(function() {

	$(this).click(function(e) {
		e.preventDefault();

		$.get('/ajax/Ajax_Content/getSnippet', { snippet_name: $(this).attr('rel') }, function (response) {
			$.colorbox({width:"400px", height:"500px", html: response['snippet_content'], scrolling: true, onOpen:function() { $("body").css("overflow", "hidden"); }, onClosed:function() { $("body").css("overflow", "auto"); }});
			}, 'json');
		});

	$(this).tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		bodyHandler: function () {
			return AuroraSnippet.get($(this).attr('rev'));
		}, 
		fade: 250 
	});

});

/*
	* Change the country and we update the basket incase of VAT differences
	* We change the shipping options
	* We update all of the sub totals, VAT, order totals and discounts
*/

$('input[name=basket_choose_delivery_type]').livequery('click', function(e) {

	if ($(this).val() == 'store') {

			if ($('.basket_collect_info').length) {
				
				$('.basket_collect_info').hide();
			}

			$('.basket_choose_delivery_date').hide();
			$.get('/ajax/Ajax_Basket/deleteBasketDeliveryDate');

			$('.basket_shipping_options_for_home_delivery').hide();
			$('.basket_store_id').parent().show();
			$('.basket_delivery_option').parent().hide();
			$('.basket_ship_to_where').val('store');
			$('.basket_shipping_info').hide();
			$('.basket_selected_store_info').html($('.basket_store_id').find('option:selected').attr('rel'));

			/*
				* Collect+ Data
			*/

			$('.basket_collect_plus_selected_address').hide();
			$('.basket_collect_plus_remove').hide();


	} else if ($(this).val() == 'collect_plus') {

			$('.basket_collect_info').show();

			$('.basket_choose_delivery_date').hide();
			$.get('/ajax/Ajax_Basket/deleteBasketDeliveryDate');


			$('.basket_shipping_options_for_home_delivery').hide();
			$('.basket_store_id').parent().hide();
			$('.basket_delivery_option').parent().hide();
			$('.basket_ship_to_where').val('');
			$('.basket_shipping_info').show();
			$('.basket_selected_store_info').html('');

	} else {

			if ($('.basket_collect_info').length) {
				
				$('.basket_collect_info').hide();
			}


			$('.basket_choose_delivery_date').show();
			$('.basket_shipping_options_for_home_delivery').show();
			$('.basket_store_id').parent().hide();
			$('.basket_delivery_option').parent().show();
			$('.basket_ship_to_where').val('');
			$('.basket_shipping_info').show();
			$('.basket_selected_store_info').html('');

			/*
				* Collect+ Data
			*/
			$('.basket_collect_plus_selected_address').hide();
			$('.basket_collect_plus_remove').hide();

	}

	update_basket($(this));

});

$('.basket_country, .basket_delivery_option, .basket_store_id').livequery('change', function(e) {

	update_basket($(this));
});

$('.basket_delete_product, .basket_delete_coupon, .basket_apply_loyalty_points, .basket_remove_loyalty_points').livequery('click', function(e) {

	e.preventDefault();
	update_basket($(this));

});

$(function() {
	$('.basket_apply_loyalty_points').click(
		function() {
			alert('Your Loyalty Points have been successfully applied to this order.');
		}
	);
});



function update_basket ($this) {

	var params = { delivery_country: $('.basket_country').val(), delivery_option: $('.basket_delivery_option input[name=delivery_option]:checked').val() }

	toggle_basket_overlay();

	if ($this.hasClass('basket_delete_coupon')) {
		params['delete_coupon'] = $this.attr('rel');
		$('.basket_coupon').fadeOut();
	} else if ($this.hasClass('basket_apply_loyalty_points')) {
		params['apply_loyalty_points'] = $('.basket_loyalty_textfield').val();
	} else if ($this.hasClass('basket_remove_loyalty_points')) {
		params['remove_loyalty_points'] = true;
	} else if ($this.hasClass('basket_country')) {
		params['update_shipping_options'] = true;
	} else {
		params['delete_basket_item'] = $this.attr('rel');
	}

	if ($('.basket_store_id:visible').length) {
		params['store_id'] = $('.basket_store_id').val();
	}


	$.get('/ajax/Ajax_Basket/getBasketDetails', params, function (responseJson) {

			var show_free_delivery = responseJson.free_shipping_spend.replace(/[^\d]/, "");

		//	console.log(responseJson.order_subtotal.replace(/[^\d]/, ""));

			if (responseJson.order_subtotal.replace(/[^\d]/, "") == '0.00') {
				window.location = '/basket';
				return false;
			}

			if ($('.basket_delivery_option:visible').length) {

				if (!$this.hasClass('basket_delivery_option')) {
					update_basket_shipping(responseJson.delivery_options);
				}

				update_delivery_time(responseJson.delivery_time);
			}

			$('.basket_subtotal_value').html(responseJson.order_subtotal);
			$('.basket_total').html(responseJson.order_total);

			if(responseJson.discount.match(/(\£|\&pound\;)0\.00/)) {
				$('.basket_discount_value').parent().hide();
			} else {
				$('.basket_discount_value').parent().show();
			}

			$('.basket_discount_value').html(responseJson.discount);
			$('.basket_youonlyneed_price').html(responseJson.free_shipping_spend);
			update_basket_free_shipping(show_free_delivery, responseJson.free_shipping_spend_show);
			update_basket_items(responseJson.basket_items);
			toggle_basket_overlay();

			if ($this.hasClass('basket_remove_loyalty_points')) {
				$('.basket_loyaltypoints_container').fadeOut(function() {
						$('.basket_loyalty_amount_value').html('0');
						$('.basket_loyalty_applied').hide();
						$('.basket_loyalty_apply').show();
						$('.basket_loyalty_textfield').val(responseJson.available_loyalty_points);
						$('.basket_loyaltypoints_container').fadeIn();
				});

			} else if ($this.hasClass('basket_apply_loyalty_points')) {
				$('.basket_loyaltypoints_container').fadeOut(function() {
						$('.basket_loyalty_amount_value').html(responseJson.loyalty_points);
						$('.basket_loyalty_applied').show();
						$('.basket_loyalty_apply').hide();
						$('.basket_loyaltypoints_container').fadeIn();
				});
			}

			if (responseJson.total_cross_sells > 0) {

				$('.basket_cross_sell_container').show();
				$('.basket_cross_sell_container').html(responseJson.cross_sells);

			} else {

				$('.basket_cross_sell_container').hide();

			}




	}, 'json');

}

function update_basket_items (basket_items) {

	$('.basket_data').html(basket_items);

}

function update_basket_shipping (shipping_options) {
	current_shipping_option = $('#basket_delivery_option').val();
	var shippingOptionHtml = '';
//	first = false;

	for (var i in shipping_options) {
		shippingOptionHtml += '<option value="' + shipping_options[i].id + '">' + shipping_options[i].name + ' (' + shipping_options[i].price + ')</option>';
//		if (!first) {
//			update_delivery_time(shipping_options[i].id);
//			first = true;
//		}
	}

	$('.basket_delivery_option').html(shippingOptionHtml);
	$('#basket_delivery_option').val(current_shipping_option);
	$('#basket_delivery_option').change();
}

function update_delivery_time (shipping_time) {

	$('.basket_shipping_info').fadeOut('200', function() {
		$('.basket_shipping_info_time').html(shipping_time.time);
		$('.basket_shipping_info_estimate').html(shipping_time.estimate);
		$('.basket_shipping_info').fadeIn('200');
	});

}

function toggle_basket_overlay() {
	if ($('.basket_loading_on').length) {
		$('.basket_loading_on').removeClass('basket_loading_on').addClass('basket_loading_off');
		$('.basket_loading_graphic').hide();

	} else {

		$('.basket_loading_off').removeClass('basket_loading_off').addClass('basket_loading_on');
		$('.basket_loading_graphic').css('top', $('#basket_form').height());
		$('.basket_loading_graphic').css({top: $('#basket_form').height() / 2 +'px'});
		$('.basket_loading_graphic').show();

	}
}

function update_basket_free_shipping (show_free_delivery, show_free_shipping) {


	if (show_free_shipping) {
		$('.basket_freedelivery').show();

		if (show_free_delivery == '0.00' || show_free_delivery == '0') {

			$('.basket_freedelivery_qualify').show();
			$('.basket_freedelivery_extra_spend').hide();

		} else {

			$('.basket_freedelivery_qualify').hide();
			$('.basket_freedelivery_extra_spend').show();
		}

	} else {
		$('.basket_freedelivery').hide();
	}

}

