var secs
var timerID = null
var timerRunning = 0

/* kello on objekti, joka päivittää aikaa 5 sekunnin välein,
kun taas kelloDiff on oikean kellon ja käyttäjän asettaman kellon
välinen aikaero minuuteissa */
var kello = null
var kelloDiff = 0

/* kelloFormat = montako tuntia kellossa on */
var kelloFormat = 24

var tulosMoodi = 0

/* asetusMoodi:
	0: ei asetusmoodia
	1: päivä
	2: kuukausi
	3: vuosi
	4: 24/am/pm
	5: tunnit
	6: minuutit
	7: ikä
	8: painoformaatti
	9: paino
*/
var asetusMoodi = 0
var asetukset = new Array(0,0,0,0,0,0,0,0,0,0)

var laiteAuki = true
var delay = 250
if (document.images) {
	pohja_val        =new Image();  pohja_val.src ="images/laite/pohja_val.gif";
	pohja_pun        =new Image();  pohja_pun.src ="images/laite/pohja_pun.gif";
	pohja_har        =new Image();  pohja_har.src ="images/laite/pohja_har.gif";
	m_               =new Image();  m_.src ="images/laite/vali.gif";
	m_a              =new Image();  m_a.src ="images/laite/A.gif";
	m_d              =new Image();  m_d.src ="images/laite/D.gif";
	m_e              =new Image();  m_e.src ="images/laite/E.gif";
	m_k              =new Image();  m_k.src ="images/laite/K.gif";
	m_m              =new Image();  m_m.src ="images/laite/M.gif";
	m_p              =new Image();  m_p.src ="images/laite/P.gif";
	m_w              =new Image();  m_w.src ="images/laite/W.gif";
	m_y              =new Image();  m_y.src ="images/laite/Y.gif";
	m_0              =new Image();  m_0.src ="images/laite/0.gif";
	m_1              =new Image();  m_1.src ="images/laite/1.gif";
	m_2              =new Image();  m_2.src ="images/laite/2.gif";
	m_3              =new Image();  m_3.src ="images/laite/3.gif";
	m_4              =new Image();  m_4.src ="images/laite/4.gif";
	m_5              =new Image();  m_5.src ="images/laite/5.gif";
	m_6              =new Image();  m_6.src ="images/laite/6.gif";
	m_7              =new Image();  m_7.src ="images/laite/7.gif";
	m_8              =new Image();  m_8.src ="images/laite/8.gif";
	m_9              =new Image();  m_9.src ="images/laite/9.gif";
	kellotin         =new Image();  kellotin.src ="images/laite/kellotin.gif";
	kellotin_blank   =new Image();  kellotin_blank.src ="images/laite/kellotin_blank.gif";
	prosentit13      =new Image();  prosentit13.src ="images/laite/prosentit13.gif";
	prosentit18      =new Image();  prosentit18.src ="images/laite/prosentit18.gif";
	tyhja            =new Image();  tyhja.src ="images/laite/tyhja.gif";
	napit_c          =new Image();  napit_c.src="images/laite/valk_c.gif";
	napit_c_pohja    =new Image();  napit_c_pohja.src="images/laite/valk_c_pohja.gif";
	napit_nuoli      =new Image();  napit_nuoli.src ="images/laite/valk_nuoli.gif";
	napit_nuoli_pohja=new Image();  napit_nuoli_pohja.src="images/laite/valk_nuoli_pohja.gif";
	napit_ok         =new Image();  napit_ok.src ="images/laite/valk_ok.gif";
	napit_ok_pohja   =new Image();  napit_ok_pohja.src="images/laite/valk_ok_pohja.gif";
	napit_power      =new Image();  napit_power.src ="images/laite/valk_power.gif";
	napit_power_pohja=new Image();  napit_power_pohja.src="images/laite/valk_power_pohja.gif";
	aset_1           =new Image();  aset_1.src="images/laite/aset_1.gif";
	aset_2           =new Image();  aset_2.src="images/laite/aset_2.gif";
	aset_3           =new Image();  aset_3.src="images/laite/aset_3.gif";
	aset_4           =new Image();  aset_4.src="images/laite/aset_4.gif";
	aset_tyhja       =new Image();  aset_tyhja.src="images/laite/aset_tyhja.gif";
	yksikko_kg       =new Image();  yksikko_kg.src="images/laite/yksikko_kg.gif";
	yksikko_lbs      =new Image();  yksikko_lbs.src="images/laite/yksikko_lbs.gif";
	yksikko_tyhja    =new Image();  yksikko_tyhja.src="images/laite/yksikko_tyhja.gif";
	dmy              =new Image();  dmy.src="images/laite/dmy.gif";
	dmy_tyhja        =new Image();  dmy_tyhja.src="images/laite/dmy_tyhja.gif";
}

function vaihdaVari(vari) {
	switch (vari) {
		case "val":
			document.images["pohja"].src = pohja_val.src
			break
			
		case "pun":
			document.images["pohja"].src = pohja_pun.src		
			break
			
		case "har":
			document.images["pohja"].src = pohja_har.src
			break
	}
}

function press(imageName) {
	switch (imageName) {
		case "nappi_c":
			pressC()
			StartTimer("nappi_c")
			break
		
		case "nappi_ok":
			pressOK()
			break
			
		case "nappi_nuoli":
			pressNuoli()
			StartTimer("nappi_nuoli")
			break
			
		case "nappi_power":
			pressPower()
			StartTimer("nappi_power")
			break
	}
}

function pressPower() {
	document.images["nappi_power"].src = napit_power_pohja.src
	if (laiteAuki) {
      document.ohje.ohjenimi.value = 'Turning the Monitor off'
      document.ohje.ohjeloota.value = 'To turn the Monitor off, hold the right-hand Power button down for about five seconds.\n\nWe wanted to make sure that the Monitor will not accidentally turn off during a hard workout so you have to hold the button down for a while.'
	}	
}

function pressNuoli() {
	document.images["nappi_nuoli"].src = napit_nuoli_pohja.src
	if (laiteAuki) {
		if (asetusMoodi > 0) {
			asetusCounter++

			naytaAsetukset()
		} else {
			// Päästiin juuri asetuksiin
			asetusMoodi = 1
			asetusCounter = asetukset[1]

			naytaAsetukset()
		}
	}	
}

function pressC() {
	document.images["nappi_c"].src = napit_c_pohja.src
	if (laiteAuki && asetusMoodi > 0) {
		// Jos on asetusta, nollataan vain
		if (asetusCounter > 0) {
			asetusCounter = 0
			naytaAsetukset()
			return
		}

		// Muuten kelataan edelliseen asetukseen tai kokonaan pois
		if (asetusMoodi > 1) {
			asetusMoodi--
			asetusCounter = asetukset[asetusMoodi]
			naytaAsetukset()				
		} else {
			document.images["aset"].src = aset_tyhja.src
			document.images["paiva"].src = dmy_tyhja.src
			
			asetusCounter = 0
			asetusMoodi = 0
			tulosMoodi = 0
			
			document.ohje.ohjenimi.value = 'How does Newtest Exercise Monitor - Bone Health Work?'
			document.ohje.ohjeloota.value = "You can freely manipulate the above monitor's buttons. Find out what using Newtest Exercise Monitor - Bone Health is like!"

			naytaAika()				
		}
	} else if (laiteAuki) {
		document.ohje.ohjenimi.value = 'Settings'
		document.ohje.ohjeloota.value = 'The C button is used to cancel and reset settings. You are able to make your own settings to the monitor by clicking the arrow button.'
	}
}

function pressOK() {
	document.images["nappi_ok"].src = napit_ok_pohja.src
	if (laiteAuki && asetusMoodi == 0) {
	      document.ohje.ohjenimi.value = 'Viewing results'
	      document.ohje.ohjeloota.value = 'By pressing the OK button, you can view your previous exercise results. When you press the button once, the display reads "DAY" and shows the amount of daily exercise that you have already completed. When you press the button again, the display reads "WEEK", which shows your weekly exercise results. Press the button a third time to go back to the normal mode.'

		if (tulosMoodi == 0) {
			tulosMoodi = 1
			document.images["k1"].src = m_d.src;
			document.images["k2"].src = m_a.src;
			document.images["kellovali"].src = kellotin_blank.src;
			document.images["k3"].src = m_y.src;
			document.images["k4"].src = m_.src;
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
			document.images["naytto"].src = prosentit13.src
		} else if (tulosMoodi == 1) {
			tulosMoodi = 2
			document.images["k1"].src = m_w.src;
			document.images["k2"].src = m_e.src;
			document.images["kellovali"].src = kellotin_blank.src;
			document.images["k3"].src = m_e.src;
			document.images["k4"].src = m_k.src;
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
			document.images["naytto"].src = prosentit18.src
		} else {
			tulosMoodi = 0
			document.images["naytto"].src = tyhja.src
			naytaAika()
		}
	} else if (laiteAuki) {
		// Ollaan juuri hyväksymässä jokin asetus
		var aika = new Date()
		asetukset[asetusMoodi] = asetusCounter

		switch (asetusMoodi) {
			case 1:
			case 2:
			case 6:
			case 7:
			case 8:
				asetusMoodi++
				asetusCounter = asetukset[asetusMoodi]
				naytaAsetukset()
				break

			case 3:
				asetusMoodi++
				if (kelloFormat == 24) {
					asetusCounter = 0
				} else if (aika.getHours < 12) {
					asetusCounter = 1
				} else {
					asetusCounter = 2
				}
				naytaAsetukset()
				break

			case 4:
				asetusMoodi++
				asetusCounter = aika.getHours() + Math.floor(kelloDiff/60)
				while (asetusCounter < 0) {
					asetusCounter = asetusCounter + 24
				}
				naytaAsetukset()
				break

			case 5:
				asetusMoodi++
				asetusCounter = aika.getMinutes() + kelloDiff%60
				while (asetusCounter < 0) {
					asetusCounter = asetusCounter + 60
				}
				naytaAsetukset()
				break

			default:
				var h = asetukset[5]

				switch (asetukset[4]) {
					case 2:
						h = h + 12
					case 1:
						kelloFormat = 12
						break

					default:
						kelloFormat = 24
				}

				kelloDiff = 60*(h - aika.getHours()) + asetukset[6] - aika.getMinutes()
				
				document.images["aset"].src = aset_tyhja.src
				document.images["unit"].src = yksikko_tyhja.src

				asetusCounter = 0
				asetusMoodi = 0
				tulosMoodi = 0

				naytaAika()
				
				document.ohje.ohjenimi.value = 'Settings: Completed!'
				document.ohje.ohjeloota.value = "Your monitor's settings are now ready. The monitor is functional and ready to go."
		}
	}	
}

function unpress(imageName) {
	StopTimer()

	switch (imageName) {
		case "nappi_c":
			document.images[imageName].src = napit_c.src
			break
		
		case "nappi_ok":
			document.images[imageName].src = napit_ok.src
			break
		
		case "nappi_nuoli":
			document.images[imageName].src = napit_nuoli.src
			break
		
		case "nappi_power":
			document.images[imageName].src = napit_power.src
			break
	}
}

function naytaAsetukset() {
	switch (asetusMoodi) {
		case 1:
			document.ohje.ohjenimi.value = 'Settings: Date'
			document.ohje.ohjeloota.value = "You just opened Newtest Exercise Monitor - Bone Health's settings feature. First you need to set the date. Choose first the day, then the month, and lastly the year.\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button."
			
			asetusCounter = asetusCounter%31
			lukuNakyviin(asetusCounter + 1, 1)
			document.images["kellovali"].src = kellotin_blank.src;
			document.images["k3"].src = m_.src
			document.images["k4"].src = m_.src
			document.images["k5"].src = m_.src
			document.images["k6"].src = m_.src
			document.images["naytto"].src = tyhja.src
			document.images["aset"].src = aset_1.src
			document.images["paiva"].src = dmy.src
			break

		case 2:
			asetusCounter = asetusCounter%12
			lukuNakyviin(asetusCounter + 1, 2)
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
			break

		case 3:
			document.ohje.ohjenimi.value = 'Settings: Date'
			document.ohje.ohjeloota.value = "You just opened Newtest Exercise Monitor - Bone Health's settings feature. First you need to set the date. Choose first the day, then the month, and lastly the year.\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button."
		
			document.images["aset"].src = aset_1.src
			document.images["paiva"].src = dmy.src

			asetusCounter = asetusCounter%100
			lukuNakyviin(asetusCounter, 3)
			lukuNakyviin(asetukset[2] + 1, 2)
			lukuNakyviin(asetukset[1] + 1, 1)			
			break

		case 4:
			document.ohje.ohjenimi.value = 'Settings: Time'
			document.ohje.ohjeloota.value = "Now set up your time. First choose whether you're using a 24-hour or 12-hour clock (AM or PM). Afterwards you will be able to set hours and minutes.\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button."
		
			asetusCounter = asetusCounter%3
			document.images["k1"].src = m_.src;
			document.images["k2"].src = m_.src;
			document.images["k3"].src = m_.src;
			document.images["k4"].src = m_.src;
			document.images["kellovali"].src = kellotin_blank.src;
			document.images["aset"].src = aset_2.src
			document.images["paiva"].src = dmy_tyhja.src

			if (asetusCounter == 0) {
				document.images["k5"].src = m_2.src;
				document.images["k6"].src = m_4.src;
			} else if (asetusCounter == 1) {
				document.images["k5"].src = m_a.src;
				document.images["k6"].src = m_m.src;
			} else if (asetusCounter == 2) {
				document.images["k5"].src = m_p.src;
				document.images["k6"].src = m_m.src;
			}
			break

		case 5:
			document.images["k3"].src = m_.src;
			document.images["k4"].src = m_.src;
			document.images["kellovali"].src = kellotin.src;

			if (asetukset[4] == 0) {
				asetusCounter = asetusCounter%24
				lukuNakyviin(asetusCounter, 1)
			} else {
				asetusCounter = asetusCounter%12
				if (asetusCounter == 0) {
					lukuNakyviin(12, 1)
				} else {
					lukuNakyviin(asetusCounter, 1)
				}
			}
			break

		case 6:
			document.ohje.ohjenimi.value = 'Settings: Time'
			document.ohje.ohjeloota.value = "Now set up your time. First choose whether you're using a 24-hour or 12-hour clock (AM or PM). Afterwards you will be able to set hours and minutes.\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button."
		
			document.images["kellovali"].src = kellotin.src
			document.images["aset"].src = aset_2.src

			asetusCounter = asetusCounter%60
			lukuNakyviin(asetusCounter, 2)
			
			/* Näytetään myös tunnit, jotta näkyvät oikein peruuttaessa */
			if (asetukset[4] == 0) {
				lukuNakyviin(asetukset[5], 1)
			} else {
				if (asetukset[5] == 0) {
					lukuNakyviin(12, 1)
				} else {
					lukuNakyviin(asetukset[5], 1)
				}
			}
			break

		case 7:
			document.ohje.ohjenimi.value = 'Settings: Age'
			document.ohje.ohjeloota.value = 'Now you need to set your age. Choose how many years old you are.\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button.'
		
			document.images["kellovali"].src = kellotin_blank.src;
			document.images["k3"].src = m_.src;
			document.images["k4"].src = m_.src;
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
			document.images["aset"].src = aset_3.src
			document.images["unit"].src = yksikko_tyhja.src

			asetusCounter = asetusCounter%100
			lukuNakyviin(asetusCounter, 1)
			break

		case 8:
			document.ohje.ohjenimi.value = 'Settings: Weight'
			document.ohje.ohjeloota.value = 'Lastly, you need to set your weight. First choose whether you measure your weight in pounds (lbs) or kilograms (kg) and then set your weight. After this your monitor is ready to use!\n\nYou can increase the numbers with the arrow button. First click of the C-button resets the number; second click moves backwards in settings. You can get forward by clicking the OK button.'

			document.images["k1"].src = m_.src;
			document.images["k2"].src = m_.src;
			document.images["k3"].src = m_.src;
			document.images["k4"].src = m_.src;
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
			document.images["aset"].src = aset_4.src
			
			asetusCounter = asetusCounter%2
			if (asetusCounter == 1) {
				document.images["unit"].src = yksikko_lbs.src
			} else {
				document.images["unit"].src = yksikko_kg.src
			}
			break

		case 9:
			asetusCounter = asetusCounter%255
			lukuNakyviin(asetusCounter, 4)
			break
	}
}

// Ajastinfunktiot huolehtivat siitä, että napin pohjassa pitäminen tekee asioita
function StartTimer(imageName) {
	secs = 3
	switch (imageName) {
		case "nappi_c":
			secs = 0.5
			break
		case "nappi_nuoli":
			secs = 0.25
			break
		case "nappi_power":
			secs = 3
			break
	}

	StopTimer()
	RunTimer(imageName)
}

function StopTimer() {
	if(timerRunning > 0) {
		clearTimeout(timerID)
	}
	timerRunning = 0
}

function RunTimer(imageName) {

	if (secs <= 0) {
		switch (imageName) {
			case "nappi_c":
				pressC()
				secs = 0.5
				break
				
			case "nappi_nuoli":
				pressNuoli()
				secs = 0.25
				break
				
			case "nappi_power":
				StopTimer()
				if (laiteAuki) {
					suljeLaite()
				} else {
					aukaiseLaite()
				}
				return
		}
	}
	
	timerRunning = 1
	timerID = self.setTimeout("RunTimer('" + imageName + "')", delay)
	secs = secs - delay/1000
}

function aukaiseLaite() {
	document.ohje.ohjenimi.value = 'How does Newtest Exercise Monitor - Bone Health Work?'
	document.ohje.ohjeloota.value = "You can freely manipulate the above monitor's buttons. Find out what using Newtest Exercise Monitor - Bone Health is like!"
	laiteAuki = true
	tulosMoodi = 0
	naytaAika()
}

function suljeLaite() {
	laiteAuki = false
	document.images["k1"].src = m_.src;
	document.images["k2"].src = m_.src;
	document.images["kellovali"].src = kellotin_blank.src;
	document.images["k3"].src = m_.src;
	document.images["k4"].src = m_.src;
	document.images["k5"].src = m_.src;
	document.images["k6"].src = m_.src;
	document.images["naytto"].src = tyhja.src
	document.images["aset"].src = aset_tyhja.src
	document.images["unit"].src = yksikko_tyhja.src
	document.images["paiva"].src = dmy_tyhja.src
	
	document.ohje.ohjenimi.value = 'Turning the Monitor on'
	document.ohje.ohjeloota.value = 'To turn the Monitor on, hold the right-hand Power button down for about five seconds.'
}

function numerostaKuva(nro) {
	nro = Math.floor(nro)
	if (nro == 0) return m_0.src;
	if (nro == 1) return m_1.src;
	if (nro == 2) return m_2.src;
	if (nro == 3) return m_3.src;
	if (nro == 4) return m_4.src;
	if (nro == 5) return m_5.src;
	if (nro == 6) return m_6.src;
	if (nro == 7) return m_7.src;
	if (nro == 8) return m_8.src;
	if (nro == 9) return m_9.src;
}

// Näytetään lukuarvo näytöllä
//	n: lukuarvo
//	slot: väliltä 1-3 laittaa näkyviin lukuparille 1-3, 4 näyttää sen neljällä isolla numerolla
function lukuNakyviin(n, slot) {
	if (slot < 1 || slot > 4) {
		return
	}

	switch (slot) {
		case 1:
			document.images["k1"].src = numerostaKuva(n/10)
			document.images["k2"].src = numerostaKuva(n%10)
			break

		case 2:
			document.images["k3"].src = numerostaKuva(n/10)
			document.images["k4"].src = numerostaKuva(n%10)
			break

		case 3:
			document.images["k5"].src = numerostaKuva(n/10)
			document.images["k6"].src = numerostaKuva(n%10)
			break

		default:
			document.images["k1"].src = numerostaKuva(n/1000)
			document.images["k2"].src = numerostaKuva((n%1000)/100)
			document.images["k3"].src = numerostaKuva((n%100)/10)
			document.images["k4"].src = numerostaKuva(n%10)
	}
}

function kelloKay() {
	if (laiteAuki && (tulosMoodi == 0) && (asetusMoodi == 0)) {
		naytaAika()
	}
	kello = self.setTimeout("kelloKay()", 5000)
}

function naytaAika() {
	var aika = new Date()

	// varmuustarkastus, koska javascript ei hallitse hyvin negatiivista moduloa
	while (kelloDiff < 0) {
		kelloDiff = kelloDiff + 1440
	}

	var h = (aika.getHours() + Math.floor(kelloDiff/60)) % 24
	var min = (aika.getMinutes() + kelloDiff) % 60

	if (document.images) {
		if (kelloFormat == 12) {
			// 12 tunnin kello

			document.images["kellovali"].src = kellotin.src;
			if (h%12 == 0) {
				lukuNakyviin(12, 1)
			} else {
				lukuNakyviin(h%12, 1)
			}
			lukuNakyviin(min, 2)

			if (h >= 12) {
				document.images["k5"].src = m_p.src;
			} else {
				document.images["k5"].src = m_a.src;
			}
			document.images["k6"].src = m_m.src;
		} else {
			// 24 tunnin kello

			lukuNakyviin(h, 1)
			lukuNakyviin(min, 2)
			document.images["kellovali"].src = kellotin.src;
			document.images["k5"].src = m_.src;
			document.images["k6"].src = m_.src;
		}
	}

	if (kello == null) {
		kello = self.setTimeout("kelloKay()", 5000)
	}
}



