jQuery(document).ready(function($){

// Show/Hide relevant panels
	$('#step2').hide();
	$('#calc-tellmerate').click(function(event) {
		event.stopPropagation();
		$('#calc-wagecalc div').addClass('altbg');
		$('#calc-wagecalc').slideUp('fast');
		$('#step2').slideDown('fast');
	});

	// Set default tax bands
	var tax20 = 125;
	var tax40 = 166.6;
	var tax50 = 200;
	$('#calc-tax20').attr("checked","checked").val(tax20);

	// Setup default message for slider %
	$('#calc-percentset div.left h3 b').text('1%');

	// Setup slider
	$("#calculator #slider").slider({
		max: 10,
		min: 1,
		step: 0.1,
		start: function(event,ui) {
			// Get Salary figure and test if valid
			salary = parseFloat($('#calc-annualsalary').val());
			if (isNaN(salary) || (salary==0) || salary=="") {
				event.stopPropagation();
				alert('Please enter your salary in the field above.')
			} else {
				return false;
			}			
		},
		 slide: function(event, ui) {
			// Update slider values on screen
			$("#calc-amount").val('Value: '+ui.value.toFixed(1)+'%');
			$("#calc-percentset div.left h3 b").text(ui.value.toFixed(1)+'%');
			
			// Cufon.refresh not working, investigate:
			// Cufon.refresh('#calc-percentset div.left h3 b');
		},
		stop: function(event,ui) {
			updateBasicDonation();
			updateHourlyRate();
			updateText();
		}
	  });
	  
	// Function to update all text items based on slider position / hourly rate
	function updateHourlyRate() {
		// Get values and do calculations
		salary = parseFloat($('#calc-annualsalary').val());
		hourlyrate = ((salary/52)/35).toFixed(2);
		taxband = parseFloat($('input[name=calc-taxband]:checked').val());
		
		// Update values
		if (isNaN(salary) || (salary==0) || salary=="") {
			// Do nothing if Salary field blank or 0
		} else {
			$('#calcHour').val(hourlyrate);
		}
	}
	
	// Function to update donation field
	function updateBasicDonation() {
		// Get values and do calculations
		donation = parseFloat($('#calc-donateamount').val());
		taxband = parseFloat($('input[name=calc-taxband]:checked').val());
		if (isNaN(donation) || (donation==0) || donation=="") {
			// Do nothing if a string or 0
			$('#calc-receiveamount').html('<span>&pound;</span><span>0</span><span>.</span><span>0</span><span>0</span>');
		} else {
			received = ((donation / 100) * taxband).toFixed(2);
			// Split string into individual characters
			received = received+"";;
			receivedSplit = "<span>&pound;</span>";
			for(i=0; i<received.length; i++) {
				currentDigit = received.substr(i,1)
				receivedSplit += "<span>"+currentDigit+"</span>";
			}
			// Update values
			$('#calc-receiveamount').html(receivedSplit);
		}
	};
	
	// Function to update all text areas based on Salary %
	function updateText() {

		// Set defaults
		htmlYear = "<strong>&pound;0.0</strong> per year";
		htmlMonth = "<strong>&pound;0.0</strong> per month";
		htmlWeek = "<strong>&pound;0.0</strong> per week";
		htmlSummary = "<strong>&pound;00.00</strong> per month<br /><strong>&pound;0.00</strong> per week";

		// Get values and do calculations
		salary = parseFloat($('#calc-annualsalary').val());
		hourlyrate = ((salary/52)/35).toFixed(2);
		taxband = parseFloat($('input[name=calc-taxband]:checked').val());
		percent = $("#calculator #slider").slider("value");
		
		if (isNaN(salary) || (salary==0) || salary=="") {
			// Do nothing if a string or 0
			$('#calc-summary-pointone').html(htmlSummary);
			$('#calc-summary-two').html(htmlSummary);
			$('#calc-summary-five').html(htmlSummary);
			$('#calc-summary-peryear').html(htmlYear);
			$('#calc-summary-permonth').html(htmlMonth);
			$('#calc-summary-perweek').html(htmlWeek);
		} else {
			yearVal = ((salary/100)*percent);
			monthVal = (((salary/100)*percent)/12);
			weekVal = (((salary/100)*percent)/52);
			
			yearValPointone = ((salary/100)*1);
			yearValTwo = ((salary/100)*2);
			yearValFive = ((salary/100)*5);
			
			$('#calc-summary-peryear').html('<strong>&pound;'+yearVal.toFixed(2)+'</strong> per year');
			$('#calc-summary-permonth').html('<strong>&pound;'+monthVal.toFixed(2)+'</strong> per month');
			$('#calc-summary-perweek').html('<strong>&pound;'+weekVal.toFixed(2)+'</strong> per week');
			
			$('#calc-summary-pointone').html('<strong>&pound;'+(yearValPointone/12).toFixed(2)+'</strong> per month<br /><strong>&pound;'+(yearValPointone/52).toFixed(2)+'</strong> per week');
			$('#calc-summary-two').html('<strong>&pound;'+(yearValTwo/12).toFixed(2)+'</strong> per month<br /><strong>&pound;'+(yearValTwo/52).toFixed(2)+'</strong> per week');
			$('#calc-summary-five').html('<strong>&pound;'+(yearValFive/12).toFixed(2)+'</strong> per month<br /><strong>&pound;'+(yearValFive/52).toFixed(2)+'</strong> per week');
			
			$('#calcMonth').val(monthVal.toFixed(2));
		}
	};	
	
	// Monitor Donation field for change, and do updates
	$('#calc-donateamount,input[name=calc-taxband]').bind('change keyup',function(){
		updateBasicDonation();
	});
	
	// Monitor Annual Salary field for change, and do updates
	$('#calc-annualsalary').bind('change keyup',function(){
		updateHourlyRate();
		updateText();		
	});
	  
// Close jQuery object
});
