
var otherPt = 0;
var cellData = [];    
var rules = [];
var cellHeight = 10;

var sameCount = 0;
var madeChange = false;

var screenWidth = 1000;
function main()
{    

//    alert (Math.random() > 0.5);
    randomizeCA();
        
    setInterval ("timeStep()",100);
    setInterval ("randomizeCA()",10000);
  //  alert (document.getElementById ('pic'));
}

function drawLine()
{
    var canvas = document.getElementById('ca');
    var ctx = null;

    if (ctx = canvas.getContext('2d'))
    {
        ctx.fillStyle = "rgb(255,255,255)";
        ctx.fillRect (0,0,screenWidth,screenWidth);
        
        ctx.fillStyle = "rgb(0 ,0,100)";           
//        drawCircle (ctx,Math.random() * 500,Math.random() * 500);
        ctx.fillRect (Math.random()*screenWidth, Math.random() * 500, 30, 30);
//        ctx.drawImage (ctx,
    }
}

function randomizeCA()
{
    for (var x = 0; x < 100; x++)
        cellData[x] = Math.random() > 0.5 ? 1 : 0;
    
    for (var x = 0; x < 8; x++)
        rules[x] = Math.random() > 0.5 ? 1 : 0;
}

function drawCircle (ctx,shapeX, shapeY)
{    
    var origX;
    var origY;
    
    for (var sideIndex = 0; sideIndex < 30; sideIndex++) {
        var angle = (((Math.PI * 2.0) * sideIndex) / 30);
        var tx = Math.cos(angle);
        var ty = Math.sin(angle);
        
        if (sideIndex == 0)
        {
            origX = tx * 5;
            origY = ty * 5;

            ctx.moveTo (origX + shapeX, origY + shapeY);
        }
        else
            ctx.lineTo (tx * 5+shapeX,ty * 5  + shapeY);
    }
    
    ctx.lineTo(origX + shapeX,origY + shapeY);
    ctx.stroke();
}

function drawNextStep()
{
    var canvas = document.getElementById('ca');
    var ctx = null;

    if (ctx = canvas.getContext('2d'))
    {
        /*ctx.fillStyle = "rgb(255,255,255)";
        ctx.fillRect (0,0,500,500);*/
        
        ctx.fillStyle = "rgba(0,0,0,0.05)";
        ctx.fillRect(0,0,screenWidth, 500);
        
        ctx.drawImage (canvas,0,cellHeight,screenWidth,500-cellHeight,0,0,screenWidth,500-cellHeight);
        
        ctx.fillStyle = "rgb(0,0,0)";
        ctx.fillRect (0,500-cellHeight,screenWidth,cellHeight);
        
        ctx.fillStyle = "rgb(0,255,0)";
        
        madeChange = false;
        
        for (var x = 0; x < cellData.length; x++)
        {
            var newVal;
        
            if (rules[getCellValue(x-1) + 2 * getCellValue(x) + 4 * getCellValue (x+1)] == 1)
            {
                newVal = 1;
                ctx.fillRect (x*(cellHeight+2),500-cellHeight,cellHeight,cellHeight);
            }
            else
                newVal = 0;     
                
            if (cellData[x] != newVal) madeChange = true;    
            
            cellData[x] = newVal;
        }                
        
        if (!madeChange) sameCount ++;
        
        if (sameCount > 15)
        {
            randomizeCA();
            sameCount = 0;
        }
        
    }
}

function getCellValue(num)
{
    return cellData[getCell(num)];
}

function getCell(num)
{
    return num % cellData.length;
}

function timeStep()
{
    otherPt += 1;   
    drawNextStep();
    
}
