﻿//singleton pattern
var formSprint1500 = {
						//PRIVATE
						//units 'm' or 'y'
						_units : 'm',
						_distanceM : 1500.0,
						_distanceY : 1371,
						_hours : null,
						_minutes : null,
						_seconds : null,
						_frm : null,
						_frmName : 'Sprint1500',
						_raceTime : null,
						_distanceUnit : 'metry',
						//time in strings to show in form
						_timeArray : { "easy" : null,
										"tempo" : null,
										"maximum" : null,
										"speed" : null,
										"xlong" : null},
						_secondsToHms : function(d) {
													d = Number(d);
													var h = Math.floor(d / 3600);
													var m = Math.floor(d % 3600 / 60);
													var s = Math.floor(d % 3600 % 60);
													return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
										},
						_isValidTime : function (time) {
													if(time <= 0 || isNaN(time)) {
																					return false;
													}
													else { return true; }
										},
						_isValidLength : function (rlength) {
														if(rlength <= 0 || isNaN(rlength)) {
																							return false;
														}
														else { return true;}
										},
						_getPaceType : function () {
												 if(this._frm.paceType.value === "metry"){
																						this._units = 'm';
												}
												else {
														this._units = 'y';// dla yardów
												}
									},						
						_setRaceTime : function () {
													this._raceTime = 0;
													if (!!this._hours) { this._raceTime += this._hours; }
													if (!!this._minutes) { this._raceTime += this._minutes; }
													if (!!this._seconds) { this._raceTime += this._seconds; }
													if (this._units === 'y') { this._raceTime *= 0.9; }
										},
						//PUBLIC
						runConversion : function () {
													// race time in seconds, length in m
													var time = null;
													if ( !!this._frm.hours.value) {
																					time = parseInt(this._frm.hours.value, 10) * 3600;
																					this._hours = parseInt(this._frm.hours.value, 10) * 3600;
																					
													}								
													if ( !!this._frm.minutes.value) {
																					time += parseInt(this._frm.minutes.value, 10) * 60;
																					this._minutes = parseInt(this._frm.minutes.value, 10) * 60;
																					
													}
													if ( !!this._frm.seconds.value) {
																					time += parseInt(this._frm.seconds.value, 10);
																					this._seconds = parseInt(this._frm.seconds.value, 10);
																					
													}
													
													if ( !this._isValidTime(time) ) {
																			alert('Please input a valid time');
																			return;
													}
													  
													switch ( this._getPaceType() ){
																			case "metry":
																						// do nothing
																						break;
																			case "yardy":
																						this._distance *= 0.914;// dla yardów
																						this._distanceUnit = 'yardy';
																						break;
																			default : 
													}
													this._setRaceTime();  
													
													this.makeCalculations();
															
										},
						makeCalculations : function () {
														//Time in seconds
														this._timeArray.easy = Math.floor(this._raceTime / 15) + 6;
														this._timeArray.tempo = Math.floor(this._raceTime / 15) + 2;
														this._timeArray.maximum = Math.floor(this._raceTime / 15) - 6;
														this._timeArray.speed = Math.floor(this._raceTime / 15) - 10;
														
														this._frm.easy.value = this._secondsToHms(this._timeArray.easy);
														this._frm.tempo.value = this._secondsToHms(this._timeArray.tempo);
														this._frm.maximum.value = this._secondsToHms(this._timeArray.maximum);
														this._frm.speed.value = this._secondsToHms(this._timeArray.speed);
														
														this._frm.xlong.value = this._frm.easy.value + " - " +  this._secondsToHms(Math.floor(this._raceTime / 15) + 10); 
														
											},				
						initiate : function () {
												
												this._frm = document.getElementById( this._frmName );
												var selektor = document.getElementById('paceType');
												selektor.onchange = function () { formSprint1500.runConversion();};
												var run = document.getElementById('run');
												run.onclick = function () { formSprint1500.runConversion();};
												//if ( !this._frm ) alert ('formularz niezainicjowany');
									},
						runTest : function () {
												
												this._frm.minutes.value = 3;
												this._frm.seconds.value = 22;
												var button = this._frm.run;
												button.click();
									}
};


if (typeof window.onload === "function") {
			var old = window.onload;
			window.onload = function () { 
										old(); 
										formSprint1500.initiate();
										//formSprint1500.runTest();
							};
}
else {
		window.onload = function () { 
										 
										formSprint1500.initiate();
										//formSprint1500.runTest();
							};
}

