function FoodType(id, category, question, weightOfOnePortion, weightType, fatPerPortion, satFatPerPortion, divider)
{
	this.id = id;
	this.category = category;
	this.question = question;
	this.weightOfOnePortion = weightOfOnePortion;
	this.weightType = weightType;
	this.fatPerPortion = fatPerPortion;
	this.satFatPerPortion = satFatPerPortion;

	if (!divider)
	{
		this.divider = 1;
	}
	else
	{
		this.divider = divider;
	}
}

FoodType.prototype.calculateFat = function(numPortions)
{
	return (this.fatPerPortion * numPortions) / this.divider;
}

FoodType.prototype.calculateSatFat = function(numPortions)
{
	return (this.satFatPerPortion * numPortions) / this.divider;
}


// Constants
var MALE = 'm';
var FEMALE = 'f';

var MALE_ADDITIONAL_FAT = 15.6;
var MALE_ADDITIONAL_SATFAT = 4.9;
var FEMALE_ADDITIONAL_FAT = 11.7;
var FEMALE_ADDITIONAL_SATFAT = 3.5;

var MALE_RECOMMENDED_FAT = 95;
var MALE_RECOMMENDED_SATFAT = 30;
var FEMALE_RECOMMENDED_FAT = 70;
var FEMALE_RECOMMENDED_SATFAT = 20;

var ADULT_AGE = 18;

var summaryText = '<h3>Your fat age is <strong>{fatAge}</strong></h3><h3>Your saturated fat age is <strong>{satFatAge}</strong></h3>';
var summaryTextBmi = '<p class="topPadding result">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 fatCalculator =
{
	calculated : false
	, foodTypes : new Array(
		new FoodType(0, 'Cakes and pastries', 'How many times a week do you eat cakes or sweet pastries (like a Danish Pastry) NOTE two slices of cake count as two portions!', 50, 'g', 11, 2.5, 7)
		, new FoodType(1, 'Biscuits', 'How many times a week do you eat a biscuit?', 12, 'g', 2.7, 1.3, 7)
		, new FoodType(2, 'Chocolate bars', 'How many chocolate bars do you eat a week (like a Mars, kit Kat etc)', 50, 'g', 12, 5.8, 7)
		, new FoodType(3, 'Pizza', 'How many times a week do you eat pizza', 260, 'g', 23.1, 10.1, 7)
		, new FoodType(4, 'Cheese', 'How many portions of cheese do you have a week?', 40, 'g', 13.7, 8.7, 7)
		, new FoodType(5, 'Eggs', 'How many eggs do you eat a week', 50, 'g', 5.6, 1.5, 7)
		, new FoodType(6, 'Biutter', 'How much butter do you have a day (a portion is the amount you would spread on a slice of bread)', 10, 'g', 8.2, 5.2)
		, new FoodType(7, 'Margarine', 'How much margrine do you have a day (a portion is the amount you would spread on a slice of bread_', 10, 'g', 6.8, 1.6)
		, new FoodType(8, 'Red Meat', 'How many times a week do you eat red meat ', 150, 'g', 12.9, 5.4, 7)
		, new FoodType(9, 'Chicken ', 'How many times a week do you eat chicken', 100, 'g', 7.5, 2.1, 7)
		, new FoodType(10, 'Sauages, burgers or pies', 'How many times a week do you eat sausages, burgers or pies?', 75, 'g', 20.3, 7.5, 7)
		, new FoodType(11, 'Crisps', 'How many times a week do you eat a bag of crisps', 40, 'g', 9.2, 2.2, 7)
		, new FoodType(12, 'Chips', 'How many times a week do you eat chi[ps', 165, 'g', 20.5, 1.8, 7)
		, new FoodType(13, 'Milk', 'Do you take milk in your everday cups of tea or coffee', 150, 'mls', 2.6, 1.5)
		, new FoodType(14, 'Milk', 'Do you have milk with cereal', 100, 'mls', 1.7, 1)
		, new FoodType(15, 'Milk', 'How many glasses of milk, milkshake, cappucinos or lattes do you have a day?', 200, 'mls', 3.4, 2)
	)
	, gender : ''
	, portionSelections : null

	, 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);
	}

	, calculate : function()
	{
		this.gender = $("input[@name='gender']:checked").val();

		if (!this.portionSelections)
		{
			this.portionSelections = $('select.portionSelect');
		}

		var j;
		var fat = 0;
		var satFat = 0;
		var portions;

		for (var i = 0; i < this.portionSelections.length; i++)
		{
			j = parseInt(this.portionSelections[i].id.replace(/q/, ''), 10);

			portions = parseInt($('#' + this.portionSelections[i].id).val());

			if (portions > 0)
			{
				fat += this.foodTypes[j].calculateFat(portions);
				satFat += this.foodTypes[j].calculateSatFat(portions);
			}
		}

		var fatAge;
		var satFatAge;
		var age = parseInt($('#age').val());
		var recommendedFat;
		var recommendedSatFat;

		if (this.gender == MALE)
		{
			fat += MALE_ADDITIONAL_FAT;
			satFat += MALE_ADDITIONAL_SATFAT;
			recommendedFat = MALE_RECOMMENDED_FAT;
			recommendedSatFat = MALE_RECOMMENDED_SATFAT;
		}
		else
		{
			fat += FEMALE_ADDITIONAL_FAT;
			satFat += FEMALE_ADDITIONAL_SATFAT;
			recommendedFat = FEMALE_RECOMMENDED_FAT;
			recommendedSatFat = FEMALE_RECOMMENDED_SATFAT;
		}

		fatAge = this.round(((((fat - recommendedFat) / recommendedFat) + 1) * (age - ADULT_AGE)) + ADULT_AGE, 0);
		satFatAge = this.round(((((satFat - recommendedSatFat) / recommendedSatFat) + 1) * (age - ADULT_AGE)) + ADULT_AGE, 0);

		var text = summaryText;

		text = text.replace(/\{fatAge\}/, fatAge);
		text = text.replace(/\{satFatAge\}/, satFatAge);

		$('#summary').html(text);
		$.scrollTo('#summaryBottom', 250);
		$('#summary').fadeIn('slow');

		return false;
	}

	, 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() { fatCalculator.checkBmiMetric(); })
		this.heightCmEl.bind('change keyup', function() { fatCalculator.checkBmiMetric(); })

		this.weightStEl = $('#weightSt');
		this.weightPsEl = $('#weightPs');
		this.heightFtEl = $('#heightFt');
		this.heightInEl = $('#heightIn');

		this.weightStEl.bind('change keyup', function() { fatCalculator.checkBmiImperial(); })
		this.weightPsEl.bind('change keyup', function() { fatCalculator.checkBmiImperial(); })
		this.heightFtEl.bind('change keyup', function() { fatCalculator.checkBmiImperial(); })
		this.heightInEl.bind('change keyup', function() { fatCalculator.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');
		}
	}

}; // fatCalculator

$(document).ready(function()
{
	fatCalculator.init();
});
