//
// Title: Speech Timer for Toastmasters
// Author: Stan Birdwell 
// Date: March 26, 2001
// **************************** Copyright Notice ****************************
// Copyright (C) 2001 Stan Birdwell

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ***************************************************************************
// This timer works in conjunction with timer.html. The timer is launched when the Start/Restart button in
// timer.html is clicked. The timer increments from zero until the desired count is reached. Timer modes are 
// initialized from the timer mode radio buttons in timer.html. 
//
// A Flashing Light feature has been added to replace the Buzzer that would normally sound to indicate 
// that the maximum speech time has elapsed
//
var Stop = 0;
var Start = 0;
var setCount = 0; // initialize counter to zero
var modeValue = 1;
var flash = false;
var flashing = 0;
//
// The getStarted() function is called by the "Start/Restart Timer" button in timer.html
//
function getStarted(count) {
	// need to set Stop = 0 here so that if Reset Timer button pressed more 
	//than once, Stop will always be reset to zero when Start button is pressed.
	Stop = 0; 
	// Test to see if Timer has already been started. If No, then proceed with count down, 
	// if Yes, then reset timer.
	if (Start == 0) { 
	  	//-- Loads the number specified (passed via count) into the form field. 
		document.myForm.Mode.value = parseInt(modeValue);	
		document.myForm.numberShown.value= parseInt(count);
		convertSec(count);
		writeMinSec();
		document.holder.src = imageoff.src;
		document.holder1.src = imageoff.src;
		document.holder2.src = imageoff.src;
	  	//-- Pauses one-second, then calls the addOne function at 1 second intervals.
	  	myTimerID=setInterval("addOne()",1000)
		// Set Start = 1 so we know that timer has been started.
		Start = 1;
		}
		// If timer already started, restart timer. 
		// Pass value of flashing so restartTimer function can determine if timer is flashing
		// restartTimer function will turn of flashing lights if they are flashing
		else restartTimer(flashing);
}

//-- This function adds one to the current value in the form field

function addOne() {
	//Add 1 to current value in form field.
	var newValue = parseInt(document.myForm.numberShown.value) + 1
	//Replace the contents of form field with smaller number.
	document.myForm.numberShown.value = newValue
	document.myForm.timerID.value = myTimerID
	convertSec(newValue);
	writeMinSec();
	// write the following values for debug
	document.myForm.flashing.value = flashing
	document.myForm.flash.value = flash
	
	//if RESET not clicked, do the following
	if (Stop == 0) { // modeValue 0 = 8-10 min, modeValue 1 = 5-7 min, modeValue 2 = 2 min
		//check for Greenlight
		if ((newValue == 60) && (modeValue == 2)){  
			reachedGreenLight()
		}
		if ((newValue == 300) && (modeValue == 1)){  
			reachedGreenLight()
		}
		if ((newValue == 480) && (modeValue == 0)){  
			reachedGreenLight()
		}
		//check for Yellowlight
		if ((newValue == 90) && (modeValue == 2)) { 
			reachedYellowLight()
		}
		if ((newValue == 360) && (modeValue == 1)) { 
			reachedYellowLight()
		}
		if ((newValue == 540) && (modeValue == 0)) { 
			reachedYellowLight()
		}
		//check for Redlight
		if ((newValue == 120) && (modeValue == 2)) {  
			reachedRedLight()
		}
		if ((newValue == 420) && (modeValue == 1)) {  
			reachedRedLight()
		}
		if ((newValue == 600) && (modeValue == 0)) {  
			reachedRedLight()
		}
		//check for Zero time left - FLASH LIGHTS!!!!!!
		if ((newValue == 150) && (modeValue == 2)) {   
		reachedMax();
		}
		if ((newValue == 450) && (modeValue == 1)) {   
		reachedMax();
		}
		if ((newValue == 630) && (modeValue == 0)) {   
		reachedMax();
		}
	}
	// if RESET has been clicked (Stop = 1), call reset function
	if (Stop == 1) {
	// Pass value of "flashing" so resetTimer function can determine if timer is flashing or not;
	// resetTimer function will turn of flashing lights if they are flashing
	resetTimer(flashing)
	}
}

//This function turns on the Green Light
function reachedGreenLight() {
	document.holder.src = imagegreen.src;
}
//This function turns on the Yellow Light
function reachedYellowLight() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageyellow.src;
}
//This function turns on the Red Light
function reachedRedLight() {
	document.holder1.src = imageoff.src;
	document.holder2.src = imagered.src;
}
function reachedBuzzer() {
	//add code to sound buzzer here
}

function reachedMax(count) {
	//-- This is the function that is executed when the max count is reached.
	flashing = 1;
	//clearInterval(myTimerID)
	onLights();
	//document.holder.src = imageoff.src;
	//document.holder1.src = imageoff.src;
	//document.holder2.src = imageoff.src;
	flash = false;
	flashTimerID=setInterval("flashLights()",500)
}
function flashLights () {
	if (flash == false) {
		onLights();
	}
	else if (flash == true) {
		offLights();
	}
	//toggle = !toggle;
	flash = !flash;
	document.myForm.flashtimerID.value = flashTimerID
}
function onLights() {
	document.holder.src = imagegreen.src;
	document.holder1.src = imageyellow.src;
	document.holder2.src = imagered.src;
}
function offLights() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageoff.src;
	document.holder2.src = imageoff.src;
}
function restartTimer(set) {
	//-- This is the function that is executed when the timer is restarted.
	clearInterval(myTimerID)
	if (set == 1){
		resetFlash();
	}
	resetImages();
	Start = 0;
	getStarted(setCount);

}
function resetTimer(set) {
	//-- This is the function that is executed when Stop = 1 (Reset Timer button is pressed).
	document.myForm.flashing.value = set
	clearInterval(myTimerID)
	if (set == 1){
	//turn flashing lights off if they are flashing
		resetFlash();
	}
	resetImages();
	resetFields();
	Stop = 0;
	Start = 0;
	flash = false;
	flashing = 0;
}
// The variable Stop is called by the RESET button. Stop is used in the addOne function to determine if
// the RESET button has been clicked. This function sets it equal to 1
function setStop(){
    Stop = 1;
	resetImages();
	resetFields();
}
function resetFlash() {
	clearInterval(flashTimerID);
}

function convertSec(sec) {
	if (sec >= 0 && sec < 60) {
	seconds = sec;
	minutes = 0;
	}
	if (sec >= 60 && sec < 120) {
	seconds = sec - 60;
	minutes = 1;
	}
	if (sec >= 120 && sec < 180) {
	seconds = sec - 120;
	minutes = 2;
	}
	if (sec >= 180 && sec < 240) {
	seconds = sec - 180;
	minutes = 3;
	}
	if (sec >= 240 && sec < 300) {
	seconds = sec - 240;
	minutes = 4;
	}
	if (sec >= 300 && sec < 360) {
	seconds = sec - 300;
	minutes = 5;
	}
	if (sec >= 360 && sec < 420) {
	seconds = sec - 360;
	minutes = 6;
	}
	if (sec >= 420 && sec < 480) {
	seconds = sec - 420;
	minutes = 7;
	}
	if (sec >= 480 && sec < 540) {
	seconds = sec - 480;
	minutes = 8;
	}
	if (sec >= 540 && sec < 600) {
	seconds = sec - 540;
	minutes = 9;
	}
	if (sec >= 600 && sec < 660) {
	seconds = sec - 600;
	minutes = 10;
	}
	if (sec >= 660 && sec < 720) {
	seconds = sec - 660;
	minutes = 11;
	}
	if (sec >= 720 && sec < 780) {
	seconds = sec - 720;
	minutes = 12;
	}
}
function writeMinSec() {
	document.myForm.Minutes.value = minutes;
	document.myForm.Seconds.value = seconds;	
}
function resetImages() {
	document.holder.src = imageoff.src;
	document.holder1.src = imageoff.src;
	document.holder2.src = imageoff.src;
}
function resetFields() {
	document.myForm.numberShown.value = ""
	document.myForm.timerID.value = ""
	document.myForm.Minutes.value= "";
	document.myForm.Seconds.value= "";
	document.myForm.Mode.value= "";
}
//speechIndex passed from timer mode radio buttons in form - modeValue used to determine timer mode
function speechDuration(speechIndex) {
	var returnValue = true;
	if (Start == 1) {
	// if timer running, set index to previous value (modeValue)
	index = modeValue;
	alert("Timer still runnning! Click OK below then Reset Timer and reselect Speech Duration.");
	// set checked radio button to previous state
	document.myForm.duration[index].checked = true;
	}
	else {
	modeValue = speechIndex;
	//return;
	}
	//return returnValue;
}
//preload images to be swapped
var imagegreen = new Image();
imagegreen.src ="assets/greenlight.gif";

var imageyellow = new Image();
imageyellow.src ="assets/yellowlight.gif";	

var imagered = new Image();
imagered.src ="assets/redlight.gif";	

var imageoff = new Image();
imageoff.src ="assets/offlight.gif";

