$(function() {
	$(".slider").slider({
		start: function() {calculate()},
		slide: function(e, ui) {
			$(this).slider('value', (ui.value));
			calculate();
		},
		stop: function() {calculate()},
		min: 1,
		max: 60,
		value: 25
	});
	calculate();
	
	$("#scoreFiveAvg, #scoreSixAvg").click(function() {
		if($(this).is(":checked")){
			$(this).parent("div.set").children(".slider").slider("disable");
		}
		else{
			$(this).parent("div.set").children(".slider").slider("enable");
		}
		calculate();
	});
	
	$("#fromYear").change(function() {calculate()});
	
	$("input#reset").click(function() {
		$(".slider").each(function() {
			$(this).slider('value', 25);
			calculate();
		})
	});
});
	
	enters = [
		40.00,
		45.00,
		50.00,
		55.00,
		60.00,
		62.00,
		64.00,
		65.00,
		66.00,
		68.00,
		70.00,
		72.00,
		74.00,
		75.00,
		76.00,
		78.00,
		80.00,
		82.00,
		84.00,
		85.00,
		86.00,
		88.00,
		90.00,
		91.00,
		92.00,
		93.00,
		94.00,
		95.00,
		96.00,
		97.00,
		97.50,
		98.00,
		98.50,
		99.00,
		99.25,
		99.50,
		99.60,
		99.70,
		99.80,
		99.90
	];

//2006 scaling report
	aggregates06 = [
		93.2,
		98.7,
		104.4,
		110.3,
		115.9,
		118.3,
		120.8,
		122.2,
		123.5,
		126.1,
		128.8,
		131.5,
		134.1,
		135.5,
		136.9,
		139.8,
		142.8,
		145.9,
		149.3,
		151.1,
		152.8,
		156.8,
		160.7,
		163.0,
		165.4,
		167.8,
		170.6,
		173.5,
		176.9,
		181.0,
		183.4,
		186.1,
		189.3,
		193.6,
		196.2,
		199.4,
		200.7,
		202.5,
		205.1,
		208.6
	];

// 2007 scaling report
aggregates07 = [
		93.4,
		99.3,
		105.2,
		111.0,
		117.0,
		119.5,
		122.1,
		123.5,
		124.7,
		127.3,
		129.8,
		132.6,
		135.4,
		136.8,
		138.3,
		141.1,
		144.1,
		147.2,
		150.5,
		152.3,
		154.1,
		157.6,
		161.6,
		163.6,
		165.9,
		168.4,
		171.2,
		174.3,
		177.7,
		181.8,
		184.1,
		186.8,
		189.8,
		193.8,
		196.2,
		199.6,
		201.2,
		203.0,
		205.0,
		208.2
	];
	

aggregates08 = [
				91.4,
				97.8, 
				104.0,
				109.6,
				115.4,
				118.0,
				120.7,
				122.1,
				123.4,
				126.0,
				128.5,
				131.1,
				133.9,
				135.4,
				136.8,
				139.8,
				142.9,
				146.4,
				149.8,
				151.5,
				153.3,
				156.8,
				161.0,
				163.1,
				165.5,
				168.1,
				170.7,
				173.6,
				176.9,
				180.8,
				183.2,
				185.6,
				188.8,
				192.7,
				195.0,
				198.0,
				199.6,
				201.3,
				203.3,
				206.4
			];
	// */
	function isNotInt(x)
	{ 
		var y=parseInt(x); 
		if (isNaN(y)) return true; 
		return false;
	}

	function calculate()
	{
		var aggregates;
		// Find out what year we're processing from
		switch($("#fromYear").val()){
			case "2008":
				aggregates = aggregates08;
				break;
			case "2007":
				aggregates = aggregates07;
				break;
			case "2006":
				aggregates = aggregates06;
				break;
		}

		if (enters.length != aggregates.length)
		{
			writeToPage("Fatal error! Array of ENTER scores and matching aggregates are of differing lengths")
			return -1; // aggregates and enters arrays MUST be the same length.
		}
		
		// Get the scores
		// Yes. I have heard of arrays.
		var scoreOne = $("div#scoreOne").slider('value');
		$("div#scoreOne").parent("div.set").children("span.label").html("Subect Score 1: " + scoreOne);
		var scoreTwo = $("div#scoreTwo").slider('value');
		$("div#scoreTwo").parent("div.set").children("span.label").html("Subect Score 2: " + scoreTwo);		
		var scoreThree = $("div#scoreThree").slider('value');
		$("div#scoreThree").parent("div.set").children("span.label").html("Subect Score 3: " + scoreThree);
		
		var scoreFour = $("div#scoreFour").slider('value');
		$("div#scoreFour").parent("div.set").children("span.label").html("Subect Score 4: " + scoreFour);
		var scoreFive = $("div#scoreFive").slider('value');
		var scoreSix = $("div#scoreSix").slider('value');
		if($("#scoreFiveAvg").is(":checked")){
			scoreFive = (scoreOne + scoreTwo + scoreThree + scoreFour)/4;
		}
		if($("#scoreSixAvg").is(":checked")){
			scoreSix = (scoreOne + scoreTwo + scoreThree + scoreFour)/4;
		}
		$("div#scoreFive").parent("div.set").children("span.label").html("Subect Score 5: " + scoreFive + " ("+scoreFive/10+" actual)");
		$("div#scoreSix").parent("div.set").children("span.label").html("Subect Score 6: " + scoreSix + " ("+scoreSix/10+" actual)");
		
		// Add the scores together
		var aggregate_score = scoreOne + scoreTwo + scoreThree + scoreFour + (scoreFive * 0.1) + (scoreSix * 0.1);

		if (aggregate_score < aggregates[0]){
			//write out string
			$("div#result div#aggregate").html("Aggregate: " + save);			
			$("div#result div#enter").html("Less than 40");
			return 0; // Exit
		}
		
		for(x = 0; x <= aggregates.length-1; x++)
		{
			if(aggregate_score >= aggregates[x] && aggregate_score < aggregates[x+1])
			{
				save = aggregate_score;
				var aggregate_high = aggregates[x+1];
				var aggregate_low = aggregates[x];
				var enter_high = enters[x+1];
				var enter_low = enters[x];
				var aggregate_difference = aggregate_high - aggregate_low;	// 99.7 - 99.6 = 0.1
				var enter_difference = enter_high - enter_low;				// 100 - 110 = 10
				var modifier = enter_difference / aggregate_difference;		// 0.1 / 10 = 0.01
				var aggregate_score = aggregate_score - aggregate_low;		// 105 - 100 = 5
				var enter = enter_low + (aggregate_score * modifier);		// 99.7 + (5 * 0.01)
				
				//write out enter
				enter = enter.toFixed(2); // ENTER score to 2 decimal places
				save = save.toFixed(2);
				$("div#result div#aggregate").html("Aggregate: " + save);
				$("div#result div#enter").html(enter);
				
				return 0; // Exit
			}
		}
		
		if(aggregate_score >= aggregates[aggregates.length-1])
		{
			// write out string
			$("div#result div#aggregate").html("Aggregate: " + save);
			$("div#result div#enter").html('Greater than 99.90');
		
			return 0 // Exit
		}
		
		return 0;
	}
	