function init(){
clock();
setInterval(clock,1000);
//drawRule(30);
//drawSun();
//setInterval(drawSun,50);
}

function clock(){
var now = new Date();
var size = 1;
var ctx = document.getElementById('clock').getContext('2d');
ctx.save();
ctx.clearRect(0,0,300*size,300*size);
ctx.translate(150*size,150*size);
ctx.scale(size,size);
ctx.rotate(-Math.PI/2);
ctx.strokeStyle = "black";
ctx.fillStyle = "black";
ctx.lineWidth = 8;
ctx.lineCap = "butt";
//ctx.lineCap = "square";
//ctx.lineCap = "round";

// background
ctx.beginPath();
ctx.lineWidth = 2;
ctx.fillStyle = "deepskyblue";	
ctx.arc(0,0,120,0,Math.PI*2,true);
ctx.fill();


// Hour marks
ctx.fillStyle = "black";
ctx.save();
for (i=0;i<12;i++){
//ctx.lineWidth = 2;
ctx.beginPath();

//ctx.translate(56,0);
ctx.rotate(Math.PI/6);
ctx.arc(108,0,6,0,Math.PI*2,true);
	
//ctx.moveTo(100,0);
//ctx.lineTo(120,0);
//ctx.stroke();
ctx.fill();	
}
	
ctx.restore();

// Minute marks
ctx.save();
ctx.lineWidth = 1;
for (i=0;i<60;i++){
if (i%5!=0) {
ctx.beginPath();
ctx.moveTo(117,0);
ctx.lineTo(120,0);
//ctx.stroke();
}
ctx.rotate(Math.PI/30);
}
ctx.restore();

var sec = now.getSeconds();
var min = now.getMinutes();
var hr= now.getHours();
hr = hr>=12 ? hr-12 : hr;

ctx.fillStyle = "black";

// write Hours
ctx.save();
ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )
ctx.strokeStyle = "black";
ctx.lineWidth = 14;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(80,0);
ctx.stroke();
ctx.strokeStyle = "white";
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(80,0);
ctx.stroke();
ctx.restore();

// write Minutes
ctx.save();
ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )
ctx.strokeStyle = "black";
ctx.lineWidth = 14;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(110,0);
ctx.stroke();
ctx.strokeStyle = "white";
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(110,0);
ctx.stroke();
ctx.restore();

// Write seconds
ctx.save();
ctx.rotate(sec * Math.PI/30);
ctx.strokeStyle = "#FF3300";
ctx.fillStyle = "#FF3300";
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(120,0);
ctx.stroke();
ctx.beginPath();
ctx.arc(0,0,10,0,Math.PI*2,true);
ctx.fill();
ctx.beginPath();
//ctx.arc(95,0,10,0,Math.PI*2,true);
ctx.stroke();
ctx.fillStyle = "#555";
//ctx.arc(0,0,3,0,Math.PI*2,true);
ctx.fill();
ctx.restore();

ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = '#000000';
ctx.arc(0,0,120,0,Math.PI*2,true);
//ctx.stroke();
ctx.restore();
}

// ******************** cellular automata code here ***************************

function initAutomata(){
drawRule(45);	
}

function drawRule(rule) {
// i like resolution=1 but it kills firefox
var resolution = 2;
var canvas = document.getElementById('automata');	
var curRow = [];
var autoctx = canvas.getContext('2d');
autoctx.clearRect(0,0,550,550);
	
autoctx.fillStyle = 'yellow'
autoctx.fillRect(0,0,275,275);
autoctx.fillStyle = '#FF3300'
autoctx.fillRect(275,0,275,275);
autoctx.fillStyle = 'deepskyblue'
autoctx.fillRect(0,275,275,275);
autoctx.fillStyle = 'white'
autoctx.fillRect(275,275,275,275);
autoctx.fillStyle = 'black'

	
for (var i=0; i<550/resolution; i++) curRow.push(0);
curRow[275] = 1;
for (var y=0; y<550/resolution; y++) {
// first do the drawing
for (var x=0; x<curRow.length; x++) {
if (curRow[x]) autoctx.fillRect(x*resolution,y*resolution,resolution,resolution);
}
var nextRow = [];
// compute the next row
for (var x=0; x<curRow.length; x++) {
var c=0;
if (x>0 && curRow[x-1]) c+=4;
if (curRow[x]) c+=2;
if ((x+1)<curRow.length && curRow[x+1]) c+=1;
nextRow.push( Math.pow(2,c) & rule );
}
curRow = nextRow;
}
}

// **************************** sunspot code *********************************

var mover = 0;

function initSun(){
drawSun();
setInterval(drawSun,50);
}
function drawSun() {
var sunctx = document.getElementById('sunspot').getContext('2d');
sunctx.fillStyle = 'deepskyblue'
sunctx.fillRect(0,0,550,400);
sunctx.save();
sunctx.translate(275,200);
sunctx.scale(3.5,2.5);
for (i=1;i<6;i++){
sunctx.save();
sunctx.fillStyle = 'rgb(255,'+Math.abs(306-51*i)+',0)';
for (j=0;j<i*6;j++){
sunctx.rotate(Math.PI*2/(i*6)+ (mover/(i+j)));
sunctx.save();
sunctx.scale(1/i,1/i);
//sunctx.scale(i/10,i/10);
//sunctx.scale(1/j,1/j);

sunctx.beginPath();
sunctx.arc(0,i*50,20,0,Math.PI*2,true);
sunctx.fill();
sunctx.restore();
}
sunctx.restore();
}
sunctx.restore();
mover=mover+0.03;	
}


window.onload = function(e){init();initSun();initAutomata();}