/**
 *
 */


var calculator = {
	
	start: function() {
		
		$('input').focus(function() {
			if( $(this).val() == 0 ) {
				$(this).val('');
			}
		}).blur(function() {
			if( $(this).val() == '' ) {
				$(this).val('0');
			}
		});
		
		$('#Pi,#PO,#CR,#S,#U,#H,#C').keydown(function(e) {
			if( $(this).attr('id') != 'C' ) {
				if((e.keyCode < 48 || e.keyCode > 53) && (e.keyCode < 96 || e.keyCode > 101)) {
					if(e.keyCode == 8 || e.keyCode == 9 || (e.keyCode >= 37 && e.keyCode <= 40)) {
						return true;
					} else {
						return false;
					}
				}
			}
		}).keyup(function(e) {
			if((e.keyCode >= 48 && e.keyCode <= 53) || (e.keyCode >= 96 && e.keyCode <= 101)) {
				$(this).next().focus().select();
				calculator.run_equation();
			}
		});
		
		$('input').blur(function() {
			calculator.run_equation();
		});
		
	}
	
	,run_equation: function() {
		
		var Pi = parseInt( $('#Pi').val() );
		var PO = parseInt( $('#PO').val() );
		var CR = parseInt( $('#CR').val() );
		var S  = parseInt( $('#S').val() );
		var U  = parseInt( $('#U').val() );
		var H  = parseInt( $('#H').val() );
		var T  = parseInt( $('#T').val() );
		var L  = parseInt( $('#L').val() );
		var C  = parseInt( $('#C').val() );

		if(C) {
			var temp  = parseInt(T * L);
			var temp2 = Math.sqrt(C);
			var temp3 = (temp / temp2);

			var value = Pi + PO + CR + S + U + H;
			value += (temp / temp2);
			value = Math.round(value);
			
			$('.play-value').html( value );
		}
		
	}
	
};

$(document).ready(calculator.start);
