
var blueHenBudgetData = {
	checkedTotalPrice : 0.0,
	checkedTotalHours : 0.0,
	totalPrice : 0.0,
	totalHours : 0.0
};

function blueHenBudget( cssTag )
{
	$( '.budgetlink' ).click( function(evt) {
		evt.preventDefault();

		$('.visible-budget-detail').hide();
		$('.visible-budget-detail').removeClass( 'visible-budget-detail' );
		$('.selected-budget-link-detail').css( 'background-color', '#d0d0e0' ).css( 'color', 'black' );
		$('.selected-budget-link-detail').removeClass( 'selected-budget-link-detail' );

		var targetBlock = $(this).attr( 'href' );
		$( targetBlock ).addClass( 'visible-budget-detail' )
	  $( targetBlock ).show();		
		
		$( this ).addClass( 'selected-budget-link-detail' );
		$( this ).css( 'background-color', 'black' ).css( 'color', 'white' );
	  $( this ).focus();
	});

	$( '.budgettopitem' ).click( function(evt) {
		evt.preventDefault();
		budgetTopClicked( this );
	});

	$( '.bhs-item' ).click( function(evt) {
		recalcBudget();
	});

	$( '.cutall' ).click( function(evt) {	
		evt.preventDefault();
		if ($(this).text() == 'Cut All' ) {
			setAll( this, true );
			$(this).text( 'Spend All' );
		} else {
			setAll( this, false );
			$(this).text( 'Cut All' );
		}
		recalcBudget();
	});

	recalcBudget();

	budgetTopClicked( $('#budgettopo0')[0] );

}

function setAll( thiso, checkit )
{
		var index = $(thiso).attr( 'href' );
		var tindex = index.replace( '#', '' );
		var group = budgetItems[ tindex ].group;
		for (key in budgetItems) {
			var bi = budgetItems[ key ];
			if (bi.group == group) {
				$( '#' + key ).attr( 'checked', checkit );
			}
		}
}

function budgetTopClicked( thiso )
{
		$('.visible-budget').hide();
		$('.visible-budget').removeClass( 'visible-budget' );
		$('.selected-budget-link').css( 'background-color', '#e0e0f0' ).css( 'color', 'black' );
		$('.selected-budget-link').removeClass( 'selected-budget-link' );

		var targetBlock = $(thiso).attr( 'href' );
		$( targetBlock ).addClass( 'visible-budget' )
	  $( targetBlock ).show();		

		var selected = $( $( targetBlock ).find( 'a' )[ 0 ] ); 
		selected.click();

		$( thiso ).addClass( 'selected-budget-link' );
		$( thiso ).css( 'background-color', 'black' ).css( 'color', 'white' );
	  $( thiso ).focus();
}

function recalcBudget()
{
	var cutsByMission = {};

	blueHenBudgetData.checkedTotalPrice = 0.0;
	blueHenBudgetData.checkedTotalHours = 0.0;
	blueHenBudgetData.totalPrice = 0.0;
	blueHenBudgetData.totalHours = 0.0;

	$('.bhs-item').each( function() {
		var index = $(this).attr( 'id' );
		if (index) {
			var item = budgetItems[ index ];
			if (item) {
				blueHenBudgetData.totalPrice += item.price;
				blueHenBudgetData.totalHours += item.hours;
				if ($(this).attr('checked')) {
					blueHenBudgetData.checkedTotalPrice += item.price;
					blueHenBudgetData.checkedTotalHours += item.hours;
					if (cutsByMission[ item.mission ] == null) {
						cutsByMission[ item.mission ] = { mission: item.mission, amount: 0.0, isme: true };
					}
					cutsByMission[ item.mission ].amount += item.price;
				}
			}
		}
	});

	for (var key in cutsByMission) {
		var cut = cutsByMission[ key ];
		$('.cutclass' + cut.mission ).html( cut.amount ).format( {format:"#,###", locale:"us"} );
	}

	if (dollarsWorkingHour && totalBudget) {
		var deficit = ( blueHenBudgetData.totalPrice - blueHenBudgetData.checkedTotalPrice ) - totalBudget;
		var deficithours = deficit / dollarsWorkingHour;

		$('#spentcell').html( Math.round(blueHenBudgetData.totalPrice,0) );//.format( {format:"#,###", locale:"us"} );
		$('#totalcutscell').html( Math.round(blueHenBudgetData.checkedTotalPrice,0) );
		$('#deficitcell').html( Math.round(Math.abs(deficit),0) );
		$('#deficithourscell').html( Math.round(deficithours,0) );
		
		if (deficit > 0.0) {
			$('#surplusordeficit').html( "Remaining Deficit Is: $");
			$('#recapdeficit').show();
			$('#recapsurplus').hide();
		  $('#deficitcell').attr('style', 'color:red');
		} else {
			$('#surplusordeficit').html( "Budget Surplus Is: $");
			$('#recapdeficit').hide();
			$('#recapsurplus').show();
		  $('#deficitcell').attr('style', 'color:green');
		}		
	}
}


