var width = 50;
var height = 50;
var maxGenerations = 50;

var left = 300;
var top = 100;

var cellwidth = 4;
var cellheight = 4;

var body;

var canvas;

var front = new Array(width);
var back = new Array(width);
var elements = new Array(width);

var bufCurrent;
var bufNext;

var alivecolor = "#0000FF";
var deadcolor = "#FFFFFF";

var showsFront = true;

var generation = 0;

function StartGame(){
  InitializeGame();

  CreateField();

  RandomizeFieldAndStartGame();
}

function DoSelection(){
    if(showsFront){
      bufCurrent = front;
      bufNext = back;
    }
    else{
      bufCurrent = back;
      bufNext = front;
    }
    
    var neigh;
    var ilimit = width-1;
    var jlimit = height-1;

    var im1;
    var ip1;
    var jm1;
    var jp1;
    
    /*for(var i=0; i<width; ++i){
      for(var j=0; j<height; ++j){
        neigh = 0;
        im1 = i-1;
        ip1 = i+1;
        jm1 = j-1;
        jp1 = j+1;

        if(i != 0){
            if(j != 0){
                if(bufCurrent[im1][jm1]){
                    neigh++;
                }
            }
            
            if(bufCurrent[im1][j]){
                neigh++;
            }
            
            if(j != jlimit){
                if(bufCurrent[im1][jp1]){
                    neigh++;
                }
            }
        }
        
        if(j != 0){
            if(bufCurrent[i][jm1]){
                neigh++;
            }
        }
        
        if(j != jlimit){
            if(bufCurrent[i][jp1]){
                neigh++;
            }
        }
        
        if(i != ilimit){
            if(j != 0){
                if(bufCurrent[ip1][jm1]){
                    neigh++;
                }
            }
            
            if(bufCurrent[ip1][j]){
                neigh++;
            }
            
            if(j != jlimit){
                if(bufCurrent[ip1][jp1]){
                    neigh++;
                }
            }
        }
        
        if(bufCurrent[i][j]){
            if(2 != neigh && 3 != neigh){
                bufNext[i][j] = false;
            }
            else{
                bufNext[i][j] = true;
            }
            
        }
        else{
            if(3 != neigh){
                bufNext[i][j] = false;
            }
            else{
                bufNext[i][j] = true;
            }
        }
      }
    }*/
    
    for(var i=1; i<ilimit; ++i){
      for(var j=1; j<jlimit; ++j){
        neigh = 0;
        im1 = i-1;
        ip1 = i+1;
        jm1 = j-1;
        jp1 = j+1;

        if(bufCurrent[im1][jm1]){
            neigh++;
        }
        
        if(bufCurrent[im1][j]){
            neigh++;
        }
        
        if(bufCurrent[im1][jp1]){
              neigh++;
        }
        
        if(bufCurrent[i][jm1]){
            neigh++;
        }
        
        if(bufCurrent[i][jp1]){
            neigh++;
        }
        
        if(bufCurrent[ip1][jm1]){
            neigh++;
        }
            
        if(bufCurrent[ip1][j]){
            neigh++;
        }
            
        if(bufCurrent[ip1][jp1]){
            neigh++;
        }
        
        if(bufCurrent[i][j]){
            if(2 != neigh && 3 != neigh){
                bufNext[i][j] = false;
            }
            else{
                bufNext[i][j] = true;
            }
            
        }
        else{
            if(3 != neigh){
                bufNext[i][j] = false;
            }
            else{
                bufNext[i][j] = true;
            }
        }
      }
    }
    
    for(var i=0; i<width; ++i){
        for(var j=0; j<height; ++j){
            if(bufNext[i][j]){
                elements[i][j].style.backgroundColor = alivecolor;
            }     
            else{
                elements[i][j].style.backgroundColor = deadcolor;
            }
        }
    }
    
    showsFront = !showsFront;
  
      
   ++generation;
   
   if(generation < maxGenerations-1){
    setTimeout("DoSelection()", 50);
   }
   else{
    generation = 0;
    showsFront = true;
    setTimeout("RandomizeFieldAndStartGame()", 1000);
   }
}

function InitializeGame(){
  body = document.getElementsByTagName("body")[0];

  canvas = document.createElement("div");
  canvas.style.position = "absolute";
  canvas.style.left = left + "px";
  canvas.style.top = top + "px";
  canvas.style.backgroundColor = "#FF0000";
  canvas.style.width = width + "px";
  canvas.style.height = height + "px";
  body.appendChild(canvas);

  for(var i=0; i<width; ++i){
    front[i] = new Array(height);
    back[i] = new Array(height);
    elements[i] = new Array(height);
  }
}

function RandomizeFieldAndStartGame(){
  for(var i=1; i<width-1; ++i){
    for(var j=1; j<height-1; ++j){
      if(Math.round(100*Math.random()) < 40){
        elements[i][j].style.backgroundColor = alivecolor;
        front[i][j] = true;
      }
      else{
        elements[i][j].style.backgroundColor = deadcolor;
        front[i][j] = false;
      }
    }
  }
  
  /*for(var i=1; i<width-1; ++i){
    for(var j=1; j<height-1; ++j){
      if(i%2 != 0){
        elements[i][j].style.backgroundColor = alivecolor;
        front[i][j] = true;
      }
      else{
        elements[i][j].style.backgroundColor = deadcolor;
        front[i][j] = false;
      }
    }
  }*/
  
  DoSelection();
}

function CreateField(){
  for(var i=0; i<width; ++i){
    for(var j=0; j<height; ++j){
      var div = document.createElement("div");
      elements[i][j] = div;
      elements[i][j].style.position = "absolute";
      elements[i][j].style.width = cellwidth + "px";
      elements[i][j].style.height = cellheight + "px";
      elements[i][j].style.left = i*cellwidth + "px";
      elements[i][j].style.top = j*cellheight + "px";
      elements[i][j].style.backgroundColor = deadcolor;

      canvas.appendChild(elements[i][j]);
    }
  }
}
