var width = 20;
var height = 20;
var gameoflife = new GameOfLife(width,height);

function GameOfLife(width,height){
	this.cells = new Array();
	this.width = width;
	this.height = height;
	this.initalized = false;
	this.timer = null;
	
	/* initialize */
	this.init = function(){
		var GameOfLife = document.getElementById("GameOfLife");
		
		/* node not found */
		if(!GameOfLife){
			alert("Node 'GameOfLife' not found. Please reload page.");
			return;
		}
		
		/* draw table */
		var table = document.createElement("table");
		
		GameOfLife.appendChild(table);
		
		/* create cells */
		for(var i=0;i<this.width;i++){
			this.cells[i] = new Array();
			var tr = document.createElement("tr");
			
			for(var j=0;j<this.height;j++){
				var td = document.createElement("td");
				this.cells[i][j] = new Cell(td);
				
				tr.appendChild(td);
			}
			
			GameOfLife.appendChild(tr);
		}
		
		/* initialization done */
		this.initalized = true;
		
		/* restart */
		this.restart();
	}
	
	/* restart */
	this.restart = function(){
		/* not initalized */
		if(!this.initalized){
			alert("GameOfLife is not initalized. Please reload page.");
			return;
		}
		
		/* stop timer */
		window.clearTimeout(this.timer);
		
		/* initalize cells */
		for(var i=0;i<this.width;i++){
			for(var j=0;j<this.height;j++){
				this.cells[i][j].init();
			}
		}
		
		/* start timer */
		this.startTimer();
	}
	
	/* pause */
	this.pause = function(){
		window.clearTimeout(this.timer);
		this.timer = null;
	}
	
	/* continue */
	this.continueGame = function(){
		if(!this.timer){
			this.startTimer();
		}
	}
	
	/* start timer */
	this.startTimer = function(){
		this.calculateNextGeneration();
		this.timer = window.setTimeout("gameoflife.startTimer();",200);
	}
	
	/* calculate next generation */
	this.calculateNextGeneration = function(){
		/* calculate neighbours */
		for(var i=0;i<this.width;i++){
			for(var j=0;j<this.height;j++){
				var neighbours = 0;
			
				/* above left */
				if(i-1>0 && j-1>0 && this.cells[i-1][j-1].isAlive()){
					neighbours++;
				}
				
				/* above middle */
				if(i-1>0 && this.cells[i-1][j].isAlive()){
					neighbours++;
				}
				
				/* above right */
				if(i-1>0 && j+1<this.width && this.cells[i-1][j+1].isAlive()){
					neighbours++;
				}
				
				/* left */
				if(j-1>0 && this.cells[i][j-1].isAlive()){
					neighbours++;
				}
				
				/* right */
				if(j+1<this.width && this.cells[i][j+1].isAlive()){
					neighbours++;
				}
				
				/* below left */
				if(i+1<this.height && j-1>0 && this.cells[i+1][j-1].isAlive()){
					neighbours++;
				}
				
				/* below middle */
				if(i+1<this.height && this.cells[i+1][j].isAlive()){
					neighbours++;
				}
				
				/* below right */
				if(i+1<this.height && j+1<this.width && this.cells[i+1][j+1].isAlive()){
					neighbours++;
				}
				
				this.cells[i][j].neighbours = neighbours;
			}
		}
		
		/* calculate lifes */
		for(var i=0;i<this.width;i++){
			for(var j=0;j<this.height;j++){
				var cell = this.cells[i][j];
				
				/* cell is alive */
				if(cell.isAlive()){
					/* die */
					if(cell.neighbours<2 || cell.neighbours>3){
						cell.die();
					/* live on */
					}else{
						cell.liveOn();
					}
				/* cell is dead */
				}else{
					/* become alive */
					if(cell.neighbours==3){
						cell.becomeAlive();
					}
				}
			}
		}
	}
}

function Cell(elem){
	this.elem = elem;
	this.generations = 0;
	this.neighbours = 0;
	
	/* initialize */
	this.init = function(){
		if(Math.random()>0.75){
			this.becomeAlive();
		}else{
			this.die();
		}
	}
	
	/* become alive */
	this.becomeAlive = function(){
		this.generations += 5;
		this.elem.innerHTML = "";
		this.elem.style.backgroundColor = "rgb(0,0,0)";
	}
	
	/* die */
	this.die = function(){
		this.generations = 0;
		this.elem.innerHTML = "";
		this.elem.style.backgroundColor = "rgb(255,255,255)";
	}
	
	/* live on */
	this.liveOn = function(){
		this.generations += 5;
		this.elem.style.backgroundColor = "rgb("+(this.generations)+","+(this.generations)+","+(this.generations)+")";

		/* going to die */
		if(this.generations>=175){
			this.elem.innerHTML = "<img src='../../../img/content/js/skull.gif' width='15' height='15' />";
		}else{
			this.elem.innerHTML = "";
		}

		/* die if too old */
		if(this.generations>200){
			this.die();
		}
	}
	
	/* is alive */
	this.isAlive = function(){
		if(this.generations>0){
			return true;
		}
		
		return false;
	}
}