$(document).ready(function() {
	// budget calculations
	updateAllocator();
	updateFieldset();
	$('#content form ol input').click(function(){
		updateAllocator();
		updateFieldset();
	});
	
	// update allocator
	function updateAllocator() {
		// get total in thousands, change to millions
		var totalK = calcTotal();
		var totalM = totalK / 1000;
		// subtract total from council total
		var council = parseInt($('#councilAllocator').attr('rel'));
		var diffK = council - totalK;

			var x = parseFloat($('#rate_of_hike').val());
			var y = 9.2;
			var m = -diffK/1000; 
      var rate_increase = y + m * x;
			var delta  = parseInt(rate_increase * 10)/10;

			$('#sidebarFooter').html('');
			//var delta = parseInt((rate_increase / council) * 1000)/10; // coarse rounding to one decimal place
			if(diffK < 0) {
			$('#sidebarFooter1').html('Based on your current selections, council may have to <strong>increase rates by '+delta+'%</strong>. Typically a 1% rates increase equates to 12 cents a week.');
			}
			else {
			  if(diffK == 0)
			  {
			     $('#sidebarFooter1').html('Although you have balanced your budget, based on your current selections, Council may still have to <strong>increase rates by '+delta+'%</strong>. Typically a 1% rates increase equates to 12 cents a week.');
			  }
			  else
			  {
					 if(delta > 0) {
			     $('#sidebarFooter1').html('Although you have reduced your budget, based on your current selections, Council may still have to <strong>increase rates by '+delta+'%</strong>. Typically a 1% rates increase equates to 12 cents a week.'); 
					 }
					 else {
					 $('#sidebarFooter1').html("");
					 }

			  }
      }


		if(diffK < 0) {
			diffK = -diffK;
			$('#sidebar').removeClass('positive');
			$('#sidebar').addClass('negative');
			$('#difference').children('p').eq(0).html('You are over budget by');
		} else {
			if(diffK == 0) {
				$('#sidebar').removeClass('negative');
				$('#sidebar').addClass('positive');
				$('#difference').children('p').eq(0).html('Amount left to allocate');
				$('#sidebarFooter').html('Currently you are right on budget.');
			}
			else {
				$('#sidebar').removeClass('negative');
				$('#sidebar').addClass('positive');
				$('#difference').children('p').eq(0).html('Amount left to allocate');
				$('#sidebarFooter').html('Currently you are under budget.');
			}
		}
		var diffM = diffK / 1000;
		var diff = diffM;
		if(diff == 0) {
			diff = "0";
		}
		if(totalM == 0) {
			totalM = "0";
		}
		$('#yourAllocator').html(totalM);
		$('#diffAllocator').html(diff);
	}
	
	// update Frameset 
	function updateFieldset() {
		$('#content form fieldset').each(function(){
			var totalK = 0;
			$(this).find('input').each(function(){
				if($(this).attr('checked')) {
					var number = parseInt($(this).attr('rel'));
					totalK = totalK + number;
				}
			});
		
			totalK = totalK / 1000;

			var councilK = parseInt($(this).find('.rightCol .councilBudget .value').attr('rel'));
			if(totalK > councilK) {
				$(this).find('.rightCol .yourBudget strong').removeClass('green');
				$(this).find('.rightCol .yourBudget strong').addClass('red');
			} else {
				$(this).find('.rightCol .yourBudget strong').removeClass('red');
				$(this).find('.rightCol .yourBudget strong').addClass('green');
			}

			var totalM = totalK / 1000;

			if(totalM <= 0) {
				$(this).find('.rightCol .yourBudget .value').html('0');
				$(this).find('.rightCol .yourBudget .abbr').html('');
			}
			else{
			  $(this).find('.rightCol .yourBudget .value').html(totalM);
				$(this).find('.rightCol .yourBudget .abbr').html('m');
			}
		});
	}
	
	// total calculation
	function calcTotal() {
		var total = 0;
		$('#content form ol input').each(function(){
			if($(this).attr('checked')) {
				var number = parseInt($(this).attr('rel'));
				total = total + number;
			}
		});
		return total/1000;
	}
});
