function ScrollingArea(name, box, messages) {
  this.name = name;
  this.box = box;
  this.messages = messages;
  this.curMsg = 0;
  this.curChar = 0;
  this.box.innerHTML = "";
  this.tic = ScrollingAreaTic;
  this.start = ScrollingAreaStart;
  this.stop = ScrollingAreaStop;
  this.togglePause = ScrollingAreaTooglePause;
}

function ScrollingAreaTic() {
  if (this.curChar >= this.messages[this.curMsg].length) {
    this.curChar = -1;
    this.timeoutId = window.setTimeout(this.name + ".tic()", 500);
    return;
  }
  if (this.curChar == -1) {
    this.curChar = 0;
    this.curMsg++;
    if (this.curMsg >= this.messages.length)
      this.curMsg = 0;
  }
  var text = "";
  for (var i = 0; i <= this.curChar; i++) {
    var c = this.messages[this.curMsg].charAt(i);
    switch (c) {
      case "<": text += "&lt;"; break;
      case ">": text += "&gt;"; break;
      case "&": text += "&amp;"; break;
      default: text += c;
    }
  }
  this.box.innerHTML = text + "_";
  this.curChar++;
  this.timeoutId = window.setTimeout(this.name + ".tic()", 100);
}

function ScrollingAreaStart() {
  this.tic();
}

function ScrollingAreaStop() {
  window.clearTimeout(this.timeoutId);
  this.timeoutId = 0;
  this.curMsg = 0;
  this.curChar = 0;
  this.box.innerHTML = "";
}

function ScrollingAreaTooglePause() {
  if (this.timeoutId) {
    this.curChar = this.messages[this.curMsg].length - 1;
    window.clearTimeout(this.timeoutId);
    this.tic();
    window.clearTimeout(this.timeoutId);
    this.timeoutId = 0;
  } else this.tic();
}
