var summaryTextBmi = '<p class="topPadding">Your BMI is <strong>{bmi}</strong>. </p><p class="topPadding">This indicates your weight is in the <strong>{bmiSummary}</strong> category for adults of your height. </p>';

var BMICalculator =
{
	gender : ''

	, weightKgEl : null
	, heightCmEl : null
	, weightStEl : null
	, weightPsEl : null
	, heightFtEl : null
	, heightInEl : null

	, bmiButtonMetric : null
	, bmiButtonImperial : null

	, weightKg : 0
	, heightCm : 0
	, weightSt : 0
	, weightPs : 0
	, heightFt : 0
	, heightIn : 0

	, round : function(num, decimalPlaces)
	{
		return Math.round(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);
	}

	, getBmiSummary: function(bmi)
	{
		var bmiSummary;

		if (bmi <= 18.5)
		{
			bmiSummary = 'underweight';
		}
		else if ((bmi > 18.5) && (bmi < 25))
		{
			bmiSummary = 'normal weight';
		}
		else if ((bmi > 25) && (bmi < 30))
		{
			bmiSummary = 'overweight';
		}
		else
		{
			bmiSummary = 'obese';
		}

		return bmiSummary;
	}

	// http://flesler.blogspot.com/2007/10/jqueryscrollto.html

	, calculateBmi : function()
	{
		this.setBmiMetricValues();

		if ((this.weightKg > 0) && (this.heightCm > 0))
		{
		var bmi = this.round(this.weightKg / ((this.heightCm / 100) * (this.heightCm / 100)), 1);
		var bmiSummary = this.getBmiSummary(bmi);

		var text = summaryTextBmi.replace(/\{bmi\}/, bmi);
		text = text.replace(/\{bmiSummary\}/, bmiSummary);

		$('#summaryBmiMetric').html(text);
		$('#summaryBmiMetric').fadeIn('slow');
		}

		return false;
	}

	, calculateBmiImperial : function()
	{
		this.setBmiImperialValues();

		if ((this.weightSt > 0) && (this.heightFt > 0))
		{
		var weight = (14 * this.weightSt) + this.weightPs;
		var height = (12 * this.heightFt) + this.heightIn;

		var bmi = this.round(((weight * 703) / (height * height)), 1);
		var bmiSummary = this.getBmiSummary(bmi);

		var text = summaryTextBmi.replace(/\{bmi\}/, bmi);
		text = text.replace(/\{bmiSummary\}/, bmiSummary);

		$('#summaryBmiImperial').html(text);
		$('#summaryBmiImperial').fadeIn('slow');
		}

		return false;
	}

	, toggleBmiType : function(bmiContainerShowID, bmiContainerHideID)
	{
		$('#' + bmiContainerShowID).show();
		$('#' + bmiContainerHideID).hide();
	}

	, init : function()
	{
		this.weightKgEl = $('#weightKg');
		this.heightCmEl = $('#heightCm');
		this.weightKgEl.bind('change keyup', function() { BMICalculator.checkBmiMetric(); })
		this.heightCmEl.bind('change keyup', function() { BMICalculator.checkBmiMetric(); })

		this.weightStEl = $('#weightSt');
		this.weightPsEl = $('#weightPs');
		this.heightFtEl = $('#heightFt');
		this.heightInEl = $('#heightIn');

		this.weightStEl.bind('change keyup', function() { BMICalculator.checkBmiImperial(); })
		this.weightPsEl.bind('change keyup', function() { BMICalculator.checkBmiImperial(); })
		this.heightFtEl.bind('change keyup', function() { BMICalculator.checkBmiImperial(); })
		this.heightInEl.bind('change keyup', function() { BMICalculator.checkBmiImperial(); })

		this.bmiButtonMetric = $('#bmiButtonMetric');
		this.bmiButtonImperial = $('#bmiButtonImperial');
	}

	, setBmiMetricValues : function()
	{
		try
		{
			this.weightKg = parseInt(this.weightKgEl.val());
		}
		catch (err)
		{
			this.weightKg = 0;
		}
		try
		{
			this.heightCm = parseInt(this.heightCmEl.val());
		}
		catch (err)
		{
			this.heightCm = 0;
		}
	}

	, setBmiImperialValues : function()
	{
		try
		{
			this.weightSt = parseInt(this.weightStEl.val());
		}
		catch (err)
		{
			this.weightSt = 0;
		}
		try
		{
			if ((this.weightPsEl.val() != '') && (!isNaN(this.weightPsEl.val())))
			{
				this.weightPs = parseInt(this.weightPsEl.val());
			}
			else
			{
				this.weightPs = 0;
			}
		}
		catch (err)
		{
			this.weightPs = 0;
		}
		try
		{
			this.heightFt = parseInt(this.heightFtEl.val());
		}
		catch (err)
		{
			this.heightFt = 0;
		}
		try
		{
			if ((this.heightInEl.val() != '') && (!isNaN(this.weightPsEl.val())))
			{
				this.heightIn = parseInt(this.heightInEl.val());
			}
			else
			{
				this.heightIn = 0;
			}
		}
		catch (err)
		{
			this.heightIn = 0;
		}
	}

	, checkBmiMetric : function()
	{
		var isValid = false;
		this.setBmiMetricValues();

		isValid = (this.weightKg > 0) && (this.heightCm > 0);

		if (isValid)
		{
			//this.bmiButtonMetric.removeAttr('disabled');
		}
		else
		{
			//this.bmiButtonMetric.attr('disabled', 'disabled');
		}
	}

	, checkBmiImperial : function()
	{
		var isValid = false;
		this.setBmiImperialValues();

		isValid = (this.weightSt > 0) && (this.heightFt > 0);

		if (isValid)
		{
			//this.bmiButtonImperial.removeAttr('disabled');
		}
		else
		{
			//this.bmiButtonImperial.attr('disabled', 'disabled');
		}
	}

}; // BMICalculator

$(document).ready(function()
{
	BMICalculator.init();
});
