/**
 * @author kevin
 */

$(document).ready( function() {
	Calorie.init();
});

var Calorie = {
	openingMessage: 'Please fill out the values to find out the number of calories burned.',
	init: function() {
		$('#calsubmit').click( function(event) {
			event.preventDefault();
			var output = Calorie.calculate();
			$('#calmessage').fadeOut('slow', function() {
				$(this).attr( 'class', output[1] ).html( output[0] ).fadeIn('slow');
			} );
		});
	},
	calculate: function() {
		var activityVal = parseFloat( $('#activity').attr( 'value' ) );
		var duration = parseInt( $('#dm').attr('value') );
		var lbs = parseInt( $('#calwp').attr('value') );
		
		if ( duration ) {	$('#dm').val( duration ); }
		else { $('#dm').val( '' ); }
		
		if ( lbs ) { $('#calwp').val( lbs ); }
		else { $('#calwp').val( '' ); }
		
		if ( activityVal && duration && lbs ) {
			var calories = activityVal * duration * lbs;
			calories = calories.toFixed();
			var message = calories + ' calories burned';
			var output = [ message, 'calvalue' ];
			return output;
		}
		else {
			var output = [ Calorie.openingMessage, '' ];
			return output;
		}
	}
};

