function animatrix(elem) {
  var map = {
    'white': 'rgb(0, 255, 0)',
    'rgb(255, 255, 255)': 'rgb(0, 255, 0)',
    'rgb(0, 255, 0)': 'rgb(0, 240, 0)',
    'rgb(0, 240, 0)': 'rgb(0, 224, 0)',
    'rgb(0, 224, 0)': 'rgb(0, 208, 0)',
    'rgb(0, 208, 0)': 'rgb(0, 192, 0)',
    'rgb(0, 192, 0)': 'rgb(0, 176, 0)',
    'rgb(0, 176, 0)': 'rgb(0, 160, 0)',
    'rgb(0, 160, 0)': 'rgb(0, 144, 0)',
    'rgb(0, 144, 0)': 'rgb(0, 128, 0)',
    'rgb(0, 128, 0)': 'rgb(0, 112, 0)',
    'rgb(0, 112, 0)': 'rgb(0, 96, 0)',
    'rgb(0, 96, 0)': 'rgb(0, 80, 0)',
    'rgb(0, 80, 0)': 'rgb(0, 64, 0)',
    'rgb(0, 64, 0)': 'rgb(0, 48, 0)',
    'rgb(0, 48, 0)': 'rgb(0, 32, 0)',
    'rgb(0, 32, 0)': 'rgb(0, 16, 0)',
    'rgb(0, 16, 0)': 'rgb(0, 0, 0)',
    'rgb(0,255,0)': 'rgb(0, 240, 0)',
    'rgb(0,240,0)': 'rgb(0, 224, 0)',
    'rgb(0,224,0)': 'rgb(0, 208, 0)',
    'rgb(0,208,0)': 'rgb(0, 192, 0)',
    'rgb(0,192,0)': 'rgb(0, 176, 0)',
    'rgb(0,176,0)': 'rgb(0, 160, 0)',
    'rgb(0,160,0)': 'rgb(0, 144, 0)',
    'rgb(0,144,0)': 'rgb(0, 128, 0)',
    'rgb(0,128,0)': 'rgb(0, 112, 0)',
    'rgb(0,112,0)': 'rgb(0, 96, 0)',
    'rgb(0,96,0)': 'rgb(0, 80, 0)',
    'rgb(0,80,0)': 'rgb(0, 64, 0)',
    'rgb(0,64,0)': 'rgb(0, 48, 0)',
    'rgb(0,48,0)': 'rgb(0, 32, 0)',
    'rgb(0,32,0)': 'rgb(0, 16, 0)',
    'rgb(0,16,0)': 'rgb(0, 0, 0)'
  }

  function addChar(elem) {
    elem.append('<div>' + (elem.webdjinn ? 'moc.nnijdbew'.charAt(elem.animatrix) : String.fromCharCode(Math.floor(Math.random() * 94) + 33)) + '</div>');
  }

  function animate(elem) {
    last = $('div:last', elem);
    if (elem.animatrix-- > 0) addChar(elem);
    if (last.css('color') == 'rgb(0, 0, 0)') {
      elem.remove();
    } else {
      setTimeout(function() { animate(elem); }, 100);
      while (last.length) {
        last.css('color', map[last.css('color')]);
        last = last.prev();
      }
    }
  }

  function line(elem) {
    var t = Math.floor(Math.random() * elem.height());
    var h = Math.floor(Math.random() * (elem.height() - t));
    var l = Math.floor(Math.random() * (elem.width() - 25));

    if (h >= 20) {
      var id = 'animatrix' + (elem.animatrix++);
      elem.append('<div id="' + id + '" class="animatrix">');
      id = $('#' + id);
      id.css({ height: h, top: t, left: l });
      id.animatrix = Math.floor(h / 20);
      id.webdjinn = id.animatrix == 12;
      animate(id);
    }  
  }

  if (typeof elem.animatrix == 'undefined') elem.animatrix = 0;
  animatrix.t = setTimeout(function() { animatrix(elem); }, 10);
  line(elem);
}

$(document).ready(function () {
  animatrix($(document.body));
});



