var g_objectID = 0;
var g_objectArray = new Array();




function Letter(value, visible)
{
  this.m_x = 0;
  this.m_y = 0;
  this.m_value = value;
  this.m_html = document.createElement('DIV');
  this.m_html.style.position = 'absolute';
  if (visible)
    this.m_html.style.display = 'inline';
  else
    this.m_html.style.display = 'none';
  this.m_html.style.color = 'rgb(255, 255, 255)';
  this.m_html.innerHTML = value;
  this.m_html.style.fontFamily = 'courier new';
  this.m_html.style.fontSize = '24px';
  this.m_html.style.fontWeight = 'bold';
  this.m_html.m_object = this;
  this.m_glowState = 0
  this.m_growStep = 0;
  this.m_fadeState = 0;
  this.m_objectID = g_objectID;
  this.m_color = 'rgb(255, 255, 255)';
  g_objectArray[this.m_objectID] = this;
  g_objectID++;

  document.body.appendChild(this.m_html);


  this.SetColor = function(color)
  {
    this.m_html.style.color = color;
    this.m_color = color;
  }



  this.Glow = function(repeatCount, callback)
  {
    this.m_glowState = 256 * repeatCount;
    this.m_glowCallback = callback;
    window.setTimeout("Glow(" + this.m_objectID + ");", 1);
  }


  this.Fade = function(callback)
  {
    this.m_fadeState = 256;
    this.m_fadeCallback = callback;
    window.setTimeout("Fade(" + this.m_objectID + ");", 1);
  }


  this.MoveTo = function(x, y, callback)
  {
    var distanceX = Number(this.m_x - x);
    if (distanceX < 0)
      distanceX = -distanceX;
    var distanceY = Number(this.m_y - y);
    if (distanceY < 0)
      distanceY = -distanceY;
    this.m_stepX = (distanceX / 100);
    this.m_stepY = (distanceY / 100);
    this.m_moveX = x;
    this.m_moveY = y;
    this.m_moveCallback = callback;
    window.setTimeout("MoveTo(" + this.m_objectID + ");", 1);
  }

  this.Position = function(x, y)
  {
    this.m_html.style.top = y + 'px';
    this.m_html.style.left = x + 'px';
    this.m_x = x;
    this.m_y = y;
  }

  this.Clone = function()
  {
     var object = new Letter(this.m_value, false);
     object.Position(this.m_x, this.m_y);
     object.Show(true);
     return object;
  }

  this.Show = function(value)
  {
    if (value == true)
      this.m_html.style.display = 'inline';
    else
      this.m_html.style.display = 'none';
  }

  this.Hide = function()
  {
    this.Show(false);
  }

  this.Grow = function()
  {
    this.m_growStep = 16;
    window.setTimeout("Grow(" + this.m_objectID + ");", 1);
    
  }

  return this;
}



function Glow(objectID)
{
  var object = g_objectArray[objectID];
  var color = object.m_glowState;
  color = color % 256;
  object.m_glowState -= 8;
  object.m_html.style.color = 'rgb(255, ' + color + ', ' + color +')';
  if (object.m_glowState)
    window.setTimeout("Glow(" + objectID + ");", 10);
  else
  {
    object.m_html.style.color = object.m_color;
    if (object.m_glowCallback)
      object.m_glowCallback(object);
  }
}



function Fade(objectID)
{
  var object = g_objectArray[objectID];
  var color = object.m_fadeState;
  object.m_fadeState -= 8;
  object.m_html.style.color = 'rgb(' + color + ', ' + color + ', ' + color +')';
  if (object.m_fadeState)
    window.setTimeout("Fade(" + objectID + ");", 10);
  else
  {
    object.Show(false);
    if (object.m_fadeCallback)
      object.m_fadeCallback(object);
  }
}



function Grow(objectID)
{
  var object = g_objectArray[objectID];
  object.m_html.style.left = (object.m_html.m_x - (object.m_growStep >> 1)) + 'px';
  object.m_html.style.top = (object.m_html.m_y - (object.m_growStep >> 1)) + 'px';
  object.m_html.style.fontSize = (24 + object.m_growStep) + 'px';
  object.m_growStep--;

  if (object.m_growStep)
    window.setTimeout("Grow(" + objectID + ");", 10);
}



function MoveTo(objectID)
{
  var object = g_objectArray[objectID];
  x = object.m_x;
  y = object.m_y;
  if (x < object.m_moveX)
  {
    x += object.m_stepX;
    if (x > object.m_moveX)
      x = object.m_moveX;
  }
  if (x > object.m_moveX)
  {
    x -= object.m_stepX;
    if (x < object.m_moveX)
      x = object.m_moveX;
  }
  if (y < object.m_moveY)
  {
    y += object.m_stepY;
    if (y > object.m_moveY)
      y = object.m_moveY;
  }
  if (y > object.m_moveY)
  {
    y -= object.m_stepY;
    if (y < object.m_moveY)
      y = object.m_moveY;
  }
  object.m_x = x;
  object.m_y = y;
  object.m_html.style.left = x + 'px';
  object.m_html.style.top = y + 'px';
  if ((object.m_moveX != x) || (object.m_moveY != y))
    window.setTimeout("MoveTo(" + objectID + ");", 0);
  else
    if (object.m_moveCallback)
      object.m_moveCallback(object);
}

