var GoL={
init:function(n){
	this.n=n;
	this.run=false;
	this.cursorx=Math.floor(n/2);
	this.cursory=Math.floor(n/2);
	var i,j,s="<table style='width:100%; height:100%; border-collapse:collapse;border:1px solid #0b0;'>"
	for(i=0;i<n;i++) {
		s+="<tr>"
			for(j=0;j<n;j++) s+="<td id=gol"+i+"_"+j+" style='border:1px solid #0b0;'></td>";
		s+="</tr>";
	}
	s+="</table>";
	$('gol').style.padding=10+'px';
	this.cellsize=($('gol').style.width.substring(0,$('gol').style.width.length-2) - 20)/n-1;
	s+="<div id='golcursor' style='display:none; position:absolute; width:"+this.cellsize+"px; height:"+this.cellsize+"px; border:3px dashed #0f0;'></div>";
	$('gol').innerHTML=s;
},
start:function(){
	var self=this;
	$('gol').style.display="";
	x=parseInt($('gol').style.left.substring(0,$('gol').style.left.length-2));
	ttyx=parseInt($('tty').style.left.substring(0,$('gol').style.left.length-2));
	if(x<ttyx+410) {
		$('gol').style.left = (x+2)+'px';
		timer=setTimeout(function(){self.start();},10);
	}
	else {
		this.cursormoveto(this.cursorx,this.cursory);
		$('golcursor').style.display="";
	}
},
stop:function(){
	var self=this;
	$('gol').style.display="";
	x=parseInt($('gol').style.left.substring(0,$('gol').style.left.length-2));
	ttyx=parseInt($('tty').style.left.substring(0,$('gol').style.left.length-2));
	if(x>ttyx) {
		$('gol').style.left = (x-2)+'px';
		timer=setTimeout(function(){self.stop();},10);
	}
	else {
		$('golcursor').style.display="none";
		$('gol').style.display="none";
	}
},
flip:function() {
	var id='gol'+this.cursory+'_'+this.cursorx;
	$(id).style.backgroundColor=($(id).style.backgroundColor?"":"#0b0");
},
life:function(x,y) {
	return ($('gol'+x+'_'+y).style.backgroundColor?1:0);
},
next:function(){
	var self=this,n=this.n;
	var x,y,xm,ym,xp,yp,sum=0,moore=[];
	for(x=0;x<n;x++) for(y=0;y<n;y++) {
		xm=(x==0)?n-1:x-1; xp=(x==(n-1))?0:x+1;
		ym=(y==0)?n-1:y-1; yp=(y==(n-1))?0:y+1;
		moore[y*n+x]=this.life(xm,ym)+this.life(xm,y)+this.life(xm,yp)+this.life(x,ym)+this.life(x,yp)+this.life(xp,ym)+this.life(xp,y)+this.life(xp,yp);
	}
	for (x=0;x<n;x++) for (y=0;y<n;y++) $('gol'+x+'_'+y).style.backgroundColor=((moore[y*n+x]==3)||(this.life(x,y)&&(moore[y*n+x]==2)))?"#0b0":"";
	for(x=0;x<n;x++) for(y=0;y<n;y++) sum+=this.life(x,y);
	if(!sum) this.run=!this.run;
	if(this.run) timer=setTimeout(function(){self.next();},100);
},
togglerun:function() {
	this.run=!this.run;
	if (this.run) this.next();
},
cursormoveto:function(x,y){
	this.cursorx=x;
	this.cursory=y;
	$('golcursor').style.left=$('gol'+this.cursory+'_'+this.cursorx).offsetLeft+8+'px';
	$('golcursor').style.top=$('gol'+this.cursory+'_'+this.cursorx).offsetTop+8+'px';
},
cursorright:function() {
	this.cursormoveto(((this.cursorx==this.n-1)?0:this.cursorx+1),this.cursory);
},
cursorleft:function() {
	this.cursormoveto(((this.cursorx==0)?this.n-1:this.cursorx-1),this.cursory);
},
cursorup:function() {
	this.cursormoveto(this.cursorx,((this.cursory==0)?this.n-1:this.cursory-1));
},
cursordown:function() {
	this.cursormoveto(this.cursorx,((this.cursory==this.n-1)?0:this.cursory+1));
}
}