function init(effect) {
   this.effect=effect;
   show('layer1', 0, 0);
   var t= setTimeout("show('layer2', 0, 0)", 2500);
   var t= setTimeout("show('layer3', this.effect)", 3500);
}

function show(elementid) {
   if (elementid == "layer1") {
      document.getElementById("layer1").style.display = '';
      document.getElementById("layer2").style.display = 'none';
      document.getElementById("layer3").style.display = 'none';
   } else if (elementid == "layer2") {
      document.getElementById("layer1").style.display = 'none';
      document.getElementById("layer2").style.display = '';
      document.getElementById("layer3").style.display = 'none';
   } else if (elementid == "layer3") {
      document.getElementById("layer1").style.display = 'none';
      document.getElementById("layer2").style.display = 'none';
      document.getElementById("layer3").style.display = '';

      if (this.effect=='typing') {
         new TypeIt(document.getElementById("animation"));
         TypeIt.runAll();
      }
   }
}

function pause(msecs) {
  var date = new Date();
  var curDate = null;
  do { curDate = new Date(); }
  while(curDate-Date < msecs);
}

TypeIt = function(element, interval, cursor, finishedCallback) {


  if((typeof document.getElementById == "undefined") || (typeof element.innerHTML == "undefined")) {
    this.running = true;    // Never run.
    return;
  }

  this.element = element;
  this.finishedCallback = (finishedCallback ? finishedCallback : function() { return; });
  this.interval = (typeof interval == "undefined" ? 2 : interval);
  this.origText = this.element.innerHTML;
  this.unparsedOrigText = this.origText;
  this.cursor = (cursor ? cursor : "");
  this.currentText = "";
  this.currentChar = 0;
  this.element.typeIt = this;
  if(this.element.id == "") this.element.id = "typeit" + TypeIt.currentIndex++;
  TypeIt.all.push(this);
  this.running = false;
  this.inTag = false;
  this.tagBuffer = "";
  this.inHTMLEntity = false;
  this.HTMLEntityBuffer = "";
}

TypeIt.all = new Array();
TypeIt.currentIndex = 0;
TypeIt.runAll = function() {
  for(var i = 0; i < TypeIt.all.length; i++) TypeIt.all[i].run();
}

TypeIt.prototype.run = function() {
  if(this.running) return;
  if(typeof this.origText == "undefined") {
    setTimeout("document.getElementById('" + this.element.id + "').typeIt.run()", this.interval);
    return;
  }
  if(this.currentText == "") this.element.innerHTML = "";
  //  this.origText = this.origText.replace(/<([^<])*>/, "");     // Strip HTML from text.

  if(this.currentChar < this.origText.length) {
    if(this.origText.charAt(this.currentChar) == "<" && !this.inTag) {
      this.tagBuffer = "<";
      this.inTag = true;
      this.currentChar++;
      this.run();
      return;
    } else if(this.origText.charAt(this.currentChar) == ">" && this.inTag) {
      this.tagBuffer += ">";
      this.inTag = false;
      this.currentText += this.tagBuffer;
      this.currentChar++;
      this.run();
      return;
    } else if(this.inTag) {
      this.tagBuffer += this.origText.charAt(this.currentChar);
      this.currentChar++;
      this.run();
      return;
    } else if(this.origText.charAt(this.currentChar) == "&" && !this.inHTMLEntity) {
      this.HTMLEntityBuffer = "&";
      this.inHTMLEntity = true;
      this.currentChar++;
      this.run();
      return;
    } else if(this.origText.charAt(this.currentChar) == ";" && this.inHTMLEntity) {
      this.HTMLEntityBuffer += ";";
      this.inHTMLEntity = false;
      this.currentText += this.HTMLEntityBuffer;
      this.currentChar++;
      this.run();
      return;
    } else if(this.inHTMLEntity) {
      this.HTMLEntityBuffer += this.origText.charAt(this.currentChar);
      this.currentChar++;
      this.run();
      return;

    } else if(this.origText.charAt(this.currentChar) == "~") {
       pause(1500);
       this.currentChar++;
       this.run();
       return;
    } else if(this.origText.charAt(this.currentChar) == "`") {
       this.currentText = "";
       this.currentChar++;
       this.run();
       return;
    } else {
      this.currentText += this.origText.charAt(this.currentChar);
    }
    this.element.innerHTML = this.currentText;
    // this.element.innerHTML += (this.currentChar < this.origText.length - 1 ? (typeof this.cursor == "function" ? this.cursor(this.currentText) : this.cursor) : "");
    this.currentChar++;
    setTimeout("document.getElementById('" + this.element.id + "').typeIt.run()", this.interval);
  } else {
    this.currentText = "";
    this.currentChar = 0;
        this.running = false;
        this.finishedCallback();
  }
}

