jQuery(document).ready(function() {
	
	$('.calc-nojs').addClass('hide');
	$('.calc-hide').removeClass('calc-hide');
	
	$('.gender-images a').click( function(e) {
		//Check the correct gender if one of the gender images are clicked
		e.preventDefault();
		var gender = $(this).attr('id');
		gender = gender.replace('slide-', '')
		$('#'+gender).attr('checked', 'checked');
	});
	
	
	$('li a.calculator').click( function(e) {
		e.preventDefault();
		var url = $(this).attr('href');	//Get the href of the clicked link
		calculate( $(this), url )
	});
	
	$('#days-per-week a').click( function(e) {
		e.preventDefault();
		var url = $(this).attr('href');	//Get the href of the clicked link
		daysPerWeek( $(this), url )
	});
	
	$('#calculator-submit').click( function(e) {
		e.preventDefault();
		formSubmit();
	});
	
	$('#calculator-cancel').click( function(e) {
		e.preventDefault();
		formCancel();
	});
	
	// track the calculator interaction
	$('.gender-images a,li a.calculator,#days-per-week a,#calculator-submit,#calculator-submit,#calculator-cancel').click(function() {
		trackCalculatorInteraction();
	});
});

function calculate( object, url ) {
	//Get query string data for calories, units and amount from URL
	var gCalories = gup( url, 'c' );
	var gUnits = gup( url, 'u' );
	var gAmount = gup( url, 'a' );
	var amountID = object.parent('div');
	amountID = amountID.parent('li');
	
	var curDrink = amountID.attr('id');	//Gets the ID of the parent <li> which happens to be the name of the current drink
	amountID = amountID.children('div');
	amountID = amountID.children('span');
	amountID = amountID.children('.amount');
	amountID = amountID.attr('id');	//gets the ID of the <span> where the amount is to be inserted
	
	var curAmount = $('#'+amountID).html();
	var curUnits = $('#total-units').html();
	var curCalories = $('#total-calories').html();
	
	curAmount = parseFloat(curAmount) + parseFloat(gAmount);
	
	if( curAmount > -1 ) {
		curUnits = parseFloat(curUnits) + parseFloat(gUnits);
		curCalories = parseFloat(curCalories) + parseFloat(gCalories);
		
		//Show units and calories to 1 decimal place
		curUnits = Math.round( curUnits*10 )/10
		curCalories = Math.round( curCalories*10 )/10
	
		curUnits = ( curUnits < 0 ) ? 0 : curUnits;
		curCalories = ( curCalories < 0 ) ? 0 : curCalories;
	}
	curAmount = ( curAmount < 0 ) ? 0 : curAmount;

	//Change HTML to calculated values
	$('#'+amountID).html(curAmount);
	$('#total-units').html(curUnits);
	$('#total-calories').html(curCalories);
	
	//Add values to form fields
	var formInput = $('#drink-'+curDrink);
	var formUnits = $('#calc-units');
	var formCalories = $('#calc-calories');
	formInput.val( curAmount );
	formUnits.val( curUnits );
	formCalories.val( curCalories );
}

function daysPerWeek( object, url ) {
	var days = gup( url, 'd' );
	var numDays = $('#num-days');
	var curDays = numDays.html();
	
	curDays = parseInt(curDays) + parseInt(days);
	
	//Makes sure you can't select more than 7 days in a week
	if( curDays < 1 ) {
		curDays = 1;
	}
	else if( curDays > 7 ) {
		curDays = 7;
	}
	
	numDays.html(curDays);
	
	var formDays = $('#calc-num-days');
	formDays.val( curDays );
}

function formSubmit() {
	//Submit form
	$('#drinkCalculatorForm').submit();
}

function formCancel() {
	//Clear form data
	$('#drinkCalculatorForm select').val('0');	//Removes value of all form inputs
	$('#drinkCalculatorForm').submit();
}
