//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());
			$('#DeliveryCompany').val($('#BillingCompany').val());
			$('#DeliveryAddress1').val($('#BillingAddress1').val());
			$('#DeliveryAddress2').val($('#BillingAddress2').val());
			$('#DeliveryCity').val($('#BillingCity').val());
			$('#DeliveryPostcode').val($('#BillingPostcode').val());
			$('#DeliveryCountry').val($('#BillingCountry').val());
			$('#SameforDelivery').closest('.carttable-row').fadeOut('slow');

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

		}
	
	});
	
	/*------------------------------------------------------
	# 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');
	});

	if ( $('#DeliveryCountry').length > 0 ) {
		$('#DeliveryCountry').ready(function() {
			updateShipping('#DeliveryCountry option:selected','#ShippingCalc');
		});
	}


	/*------------------------------------------------------
	# Update price by product options
	------------------------------------------------------*/
	$('.product_option_group input:radio').change(function(){
	
		$('p.saleprice').hide();
		
		var strProductId = $('[name=product_id]').val();
		var strOptionId = '';

		$('.feedback').append('<img src="/assets/images/loader.gif" />');
		
		$('.product_option_group input:radio:checked').each(function(){
			
			strOptionId += $(this).val() + '-'; 

		});

		//console.log(strOptionId);

		$.post("/index.php/store/updatepricebyoptions/", { product_id: strProductId, option_id: strOptionId },
			function(data){
				$('#product_price').html(data.product_price); //dropdown options
				$('#sale_price').html(data.product_saleprice); //dropdown options

				$('.feedback').empty();
								
		},'json');

	
	});


	/*------------------------------------------------------
	# 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');
		var zoom = $(this).attr('rel');

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

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


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

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

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

	/*------------------------------------------------------
	# Customer reviews rating (star) selection
	------------------------------------------------------*/
	$('.review_rating_star').css('cursor','pointer');

	$('.review_rating_star').click(function(){
	
		$('.review_rating_star').attr('src','/assets/images/star0.png');
		$(this).attr('src','/assets/images/star1.png');
		$(this).prevAll('.review_rating_star').attr('src','/assets/images/star1.png');
		
		var star = $(this).attr('rel');
		$('input[name="review_rating"]').val(star);
	
	});


	/*------------------------------------------------------
	# Customer Reviews Form Validation
	------------------------------------------------------*/
	jQuery.validator.setDefaults({ 
	    messages: {
	    			review_author: '',
	    			review_email: '',
	    			review_title: '',
	    			review_text: ''
	    		  }
	});

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

	/*------------------------------------------------------
	# Tell A Friend Form Validation
	------------------------------------------------------*/
	jQuery.validator.setDefaults({ 
	    messages: {
	    			tellafriend_friendsname: '',
	    			tellafriend_friendsemail: '',
	    			tellafriend_name: '',
	    			tellafriend_email: '',
					tellafriend_message: ''
		}
	});

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

	/*------------------------------------------------------
	# Submit sort results form
	------------------------------------------------------*/
	$('#results_sort').change(function(){
		$(this).closest('form').submit();
	});

});
