var life = {
  init: function() {
    if (!document.createElement) return;
    n = document.getElementById('nav');
    if (!n || !n.innerHTML) return;
    var lnk = document.createElement('link');
    lnk.setAttribute('rel','stylesheet');
    lnk.setAttribute('href','/designs/all/life.css');
    lnk.setAttribute('type','text/css');
    document.getElementsByTagName('head')[0].appendChild(lnk);
    lis = n.getElementsByTagName('li');
    for (var i=0;i<lis.length;i++) {
      li = lis[i];
      as = li.getElementsByTagName('a');
      el = li;
      if (as.length > 0) el = as[0];
      
      // walk through el content and add spans for every letter
      cont = el.firstChild.nodeValue;
      letters = cont.split('');
      output = '<span>'+letters.join('</span><span>')+'</span>';
      el.innerHTML = output;
      
      if (el.nodeName.toLowerCase() == 'a') {
        li.innerHTML = '';
        li.appendChild(el);
      }
    }
    
    life.COLOURS = 2;
    
    // wait a little bit to give the DOM parser time to catch up
    setTimeout(life.startGame,500);
  },
  
  startGame: function() {
    life.populateBoard();
    setInterval(life.updateBoard,1000);
  },
  
  populateBoard: function() {
    n = document.getElementById('nav');
    spans = n.getElementsByTagName('span');
    if (!spans[0].offsetTop) return;
    life.matrix = [];
    lastLeft = 100000;
    for (var i=0;i<spans.length;i++) {
      var s = spans[i];
      if (s.offsetLeft < lastLeft) {
        life.matrix[life.matrix.length] = [];
        thisList = life.matrix[life.matrix.length-1];
      }
      thisList[thisList.length] = s;
      lastLeft = s.offsetLeft;
      s.colour = parseInt(Math.random()*life.COLOURS);
    }
    if (life.matrix.length == 1) { return }
    extras = life.matrix[life.matrix.length-2].length - 
             life.matrix[life.matrix.length-1].length;
    lis = n.getElementsByTagName('li');
    lastli = lis[lis.length - 1];
    for (var i=0;i<extras;i++) {
      var s = document.createElement('span');
      s.appendChild(document.createTextNode(' '));
      s.colour = parseInt(Math.random()*life.COLOURS);
      last = life.matrix[life.matrix.length-1];
      last[last.length] = s;
      lastli.appendChild(s);
    }
  },
  
  updateBoard: function() {
    life.makeNewColours();
    life.updateColours();
  },
  
  makeNewColours: function() {
    for (var i=0;i<life.matrix.length;i++) {
      mrow = life.matrix[i];
      for (var j=0;j<mrow.length;j++) {
        mcell = mrow[j];
        liveneighbours = 0;
        if (life.getColour(i,j,-1,0) > 0) liveneighbours += 1;
        if (life.getColour(i,j,1,0) > 0) liveneighbours += 1;
        if (life.getColour(i,j,0,-1) > 0) liveneighbours += 1;
        if (life.getColour(i,j,0,1) > 0) liveneighbours += 1;
        if (mcell.colour==0 && liveneighbours == 3) {
          mcell.newColour = 1;
        } else if (mcell.colour==1 && 
                  (liveneighbours==2 || liveneighbours==3)) {
          mcell.newColour = 1;
        } else {
          mcell.newColour = 0;
        }
      }
    }
  },
  
  getColour: function(x,y,xo,yo) {
    var nx = x + xo;
    nx = nx % life.matrix[0].length;
    if (nx < 0) nx = life.matrix[0].length - 1 + nx;
    var ny = y + yo;
    ny = ny % life.matrix.length;
    if (ny < 0) ny = life.matrix.length + ny;
    //document.getElementById('content').innerHTML += nx + ',' + ny + ' ';
    try {
      return life.matrix[ny][nx].colour;
    } catch(e) {
      document.getElementById('content').innerHTML = y+','+yo+','+ny;
    }
  },
  
  updateColours: function() {
    var diffs = 0;
    for (var i=0;i<life.matrix.length;i++) {
      mrow = life.matrix[i];
      for (var j=0;j<mrow.length;j++) {
        mcell = mrow[j];
        if (mcell.colour != mcell.newColour) diffs += 1;
        mcell.colour = mcell.newColour;
        mcell.className = 'lifecol'+mcell.colour;
      }
    }
    if (diffs == 0) life.populateBoard();
  },
  
  addEvent: function(obj, evType, fn){
    if (obj.addEventListener){
      obj.addEventListener(evType, fn, true);
      return true;
    } else if (obj.attachEvent){
      var r = obj.attachEvent("on"+evType, fn);
      return r;
    } else {
      return false;
    }
  }
}
life.addEvent(window,"load",life.init);

