//fadetext.js
window.onload = fadetext; /*  this runs the code once the page has loaded
                            the code will loop round continuously 
                            changing the opacity and then the colour of the text in the fader div */

//global variables - initailse variables so it works on first run through
var fadetext; //contains the div I have styled as fader
var opNum = 1;    
var opInc = 0.1;

function fadetext(){
  fadetext = document.getElementById("fader"); // load the 'fader' div so we can work on its properties
  fadetext.style.opacity = 1; //intialise div to max opacity, otherwise is seems to load as a null
  fadetext.style.color = "red";  // initialise colour, I am using basic colours as the switch statement does not seem to recognise hex numbers
  setInterval ( "changeOpacity()", 200 );  // this built in function runs the change opacity function every 200 milli sec
} // end fadetext
      

/* I was going to use the switch statement to check opacity in the following function
 * but it did not seem to like comparators. I'll work on that later if I get time
 */
function changeOpacity(){
  if(fadetext.style.opacity > 1){  //if at full opacity we need to make the increment positive so it will be deducted and fade the text
    opInc = 0.1
  } else { //if not at full opacity we need to check if it is at minimum opacity
      if(fadetext.style.opacity < 0){
        fadetext.style.opacity = 0; // we are not invisible so start to make text visible and change colour
        opInc = -0.1;
        switch(fadetext.style.color){
            case "red":
              fadetext.style.color = "blue";
              break;
            case "blue":
              fadetext.style.color = "green";
              break;
            case "green":
              fadetext.style.color = "red";
              break;
            default:
              fadetext.style.color = "green";
          } // end switch color   
      } // end if opacity <==0
  } // end if opacity >==1
  opNum = opNum - opInc;  // change the opNum, opInc can be positive or negative depending on whether we are fading or vice versa
  fadetext.style.opacity = opNum;
} // end changeOpacity func  
      

    
