// This file contains the JavaScript routines used to handle the Nav Buttons

// variables used to light & dim buttons slowly

// 6 state RGB Colour transition list, from Black(#000000) to Yellow(#FFFF00)
var ColourArray = ['#000000', '#333300', '#666600', '#999900', '#CCCC00', '#FFFF00'];

// ButtonColour array stores current colour of up to 10 nav buttons, (element 0 not used)
// ButtonColour array values are used to point to the colours stored in 'ColourArray'
// so valid values for elements are integers in the range 0 to 5
var ButtonColour = [0,0,0,0,0,0,0,0,0,0,0]; // Default start Colour = Black

// Definitions of button colour direction.
var Up = true;     //Up=Lighting up = going from (black to yellow) 
var Down = false;  //Down=Dimming down (yellow to black)

// ColourDirection array stores the current colour change direction
// of up to 10 nav buttons, (element 0 not used).
// Default start direction = Up ie. going from black to Yellow (Lighting up)
var ColourDirection = [0,Up,Up,Up,Up,Up,Up,Up,Up,Up,Up];

// Array containing the IDs of the Button Backgrounds which change colour
var ButtonBackground_ID = [0,'Button1Background','Button2Background','Button3Background','Button4Background','Button5Background','Button6Background','Button7Background','Button8Background','Button9Background','Button10Background'];

//array used to store the Software Timer IDs used by up to 10 Buttons (element 0 not used) 
var ColourChangeTimer_ID = [0,0,0,0,0,0,0,0,0,0,0]; // (IDs are integers)

//array containing code (in string form) which is passed to the setInterval routine.
//This allows up to 10 buttons to be serviced by the same Timer Tick routine by using
//a different passed argument ie. numbers 1 to 10 for the 10 buttons. (element 0 not used)
var TimerTickHandlerList = [0,"TimerTickHandler(1)","TimerTickHandler(2)","TimerTickHandler(3)","TimerTickHandler(4)","TimerTickHandler(5)","TimerTickHandler(6)","TimerTickHandler(7)","TimerTickHandler(8)","TimerTickHandler(9)","TimerTickHandler(10)"];

// TimerTickPeriod value used by Button colour change Software Timers.
var TimerTickPeriod = 50; // (Button colour is incremented(decremented) every 50mS)


function ChangeButtonColour(button_ID, newcolour)
{
   if(document.getElementById){         
      document.getElementById( button_ID).style.backgroundColor = newcolour;   
   } 
}

function LightButtonSlowly( button) //valid button value = from 1 to 10
{
   TurnOffTimerTick(button); //make sure any previous timers are stopped 
   ColourDirection[button] = Up;
   TurnOnTimerTick(button);
}

function DimButtonSlowly(button) //valid button value = from 1 to 10 
{
   TurnOffTimerTick(button); //make sure any previous timers are stopped
   ColourDirection[button] = Down;
   TurnOnTimerTick(button);
}

//The following function is called under timer tick interrupt
function ServiceColourChanges(button) //valid button value = from 1 to 10 
{
   if(ColourDirection[button] == Up){   // Button colour direction == Up (Lighting Up)
      if(ButtonColour[button] < 5 ){
	     ButtonColour[button]++;
		 ChangeButtonColour(ButtonBackground_ID[button], ColourArray[ ButtonColour[button] ]);
	  }
	  else{
	     // Have reached full illumination
	     TurnOffTimerTick(button);
	  }
   }

   else{   // Button colour direction == Down (Dimming)
      if(ButtonColour[button] > 0){
	     ButtonColour[button]--;
	 	 ChangeButtonColour(ButtonBackground_ID[button], ColourArray[ ButtonColour[button] ]);
	  }
	  else{
	    // Have reached fully black (Totally dimmed)
	    TurnOffTimerTick(button);
	  }
   }

}

function TimerTickHandler(button)
{
   ServiceColourChanges(button);
}

function TurnOffTimerTick(button)
{
   if(ColourChangeTimer_ID[button]){
      //check that timer is running before we try to switch it off
      clearInterval(ColourChangeTimer_ID[button]);
	  ColourChangeTimer_ID[button] = 0; // clear out old ID value in array
   }
}

function TurnOnTimerTick(button)
{
   ColourChangeTimer_ID[button] = setInterval(TimerTickHandlerList[button], TimerTickPeriod);
}
