/*************************************************
 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()
{
	$('#cylinder').hide();
	$('#triangular').hide();
	$('.calc-errors').hide();
	
	// On Cylinder Click
	$('#cyl').click(function() {
		$('#cyl').addClass('active'); $('#tri').removeClass('active'); $('#rec').removeClass('active');
		$('#cylinder').show(); $('#rectangular').hide(); $('#triangular').hide(); $('.calc-errors').html('').hide();
		$('#cyl_ans').css('background', '#d6d5d9');
		return false;
	});
	
	// On Rectangular Click
	$('#rec').click(function() {
		$('#rec').addClass('active'); $('#cyl').removeClass('active'); $('#tri').removeClass('active');
		$('#rectangular').show(); $('#cylinder').hide(); $('#triangular').hide(); $('.calc-errors').html('').hide();
		$('#rec_ans').css('background', '#d6d5d9');
		return false;
	});
	
	// On Triangular Click
	$('#tri').click(function() {
		$('#tri').addClass('active'); $('#rec').removeClass('active'); $('#cyl').removeClass('active');
		$('#triangular').show(); $('#rectangular').hide(); $('#cylinder').hide(); $('.calc-errors').html('').hide();
		$('#tri_ans').css('background', '#d6d5d9');
		return false;
	});
	
	// Calculate Cylinder
	$('#calculate_cyl').click(function(){
		var height, radius;
		
		var cyl_height = parseFloat($.trim($('#cyl_height').val()));
		var cyl_radius = parseFloat($.trim($('#cyl_radius').val()));
		var cyl_height_dim	= $.trim($('#cyl_height_dim').val());
		var cyl_radius_dim	= $.trim($('#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)) {
			$('.calc-errors').html('Please enter a height and radius to calculate').show();  
		} else {
			var cylinder	= calculate_cylinder(height, radius);
			$('.calc-errors').html('').hide();
			$('#cyl_ans').css('background', '#fff');
			$('#cyl_ans').html(cylinder.toPrecision(4) + 'm&sup3;');
		}
	});
	
	// Calculate Rectangle
	$('#calculate_rec').click(function(){
		var length, width, height;
		
		var rec_length = parseFloat($.trim($('#rec_length').val()));
		var rec_width = parseFloat($.trim($('#rec_width').val()));
		var rec_height = parseFloat($.trim($('#rec_height').val()));
		var rec_length_dim	= $.trim($('#rec_length_dim').val());
		var rec_width_dim	= $.trim($('#rec_width_dim').val());
		var rec_height_dim	= $.trim($('#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)) {
			$('.calc-errors').html('Please enter a length, width and height to calculate').show(); 
		} else {
			var rectangle	= calculate_rectangle(length, width, height);
			$('.calc-errors').html('').hide();
			$('#rec_ans').css('background', '#fff');
			$('#rec_ans').html(rectangle.toPrecision(4) + 'm&sup3;');
		}
	});
	
	// Calculate Triangle
	$('#calculate_tri').click(function(){
		var length, width, height;
		
		var tri_length = parseFloat($.trim($('#tri_length').val()));
		var tri_width = parseFloat($.trim($('#tri_width').val()));
		var tri_height = parseFloat($.trim($('#tri_height').val()));
		var tri_length_dim	= $.trim($('#tri_length_dim').val());
		var tri_width_dim	= $.trim($('#tri_width_dim').val());
		var tri_height_dim	= $.trim($('#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)) {
			$('.calc-errors').html('Please enter a length, width and height to calculate').show(); 
		} else {
			var triangle	= calculate_triangle(length, width, height);
			$('.calc-errors').html('').hide;
			$('#tri_ans').css('background', '#fff');
			$('#tri_ans').html(triangle.toPrecision(4) + 'm&sup3;');
		}
	});
});
