//JQUERY DOM ready
$(document).ready(function() {

	/*------------------------------------------------------
	# Copy billing address to delivery address form
	------------------------------------------------------*/
	$('input#SameforDelivery').click(function() {

		if ($('input#SameforDelivery').is(':checked')){
			
			$('#DeliveryTitle').val($('#BillingTitle').val());
			$('#DeliveryFirstname').val($('#BillingFirstname').val());
			$('#DeliverySurname').val($('#BillingSurname').val());
			$('#DeliveryAddress1').val($('#BillingAddress1').val());
			$('#DeliveryAddress2').val($('#BillingAddress2').val());
			$('#DeliveryCity').val($('#BillingCity').val());
			$('#DeliveryPostcode').val($('#BillingPostcode').val());
			$('#SameforDelivery').closest('.carttable-row').fadeOut('slow');

		}
	
	});
	
	/*------------------------------------------------------
	# Change shipping costs
	#  - element1 = dropdown that user interacts with
	#  - element2 = similar dropdown that needs auto selecting
	------------------------------------------------------*/
	function updateShipping(element1,element2) {
	
		strCountry = $(element1).val();
		strTotal = $("#amount").val();
		strWeight = $("#weight").val();
		
		$(element2).val(strCountry);

		$('.feedback').append('<img src="/assets/images/loader.gif" />');
			
		$.post("/index.php/store/updateshippingbycountry/", { country: strCountry, total: strTotal, weight: strWeight },
			function(data){
				$('#shipping_select').html(data.shipping_select); //dropdown options
				$('#shipping_method').val(data.shipping_method); //selected option description
				$('#shipping').val(data.shipping_encrypted);
				$('#youPay').html('To pay: ' + data.total_topay);
				$('#myTotal').html(data.total_topay);
				$('#myVAT').html(data.vat);
				$('#vat').val(data.vat_encrypted);

				$('.feedback').empty();
								
		},'json');
	}
	
	//Assign the above function
	$('#DeliveryCountry').change(function() {
		updateShipping('#DeliveryCountry option:selected','#ShippingCalc');
	});

	$('#ShippingCalc').change(function() {
		updateShipping('#ShippingCalc option:selected','#DeliveryCountry');
	});


	/*------------------------------------------------------
	# Update total based on chosen shipping option
	------------------------------------------------------*/
	$('#shipping_select').change(function() {
		
		strShippingValue = $('#shipping_select option:selected').val();
		strTotal = $("#amount").val();
		strShippingMethod = $('#shipping_select option:selected').text();

		$('.feedback').append('<img src="/assets/images/loader.gif" />');
			
		$.post("/index.php/store/updatetotalbyshipping/", { shipping_value: strShippingValue, total: strTotal, shipping_method: strShippingMethod },
			function(data){
				$('#shipping_select').html(data.shipping_select); //dropdown options
				$('#shipping_method').val(data.shipping_method); //selected option description
				$('#shipping').val(data.shipping_encrypted);
				$('#youPay').html('To pay: ' + data.total_topay);
				$('#myTotal').html(data.total_topay);
				$('#myVAT').html(data.vat);
				$('#vat').val(data.vat_encrypted);
				
				$('.feedback').empty();
				
		},'json');

	
	});



	/*------------------------------------------------------
	# Product Gallery
	------------------------------------------------------*/
	$('.galleryThumb').click(function(event) {
		
		event.preventDefault();

		//console.log($(this).attr('href'));
		
		var link = $(this).attr('href');

		//$('.productImage').addClass('loader');
		$('#mainPhoto').fadeOut('slow');
		$('#mainPhoto').hide();
		
		$('#mainPhoto').attr('src',link).ready(function(){
			$('#mainPhotoLink').attr('href',link+'/500/500');
			$('#mainPhoto').fadeIn('fast');
		});
			
	});

	$('#mainPhotoLink').lightBox();


	/*------------------------------------------------------
	# Checkout Form Validation
	------------------------------------------------------*/
	jQuery.validator.setDefaults({ 
	    messages: {
	    		
	    		BillingFirstname: '',
	    		BillingSurname: '',
	    		BillingAddress1: '',
	    		BillingCity: '',
	    		BillingPostcode: '',
	    		Email: '',
	    		Phone: '',
	    		DeliveryFirstname: '',
	    		DeliverySurname: '',
	    		DeliveryAddress1: '',
	    		DeliveryCity: '',
	    		DeliveryPostcode: ''
	    	} 
	});

	$('#formCheckout').validate();

	/*------------------------------------------------------
	# Search box
	------------------------------------------------------*/
	$('#q').focus(function(){
		$(this).val('');
	});

	$('#q').blur(function(){
		if ($(this).val() == '') {
			$(this).val('What are you looking for?');
		}
	});


	/*------------------------------------------------------
	# Basket: highlight the update button when quantity is
	# changed.
	------------------------------------------------------*/
	$('.basket-qty').change(function() {
		$('#btnUpdateBasket').click();
	});

});
