$(document).ready(function() {

	$('body').addClass("page-has-javascript");
	

	// Enrolment Form
	
		// Select and highlight the program to enrol on
		$('ul#fm-enrol-on input:checked').next('label').addClass('selected');
		
		$('ul#fm-enrol-on li label').click(function() {
			$('ul#fm-enrol-on li label').removeClass('selected');
			$(this).addClass('selected');
		});
		$('ul#fm-enrol-on li input').click(function() {
			$('ul#fm-enrol-on li label').removeClass('selected');
			$(this).next('label').addClass('selected');
		});
	
	
		// Sex - show and hide relevant medical conditions
		
		$('input#sexMale').click(function() {
			$('tr.female-condition').hide();
			$('tr.male-condition').show();
		});
		$('input#sexFemale').click(function() {
			$('tr.male-condition').hide();
			$('tr.female-condition').show();
		});
		
		$('input#sexFemale:checked').show('tr.female-condition');
	
		// Are you currently exercising?
			$('div#currently-exercising, div#not-currently-exercising').hide();
			
			$('input#currentlyExerciseYes').click(function() {
				$('div#not-currently-exercising').hide();
				$('div#currently-exercising').show();
			});
			$('input#currentlyExerciseNo').click(function() {
				$('div#currently-exercising').hide();
				$('div#not-currently-exercising').show();
			});
			
		// Have you in the past?
			$('div#exercised-in-the-past').hide();
			
			$('input#pastYes').click(function() {
				$('div#exercised-in-the-past').show();
			});
			$('input#pastNo').click(function() {
				$('div#exercised-in-the-past').hide();
			});
			
			
		// If any medical conditions are checked show the doctor consent options
			function countChecked() {
      			var n = $("tr.condition input[value=Yes]:checked").length;
     			 $("tr.doctor th").text("You have answered 'YES' to " + n + (n == 1 ? " Medical Condition" : " Medical Conditions") + ", do you have clearance from your doctor to exercise?");
     			 if (n == 0)
     			 	$('tr.doctor').hide();
     			 else $('tr.doctor').show();
    		}
    		countChecked();
   			$(":radio").click(countChecked);
   			
   			
   			$("tr.condition input[value=Yes]:checked, tr.doctor input[value=Yes]:checked").parent('td').parent('tr').addClass('yes');
   			$("tr.condition input[value=No]:checked, tr.doctor input[value=No]:checked").parent('td').parent('tr').addClass('no');
   			
   			$("tr.condition input[value=Yes], tr.doctor input[value=Yes]").click(function() {
   				$(this).parent('td').parent('tr').removeClass('no').addClass('yes');
   			});
			$("tr.condition input[value=No], tr.doctor input[value=No]").click(function() {
   				$(this).parent('td').parent('tr').removeClass('yes').addClass('no');
   			});
			
			
			
		// Stipe medical conditions
			$('#fm-conditions table tbody tr:even').addClass('alt');
			$("#fm-conditions table tbody tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
	
	
	// Smooth scrolling
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});
	
	
	
	
	
	
	// Set Program
	var queryProgram = $("input[name='program']:checked").val();
	switch (queryProgram) {
		case "Sweat and Tears":
			showSweatAndTears();
			break;
		case "Peak Fitness":
			showPeakFitness();
			break;
		case "Peak Performance":
			showPeakPerformance();
			break;
		case "TrekFit":
			showTrekfit();
			break;
		default:
			showSweatAndTears();
			$('#stProgram').attr("checked", true);
			break;
	}
	$("input#stProgram, li.enrol-on-st").click(showSweatAndTears);
	$("input#pfProgram, li.enrol-on-pf").click(showPeakFitness);
	$("input#ppProgram, li.enrol-on-pp").click(showPeakPerformance);
	$("input#tfProgram, li.enrol-on-tf").click(showTrekfit);
	
	$('#select-program ul li label').mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});

	
	
	
	// Set Location
	$("input[name='stLocation']:checked").each(showLocations);
	$("input[name='stLocation']").click(showLocations);
	
	
	// Set Days
	var queryDays = $("input[name='stDays']:checked").val();
	switch (queryDays) {
		case "3":
			show3days();
			break;
		case "2":
			show2days();
			break;
		default:
			$('#fm-st-package').hide();
			break;
	}
	$("input#stDays2").click(show2days);
	$("input#stDays3").click(show3days);

	
	
});

function showLocations() {
	if($(this).val() == "Northern Beaches"){
		$('#train-3-days').show();
	} else {
		$('#train-3-days').hide();
		show2days();
	}
}

function show2days() {
	if($("#fm-st-package").is(':hidden')){
		$('input#stDays2').attr("checked",true);
		$('.train3days').hide();
		$('.train2days').show();
		$('#fm-st-package').slideDown(400);
	} else {
		$('input#stDays2').attr("checked",true);
		$('.train3days').fadeOut(300);
		//setTimeout("$('.train3days').hide();",300);
		setTimeout("$('.train2days').fadeIn(400);",300);	
	}
}
function show3days() {
	if($("#fm-st-package").is(':hidden')){
		$('.train2days').hide();
		$('.train3days').show();
		$('#fm-st-package').slideDown(400);
	} else {
		$('.train2days').fadeOut(300);
		//setTimeout("$('.train2days').hide();",300);
		setTimeout("$('.train3days').fadeIn(400);",300);	
	}
}

function showSweatAndTears() {
	$("#page").removeClass();
	$("#page").addClass("enrol-st")
	$(".program-options").hide();
	$("#st-options").show();
	$("input#stProgram").attr("checked", true);
}
function showPeakFitness() {
	$("#page").removeClass();
	$("#page").addClass("enrol-pf");
	$(".program-options").hide();
	$("input#pfProgram").attr("checked", true);
}
function showPeakPerformance() {
	$("#page").removeClass();
	$("#page").addClass("enrol-pp");
	$(".program-options").hide();
	$("input#ppProgram").attr("checked", true);
}
function showTrekfit() {
	$("#page").removeClass();
	$("#page").addClass("enrol-tf");
	$(".program-options").hide();
	$("input#tfProgram").attr("checked", true);
}




