///////////////////////
function init() {
  start = "FAF3E5";
  finish= "AAA08C";
  loop = 1;
  loopID= 0;
  iLoopCount = 1;
  
  d_r= parseInt(1);
  d_g= parseInt(1);
  d_b= parseInt(1);

  // generate the starting RGB values
  s_r= split(start,0);  s_g= split(start,1);  s_b= split(start,2);

  // and do the same for the final RGB values
  f_r= split(finish,0); f_g= split(finish,1); f_b= split(finish,2);

  // work out the difference between the start and finish
  // colours of each channel
  l_r= s_r- f_r;        l_g= s_g- f_g;        l_b= s_b- f_b;

  // and set the current colour to the starting colour
  red= s_r;             green= s_g;           blue= s_b;
}

function startFade() {

  clearTimeout(loopID);

  if (l_r< 0-d_r) l_r+=d_r; else { if (l_r> 0+d_r) l_r-=d_r; else l_r=0; }
  if (l_g< 0-d_g) l_g+=d_g; else { if (l_g> 0+d_g) l_g-=d_g; else l_g=0; }
  if (l_b< 0-d_b) l_b+=d_b; else { if (l_b> 0+d_b) l_b-=d_b; else l_b=0; }

  if (l_r== 0 && l_g== 0 && l_b== 0) {

    // what type of loop do we want to do?
    if (loop== 0) {
      // then stop fading about
      stopFade();

    } else {
      // we're looping again, so reset the amount of each colour left
      l_r= f_r- s_r; l_g= f_g- s_g; l_b= f_b- s_b;

      if (loop== 1) {
        // two-way fade (swap start and finish colours)
        tmp= s_r; s_r= f_r; f_r= tmp; // swap red channel
        tmp= s_g; s_g= f_g; f_g= tmp; // swap green channel
        tmp= s_b; s_b= f_b; f_b= tmp; // swap blue channel
      } else {
        // one-way fade (do nothing, act naturally)
      }
  
	if (iLoopCount%4 == 0)
		document.getElementById('tdText').innerHTML = "Strong Foundations";
	if (iLoopCount%4 == 2)
		document.getElementById('tdText').innerHTML = "Strong Future";



      // and prepare to do it all again
      iLoopCount++
      
      loopID= setTimeout("startFade()",100);
    }

  } else {
    // business as usual, update the current colour
    red= l_r+ f_r; green= l_g+ f_g; blue= l_b+ f_b;

    // set the background to this colour
    document.getElementById('tdText').style.color= getHex(red,green,blue);

    // and prepare to do it all again
    loopID= setTimeout("startFade()",100);
  
  }
}

function stopFade() {
  // set the background colour to the finish colour and end
  document.getElementById('tdText').style.color= '#'+DecToHex(f_r)+DecToHex(f_g)+DecToHex(f_b);
  clearTimeout(loopID);
}

function split(colour, which_pair) {
  // take a hex string and a string position and return the decimal
  // value of that pair (0xFF= 255 etc..)
  return HexToDec(colour.substring(0+ (which_pair* 2),
                                   2+ (which_pair* 2)));
}

function getHex(r,g,b) {
  // convert 3 dec integers to hex strings and use as a colour value
  return '#'+DecToHex(r)+DecToHex(g)+DecToHex(b);
}

var hexbase="0123456789ABCDEF";
function DecToHex(number) {
  // take a decimal integer, return 2-digit hex string
  return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf);
}

function HexToDec(number) {
  // take a hex string, return decimal integer
  return parseInt(number.toUpperCase(), 16);
}

//////////////////