$(document).ready(function() {
	
	/*
		Unit conversion constants
	*/
	var mileToKm = 1.609344;
	var mileToYard = 1760;
	var kmToMile = 0.62137119;
	var metreToYard = 1.0936133;
	var yardToMetre = 0.9144;
	
	$('form#pace_calculator').submit(function() {
		try {
			if ($('#pc_pace_min', this).val() == '' && $('#pc_pace_sec', this).val() == '') {

				// calculate using time and distance...
				//console.log('using bottom two...');
			
				var secs = getSecsFromTime($('#pc_sec', this).val(), $('#pc_min', this).val(), $('#pc_hour', this).val());					
				var pace = secs / parseFloatEmpty0($('#pc_distance').val());
				// distance per second...
			
				// console.log(pace);
			
				var pace_min = Math.floor(pace / 60); // distance per minute...
				var pace_sec = pace % 60;
			
				// console.log(pace);
			
				$('#pc_pace_min', this).val(pace_min);
				$('#pc_pace_sec', this).val(pace_sec);
			} else if ($('#pc_distance', this).val() == '') {
				//console.log('using top two...');
								
				var secs = getSecsFromTime($('#pc_sec', this).val(), $('#pc_min', this).val(), $('#pc_hour', this).val());
				var pace = getSecsFromTime($('#pc_pace_sec', this).val(), $('#pc_pace_min', this).val());
				var distance = Math.round(secs*100 / pace) / 100;
			
				$('#pc_distance', this).val(distance);
			} else {
				//console.log('using top and btm');
			
				var pace = getSecsFromTime($('#pc_pace_sec', this).val(), $('#pc_pace_min', this).val());
				var time = parseFloatEmpty0($('#pc_distance', this).val()) * pace;

				$('#pc_hour', this).val(Math.floor(time / 3600));
				$('#pc_min', this).val(Math.floor((time % 3600) / 60));
				$('#pc_sec', this).val(Math.floor(time % 60));
			}
		} catch(err) {
			alert(err);
		}
		return false;
	});
	
	$('form#pace_converter').submit(function() {
		var input = $('#pcon_input', this).val();
		var input_type = parseIntEmpty0($('#pcon_input_type', this).val());
		
		var input;
		
		var output_0;
		var output_0_min;
		var output_0_sec;
		var output_1;
		var output_1_min;
		var output_1_sec;
		var output_2;
		var output_3;
		var output_4;
		
		try {
		
			if (input_type == 0 || input_type == 1) {
			
				//  number of secs it takes to travel a unit of distance (km or mile)
				var pace_sec = getSecsFromTime($('#pcon_pace_sec').val(), $('#pcon_pace_min').val());
			
				if (input_type == 0) {
					// pace_sec is number of secs it takes to travel 1 mile
					output_0 = pace_sec;
					output_1 = output_0 * kmToMile; // travel 1 mile > travel 1 km
					output_2 = 3600 / pace_sec;
					output_3 = output_2 * mileToKm;
				} else {
					// pace_sec is number of secs it takes to travel 1 km
					output_1 = pace_sec;
					output_0 = output_1 * mileToKm; // travel 1 km < travel 1 mile
					output_3 = 3600 / pace_sec;
					output_2 = output_3 * kmToMile;
				}
				// m per s = km per hour * 1000 / 3600
				output_4 = output_3 / 3.6;
			} else {
				if (input_type == 2) {
					output_2 = input;
					output_3 = output_2 * mileToKm; // miles to km
					output_4 = output_3 / 3.6; // km to m/s

					// how long does it take to travel a mile?
					output_0 = Math.round((1 / output_2) * 3600);

					// 1 km =  0.6214 miles so takes 0.6214 of the time
					// to travel a km than a mile...
					output_1 = output_0 * kmToMile;
				} else {
					if (input_type == 4) {
						output_4 = input;
						output_3 = input * 3.6;
					} else { // input_type = 3
						output_3 = input;
						output_4 = input / 3.6;
					}
				
					output_2 = output_3 * kmToMile;
				
					output_1 = Math.round((1 / output_3) * 3600);
					output_0 = output_1 * mileToKm;
				}
			}
		
			output_1_min = Math.floor(output_1 / 60);
			output_1_sec = Math.round(output_1 % 60);
		
			output_0_min = Math.floor(output_0 / 60);
			output_0_sec = Math.round(output_0 % 60);
		
		} catch(err) {
			alert(err);
		}
		
		$('#pcon_results').html(
			'<strong>Pace per mile</strong>: '+output_0_min+'mins '+output_0_sec+'secs <br />'+
			'<strong>Pace per km</strong>: '+output_1_min+'mins '+output_1_sec+'secs <br />'+
			'<strong>Miles per hour</strong>: '+round2DP(output_2)+'<br />'+
			'<strong>Kilometres per hour</strong>: '+round2DP(output_3)+'<br />'+
			'<strong>Metres per second</strong>: '+round2DP(output_4)).show();
		
		// console.log("time per mile: "+output_0);
		// console.log("time per km: "+output_1);
		// console.log("mile per hour "+output_2);
		// console.log("km per hour "+output_3);
		// console.log("metre per sec "+output_4);
		
		return false;
	});
	
	$('#pcon_input_type').bind('change', function() {
		if ($('option:selected', this).val() == '0' || $('option:selected', this).val() == '1') {
			$('#pcon_pace_input').show();
			$('#pcon_normal_input').hide();
		} else {
			$('#pcon_pace_input').hide();
			$('#pcon_normal_input').show();
		}
	});
	
	$('#finish_time_calculator').submit(function() {
		try {
			var split_secs = getSecsFromTime($('#ftc_sec', this).val(), $('#ftc_min', this).val(), $('#ftc_hour', this).val());
			var speed = parseFloatEmpty0($('#ftc_distance_run', this).val()) / split_secs;

			//console.log('speed: '+speed);

			var total = parseFloatEmpty0($('#ftc_distance_total', this).val());

			var dist_unit = $('#ftc_distance_unit').val();
			//console.log(dist_unit);
			var total_unit = $('#ftc_distance_total_unit').val();
			//console.log(total_unit);

			if (dist_unit != total_unit) {
				
				/*
					0 - Miles
					1 - Kilometers
					2 - Metres
					3 - Yards
				*/
				
				if (dist_unit == 0) {
					if (total_unit == 1) {
						speed *= mileToKm;
					} else if (total_unit == 2) {
						speed *= mileToKm * 1000;
					} else if (total_unit == 3) {
						speed *= mileToYard;
					}
				} else if (dist_unit == 1) {
					if (total_unit == 0) {
						speed *= kmToMile;
					} else if (total_unit == 2) {
						speed *= 1000;
					} else if (total_unit == 3) {
						speed *= kmToMile * mileToYard;
					}
				} else if (dist_unit == 2) {
					if (total_unit == 0) {
						speed *= kmToMile / 1000;
					} else if (total_unit == 1) {
						speed /= 1000;
					} else if (total_unit == 2) {
						speed *= metreToYard;
					}
				} else if (dist_unit == 3) {
					if (total_unit == 0) {
						speed /= mileToYard;
					} else if (total_unit == 1) {
						speed /= 1093.6133;
					} else if (total_unit == 2) {
						speed *= yardToMetre;
					}
				}
			}
			
			//console.log('speed: '+speed);				
		
			var total_secs = Math.round(total / speed);
			$('div#ftc_result').html(printTime(total_secs));
		
		} catch(err) {
			alert('Error:'+ err.message+' at line '+err.line);
		}
		
		return false;
	});
	
	$('form#pace_guide').submit(function() {
		try {
			var time_secs = getSecsFromTime($('#pg_sec').val(), $('#pg_min').val() , $('#pg_hour').val());
			var split_sec = time_secs / 26;

			var html = '';
			var increment = 0

			for (var i = 1; i <= 26; i++) {
				increment += split_sec;
				html += printTime(Math.round(increment)) + ' <b>'+i+'</b><br />';
			}

			$('#pace_guide_result').html(html);
		} catch(err) {
			alert(err);
		}

		return false;
	});
});

function printTime(secs) {
	var hour = Math.floor(secs / 3600).toString();
	var min = Math.floor((secs % 3600) / 60).toString();
	var sec = (secs % 60).toString();
	if (min.length == 1) {
		min = '0'+min;
	}
	if (sec.length == 1) {
		sec = '0'+sec;
	}
	return hour+':'+min+':'+sec;
}

function parseIntEmpty0(textstring) {
	if (textstring == '') {
		return 0;
	}
	return parseInt(textstring);
}

function parseFloatEmpty0(textstring) {
	if (!textstring)
		return 0.00;
	
	textstring = textstring.replace(',', '');
	
	if (textstring == '')
		return 0.00;
	
	//console.log(textstring);
	return parseFloat(textstring);
}

function round2DP(val) {
	return (Math.round(val * 100) / 100)
}

function getSecsFromTime(sec, min, hour) {
	var result = parseIntEmpty0(sec);
	if (min)
		result += parseIntEmpty0(min) * 60;
	if (hour)
		result += parseIntEmpty0(hour) * 3600;
	return result;
}
