// holder for
updatedCountry = '';

$(document).ready(
	function(){
		/* snooks "simplest jquery slideshow" */
		if($('#imageFader').length > 0){
			$('#imageFader img:gt(0)').hide();
			setInterval(function(){
				$('#imageFader :first-child').fadeOut()
					 .next('img').fadeIn()
					 .end().appendTo('#imageFader');}, 
				3000);	
		}
		// checkout page - dynamic shipping cost and time changing
		if($('DIV#Checkout').length > 0){
			// hide 'js required' text
			$('#noJS').hide();
			// show submit button
			$('.Actions').css('display', 'block');
			
			// set billing country to NZ if not done already
			if($('SELECT#OrderForm_OrderForm_Country OPTION:selected').val() == ''){
				$('SELECT#OrderForm_OrderForm_Country option').each(
				function(i){
					// change selected value if a matching one is found
					if(this.value == 'New Zealand'){
						// make this the selected item
						this.selected = true;
					}
				}
			);
			}
			// create empty div for holding shipping information
			$('DIV#ShippingCountry').append('<div id="shippingDetails"></div>');
			// create empty hidden fields for holding shipping country, shipping cost and total cost
			$('FORM#OrderForm_OrderForm').append('<input type="hidden" id="calc_shipping_country" name="calc_shipping_country" value="" />');
			$('FORM#OrderForm_OrderForm').append('<input type="hidden" id="calc_shipping_cost" name="calc_shipping_cost" value="" />');
			$('FORM#OrderForm_OrderForm').append('<input type="hidden" id="calc_total_cost" name="calc_total_cost" value="" />');
			$('FORM#OrderForm_OrderForm').append('<input type="hidden" id="default_shipping" name="default_shipping" value="'+default_shipping+'" />');
			// event handler for country drop down
			$('SELECT#OrderForm_OrderForm_ShippingCountry').change(adjustShipping);
			$('SELECT#OrderForm_OrderForm_ShippingCountry').focus(adjustShipping);
			// event handler for shipping = billing address checkbox
			$('INPUT#OrderForm_OrderForm_BillingIsShipping').click(mapBillingToShipping);
			// handler > 'pick up from store' (wipes shipping cost from order)
			$('INPUT#OrderForm_OrderForm_DeliveryMethod_0').click(cancelShippingCost);
			// put back shipping costs if users changes mind
			$('INPUT#OrderForm_OrderForm_DeliveryMethod_1').click(adjustShipping);
			
			
		}
	}
);


function adjustShipping(){
	// only adjust if user is shipping order (not if they've selected to pick up)
	if($('INPUT#OrderForm_OrderForm_DeliveryMethod_1:checked').val() == 1){
		
		// 1. function called by mapBillingToShipping
		if(updatedCountry.length > 0){
			// console.log('called by: mapBillingToShipping');
			adjuster = updatedCountry;
			updatedCountry = '';
		}
		// 2. function called by re-selecting 'Deliver to address below'
		else if($(this).attr('id') == 'OrderForm_OrderForm_DeliveryMethod_1'){
			// console.log('called by re-selecting deliver button');
			// adjuster is country in shipping form (if set)
			if($('SELECT#OrderForm_OrderForm_ShippingCountry OPTION:selected').val()){
				adjuster = $('SELECT#OrderForm_OrderForm_ShippingCountry OPTION:selected').val();
				//// console.log($('SELECT#OrderForm_OrderForm_ShippingCountry OPTION:selected').val());
			}
			// if not set, call mapBillingToShipping
			else{
				// console.log('No country selected in shipping list');
				// check the checkbox billing = shipping
				$('INPUT#OrderForm_OrderForm_BillingIsShipping').attr('checked', 'checked');
				// call mapBillingToShipping
				mapBillingToShipping();
				return;
			}
		}
		// 3. function called by changing shipping country
		else{
			// console.log('called by changing shipping country');
			adjuster = this.value;	
		}
		
		// no country selected
		if(adjuster.length == 0){
			return
		}
		
		countries = country_data.Countries;
		
		for(i = 0; i < country_data.Countries.length; i ++){
			// is there a match between the selected country and the data in the object's array?
			if(countries[i].name == adjuster){
				// remove current details if set 
				$('DIV#shippingDetails').html('');
				// set div with new data
				data = divContent(countries[i].cost, countries[i].time);
				$('DIV#shippingDetails').html(data);
				
				// now adjust shipping cost and info in shipping table row
				if($('TD#Table_SimpleShippingModifier_0_Title')){
					// set country and time
					$('TD#Table_SimpleShippingModifier_0_Title').html('Shipping to: '+adjuster + ' (Shipping Time: '+ countries[i].time +')');
					// set cost
					$('TD#Table_SimpleShippingModifier_0_Total').html('$'+countries[i].cost);
					// adjust new hidden total
					new_total = new Number(order_total + (countries[i].cost - default_shipping));
					
					$('INPUT#calc_total_cost').attr('value', new_total);
					// adjust hidden shipping country and shipping cost
					$('INPUT#calc_shipping_country').attr('value', adjuster);
					$('INPUT#calc_shipping_cost').attr('value', countries[i].cost);
					// adjust new displayed total
					new_total = new Number($('INPUT#calc_total_cost').attr('value'));
					
					$('TD#Table_Order_Total').html('$'+new_total.toFixed(2)+' NZD');
				}
			}
		}
	}
	
}


// map the billing details to the shipping fields
function mapBillingToShipping(){
	
	// console.log($('INPUT#OrderForm_OrderForm_BillingIsShipping:checked').val());
	
	if($('INPUT#OrderForm_OrderForm_BillingIsShipping:checked').val()){
		
		// console.log('checked');
		
		// get data
		bFirst = $('INPUT#OrderForm_OrderForm_FirstName').attr('value');
		bLast = $('INPUT#OrderForm_OrderForm_Surname').attr('value');
		bAddress1 = $('INPUT#OrderForm_OrderForm_Address').attr('value');
		bAddress2 = $('INPUT#OrderForm_OrderForm_AddressLine2').attr('value');
		bCity = $('INPUT#OrderForm_OrderForm_City').attr('value');
		bCountry = $('SELECT#OrderForm_OrderForm_Country option:selected').text();
		// change values
		$('INPUT#OrderForm_OrderForm_Recipient').attr('value', bFirst + ' ' + bLast);
		$('INPUT#OrderForm_OrderForm_ShippingAddress').attr('value', bAddress1);
		$('INPUT#OrderForm_OrderForm_ShippingAddressLine2').attr('value', bAddress2);
		$('INPUT#OrderForm_OrderForm_ShippingCity').attr('value', bCity);
		// get shipping country list & loop through
		$('SELECT#OrderForm_OrderForm_ShippingCountry option').each(
				function(i){
					// change selected value if a matching one is found
					if(this.value == bCountry){
						// make this the selected item
						this.selected = true;
						// update var with new country
						updatedCountry = this.value;
						// run the update function for the shipping cost etc
						adjustShipping();
					}
				}
			);
	}
	else{
		// console.log('not checked');
	}
}

/* cancel the shipping cost/s if user picking up from store */
function cancelShippingCost(){
	//
	$('DIV#shippingDetails').html('Shipping');
	$('TD#Table_SimpleShippingModifier_0_Total').html('$0.00');
	// 
	$('TD#Table_SimpleShippingModifier_0_Title').html('');
	
	new_total = new Number(order_total - default_shipping).toFixed(2);
	
	$('INPUT#calc_total_cost').attr('value', new_total);
	// adjust hidden shipping country and shipping cost
	$('INPUT#calc_shipping_country').attr('value', '');
	$('INPUT#calc_shipping_cost').attr('value', '');
	
	$('TD#Table_Order_Total').html('$'+new_total+' NZD');
	
}

/* return shiping data for holder div */
function divContent(cost, time){
	d = '<strong>Cost:</strong> $'+cost;
	d += '<br />';
	d += '<strong>Shipping Time:</strong> ' + time;
	return d;
}