/*************************************************
 Ton Concrete Calculator
 
 @location		[root]/assets/js/calculator.js
 @author 		Eoghan O'Brien [eoghan@eoghanobrien.com]
 @created		26 October 2007
 @last-modifed	16 November 2007
 
 ************************************************/
 
function calculate_cylinder(height, radius) { var pi = 3.14285714; return pi * radius * radius * height; }
function calculate_rectangle(length, width, height) { return length * width * height; }
function calculate_triangle(length, width, height) { return 0.5 * length * width * height; }
function inches_to_metres(inches) { var inch = 0.0254; return inches * inch; }
function feet_to_metres(feet) { var foot = 0.3048; return feet * foot; }
function mm_to_metres(mms) { var mm	= 0.0010; return mms * mm; }
function convert_to_metres(value, dim) { var metres; switch(dim) { case 'in': metres = inches_to_metres(value); break; case 'ft': metres = feet_to_metres(value); break; case 'mm': metres = mm_to_metres(value); break; } return metres; }

$(function()
{

	$('#ton_cylinder').hide();
	$('#ton_triangular').hide();
	$('.ton_calc-errors').hide();
	
	// On Cylinder Click
	$('#ton_cyl').click(function() {
		$('#ton_cyl').addClass('active'); $('#ton_tri').removeClass('active'); $('#ton_rec').removeClass('active');
		$('#ton_cylinder').show(); $('#ton_rectangular').hide(); $('#ton_triangular').hide(); $('.ton_calc-errors').html('').hide();
		$('#ton_cyl_ans').css('background', '#d6d5d9');

		return false;

	});
	
	// On Rectangular Click
	$('#ton_rec').click(function() {
		$('#ton_rec').addClass('active'); $('#ton_cyl').removeClass('active'); $('#ton_tri').removeClass('active');
		$('#ton_rectangular').show(); $('#ton_cylinder').hide(); $('#ton_triangular').hide(); $('.ton_calc-errors').html('').hide();
		$('#ton_rec_ans').css('background', '#d6d5d9');

		return false;

	});
	
	// On Triangular Click
	$('#ton_tri').click(function() {
		$('#ton_tri').addClass('active'); $('#ton_rec').removeClass('active'); $('#ton_cyl').removeClass('active');
		$('#ton_triangular').show(); $('#ton_rectangular').hide(); $('#ton_cylinder').hide(); $('.ton_calc-errors').html('').hide();
		$('#ton_tri_ans').css('background', '#d6d5d9');

		return false;

	});
	
	// Calculate Cylinder
	$('#ton_calculate_cyl').click(function(){

		var height, radius;
		
		var cyl_height = parseFloat($.trim($('#ton_cyl_height').val()));
		var cyl_radius = parseFloat($.trim($('#ton_cyl_radius').val()));
		var cyl_height_dim	= $.trim($('#ton_cyl_height_dim').val());
		var cyl_radius_dim	= $.trim($('#ton_cyl_radius_dim').val());
		
		if (cyl_height_dim != 'mt') { height = convert_to_metres(cyl_height, cyl_height_dim); } else { height = cyl_height; }
		if (cyl_radius_dim != 'mt') { radius = convert_to_metres(cyl_radius, cyl_radius_dim); } else { radius = cyl_radius; }
		
		if (isNaN(cyl_height) || isNaN(cyl_radius)) {
			$('.ton_calc-errors').html('Please enter a height and radius to calculate').show();  
		} else {
			var cylinder	= calculate_cylinder(height, radius);
			$('.ton_calc-errors').html('').hide();
			$('#ton_cyl_ans').css('background', '#fff');
			$('#ton_cyl_ans').html( ( cylinder.toPrecision(4) * 2 ) + ' tonnes');
		}
	});
	
	// Calculate Rectangle
	$('#ton_calculate_rec').click(function(){
		var length, width, height;
		
		var rec_length = parseFloat($.trim($('#ton_rec_length').val()));
		var rec_width = parseFloat($.trim($('#ton_rec_width').val()));
		var rec_height = parseFloat($.trim($('#ton_rec_height').val()));
		var rec_length_dim	= $.trim($('#ton_rec_length_dim').val());
		var rec_width_dim	= $.trim($('#ton_rec_width_dim').val());
		var rec_height_dim	= $.trim($('#ton_rec_height_dim').val());
		
		if (rec_length_dim != 'mt') { length = convert_to_metres(rec_length, rec_length_dim); } else { length = rec_length; }
		if (rec_width_dim != 'mt') { width = convert_to_metres(rec_width, rec_width_dim); } else { width = rec_width; }
		if (rec_height_dim != 'mt') { height = convert_to_metres(rec_height, rec_height_dim); } else { height = rec_height; }
		
		if (isNaN(rec_length) || isNaN(rec_width) || isNaN(rec_height)) {
			$('.ton_calc-errors').html('Please enter a length, width and height to calculate').show(); 
		} else {
			var rectangle	= calculate_rectangle(length, width, height);
			$('.ton_calc-errors').html('').hide();
			$('#ton_rec_ans').css('background', '#fff');
			$('#ton_rec_ans').html( ( rectangle.toPrecision(4) * 2 )  + ' tonnes');
		}

	});
	
	// Calculate Triangle
	$('#ton_calculate_tri').click(function(){
		var length, width, height;
		
		var tri_length = parseFloat($.trim($('#ton_tri_length').val()));
		var tri_width = parseFloat($.trim($('#ton_tri_width').val()));
		var tri_height = parseFloat($.trim($('#ton_tri_height').val()));
		var tri_length_dim	= $.trim($('#ton_tri_length_dim').val());
		var tri_width_dim	= $.trim($('#ton_tri_width_dim').val());
		var tri_height_dim	= $.trim($('#ton_tri_height_dim').val());
		
		if (tri_length_dim != 'mt') { length = convert_to_metres(tri_length, tri_length_dim); } else { length = tri_length; }
		if (tri_width_dim != 'mt') { width = convert_to_metres(tri_width, tri_width_dim); } else { width = tri_width; }
		if (tri_height_dim != 'mt') { height = convert_to_metres(tri_height, tri_height_dim); } else { height = tri_height; }
		
		if (isNaN(tri_length) || isNaN(tri_width) || isNaN(tri_height)) {
			$('.ton_calc-errors').html('Please enter a length, width and height to calculate').show(); 
		} else {
			var triangle	= calculate_triangle(length, width, height);
			$('.ton_calc-errors').html('').hide;
			$('#ton_tri_ans').css('background', '#fff');
			$('#ton_tri_ans').html( ( triangle.toPrecision(4) * 2 ) + ' tonnes');
		}
	});
});
