//global var
var aTexts = new Array();
var colors = new Array("#606060", "#505070", "#404080", "#303090", "#2020A0", "#1010B0", "#0000B0");
var bgColors = new Array("#FEF2FF", "#FFE9E9", "#FFD3D3", "#FFBDBD", "#FFA7A7", "#FF9191", "#FF9090");

function animate(div_id) {
   aText = aTexts[div_id];
   aText.nextFrame();
}

////////////////////////////////////////
// AnimatedText object definition START
function startAnimation() {
   this.index = 0;
   this.direction = 1;
   this.count = 0;
   animate(this.div_id);
}

function restoreText() {
   this.element.innerHTML = this.text;
}

function nextFrame() {
   if ( this.count >= this.maxCount ) {
      this.restoreText();
      return;
   }

   t0 = "";
   t1 = "";
   t2 = "";
   t3 = "";
   t4 = "";

   if( this.index > 1)
     t0 = this.text.substring(0, this.index-1);
   if( this.index > 0)
     t1 = this.text.substring(this.index-1, this.index);

   t2 = this.text.substring(this.index, this.index+1);

   if( this.index < this.text.length - 1 )
     t3 = this.text.substring(this.index+1, this.index+2);
   if( this.index < this.text.length - 2 )
     t4 = this.text.substring(this.index+2, this.text.length);


   bgColor = (this.index < bgColors.length - 1) ? bgColors[bgColors.length - 1 - this.index] : bgColors[0];

//   html = "<span style='background-color: "+bgColor+"'>" + t0 + "</span>" + 
//          "<span style='color: "+this.middleColor+"; background-color: "+bgColor+"'>" + t1 + "</span>" +
//          "<span style='color: "+this.blinkColor+"; background-color: "+bgColor+"'>" + t2 + "</span>" +
//          "<span style='color: "+this.middleColor+"; background-color: "+bgColor+"'>" + t3 + "</span>" + 
//          "<span style='background-color: "+bgColor+"'>" + t4 + "</span>";

//   if ( this.index == (this.text.length-1) && this.direction == 1 ||
//        this.index == 0 && this.direction == -1 ) {
//      this.count++;
//      this.direction *= -1;
//   }

   html = "<span style='color: "+colors[this.index]+"; background-color: "+bgColor+"'>" + this.text + "</span>";

   this.element.innerHTML = html;
   
   if ( this.index == (colors.length-1) && this.direction == 1 ||
        this.index == 0 && this.direction == -1 ) {
      this.count++;
      this.direction *= -1;
   }
   
   this.index += this.direction;
   window.setTimeout("animate('"+this.div_id+"')", this.timeout);
}

//function AnimatedText(div_id, middleColor, blinkColor, bgColor, timeout, maxCount) {
function BlinkText(div_id, timeout, maxCount) {
   //properties
   this.div_id = div_id;
   this.element = document.getElementById(div_id);
   this.text = this.element.innerHTML;
   this.timeout = timeout;
   this.index = 0;
   this.direction = 1;
   this.count = 0;
   this.maxCount = maxCount;

//   this.middleColor = middleColor;
//   this.blinkColor = blinkColor;
//   this.bgColor = bgColor;
   
   //methods
   this.startAnimation = startAnimation;
   this.nextFrame = nextFrame;
   this.restoreText = restoreText;
   
   //register this object in the global var array
   aTexts[div_id] = this;
}
// AnimatedText object definition END
////////////////////////////////////

