	var cycleTimer;
        var N = 8;
        var cellColor = '';
        var cellNumber;
        var cellTransProb = 1;
        var cellTransTotal = 1;
        var colorNumber;
        var graphOutput;
        var tableWidth = 560;
	var myRandomString;
        var numTransOut;
        var numTransIn;
	var columnNum  = new Array(16);
	var myArray = new Array(N);
        for (x=0;x<N;x++) {
           myArray[x] = new Array(N);
        } 
	var myNorm = new Array(N);
        for (x=0;x<N;x++) {
           myNorm[x] = new Array(N);
        } 
	var myTransOut = new Array(N);
        for (x=0;x<N;x++) {
           myTransOut[x] = new Array(N);
        } 
	var myTransIn = new Array(N);
        for (x=0;x<N;x++) {
           myTransIn[x] = new Array(N);
        } 
	
	var myTransProb = new Array(N);
        for (x=0;x<N;x++) {
          myTransProb[x] = new Array(N);
          for (y=0;y<N;y++) {
            myTransProb[x][y] = new Array(N);
	    for (z=0;z<N;z++) {
              myTransProb[x][y][z] = new Array(N);
	    }
	  }
        } 
	
	var myColor = new Array(16);
        myColor[16] = '#0000FF';
        myColor[15] = '#0000FF';
        myColor[14] = '#1111FF';
        myColor[13] = '#2222FF';
        myColor[12] = '#3333FF';
        myColor[11] = '#4444FF';
        myColor[10] = '#5555FF';
        myColor[9] = '#6666FF';
        myColor[8] = '#7777FF';
        myColor[7] = '#8888FF';
        myColor[6] = '#9999FF';
        myColor[5] = '#AAAAFF';
        myColor[4] = '#BBBBFF';
        myColor[3] = '#CCCCFF';
        myColor[2] = '#DDDDFF';
        myColor[1] = '#EEEEFF';
        myColor[0] = '#E8E8E8';
 
	var initNumber = 1024;
	var transProb = .5;
	var totalCycles = 0;
	var prevTotal = 0;
	var colStep = 0;
	var maxEvents = 0;

	function transitions () {
		myvar = 0;
		prevTotal = maxEvents;
		HTMLoutput = '';
		numoutput = '';
                var numTrans;
		
		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
                                  myTransOut[x][y] = 0;
                                  myTransIn[x][y] =  0;
// alert("Before TransIn, TransOut, myArray[" + x + "][" + y + "] = " + myArray[x][y]);                  
                        }
                }
		
		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
 		              for (z=0;z<N;z++) {
			         for (w=0;w<N;w++) {
				  if (!((x == z) && (y == w))) {
// alert("myArray[" + x + "][" + y + "] = " + myArray[x][y]); 
                                    myRandom = Math.random();
//			            myRandomString += myRandom.toFixed(3) + '<br/>';
                                    if (Math.random() < .03) {	  
					   numTransOut = myArray[x][y] * transProb * myTransProb[x][y][z][w];
					   numTransIn = myArray[x][y] * transProb * myTransProb[x][y][z][w];
					   
					   
					 
					   myTransOut[x][y] =  myTransOut[x][y] + numTransOut;
					   myTransIn[z][w] =  myTransIn[z][w] + numTransOut;
//alert("myTransOut[" + x + "][" + y +  "] = " + myTransOut[x][y]);
//alert("myTransIn[" + z + "][" + w + "] = " + myTransIn[z][w]);
                                    }
				  }
                                 }
                              }
                        }
                  }
//alert("After TransIn, TransOut");

		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
                              myArray[x][y] =  myArray[x][y] - myTransOut[x][y] + myTransIn[x][y];
//alert("After trans, myArray[" + x + "][" + y + "] = " + myArray[x][y]);

                       }
                }
		
		displayOutput();

		totalCycles++;
		document.getElementById('cycles').innerHTML = totalCycles+' total cycles';

		if (totalCycles < maxCycles) {
		    cycleTimer = setTimeout('transitions();',64,true);
		}
	}


        function displayOutput () {
		
                HTMLoutput = '<table style="border: 1px solid black;" width="480" height="480" border="0" cellpadding="1" cellspacing="0">';

		for (x=0;x<N;x++) {
                        HTMLoutput += '<tr>';
			for (y=0;y<N;y++) {
//alert("myArray[" + x + "][" + y + "] = " + myArray[x][y]);
                           cellNumber = Math.round(myArray[x][y]);
                           colorNumber = cellNumber;
//alert("cellNumber(" + x + "][" + y + ") = " + cellNumber);
                           if (colorNumber > 16) {
                             colorNumber = 16;
                           } 
                           cellColor = myColor[colorNumber];
//alert("cellColor = " + cellColor);
		  
			   HTMLoutput += '<td><div style="height:' + tableWidth/N + 'px; width:' + tableWidth/N + 'px; background-color:' + cellColor + ';"><font size="5">&nbsp;' + cellNumber + '</font><br/><br/>';
/*
			   cellTransTotal = 0;
 		              for (z=0;z<N;z++) {
			         for (w=0;w<N;w++) {
                                  cellTransProb = myTransProb[x][y][z][w];
			          HTMLoutput +=  cellTransProb.toFixed(2) + '&nbsp;';
				  cellTransTotal += cellTransProb; 
				 }
				 HTMLoutput +=  '<br/>';
				 
			      } 

			      HTMLoutput += '<br/>' + cellTransTotal.toFixed(3); 			   
*/
			      
			      HTMLoutput += '</div></td>';
//alert(HTMLoutput);	
			}
                        HTMLoutput += '</tr>';
		}

                HTMLoutput += '</table>';
//alert("HTMLoutput complete = " + HTMLoutput);	
                
		document.getElementById('dist').innerHTML = HTMLoutput;
// now graph the frequencies

		for (i=0;i<17;i++) {
		  columnNum [i] = 0;
		  for (x=0;x<N;x++) {
		     for (y=0;y<N;y++) {
			   if (Math.floor(myArray [x][y]) == i) {
			     columnNum [i] += 1;
			   }
	             }  
	          }
		}
		
		HTMLoutput = '<table align="left" width="64" height="128" border="0" cellpadding="0" cellspacing="0"><tr>';
		
		for (i=1;i<17;i++) {
		   HTMLoutput += '<td valign="bottom"><div style="height:' + columnNum [i] * 4 + 'px; width:6px; background-color: #666699;">&nbsp;</div></td>';
		}
		
		HTMLoutput += '</tr></table>'
		document.getElementById('graph').innerHTML = HTMLoutput;
	}

	function initNumbers() {
                initNumber = document.getElementById('number').value;
                if (initNumber == 0) {
                  initNumber = 1024;
                }
// alert("initNumber = " + initNumber);


	        for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
			    myArray[x][y] = 0;
// alert("Init myArray[" + x + "][" + y + "] = " + myArray[x][y]);
                        }
		}		

             myArray[0][0] = initNumber;

		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
                                  myTransOut[x][y] = 0;
                                  myTransIn[x][y] =  0;
// alert("End initNumbers myArray[" + x + "][" + y + "] = " + myArray[x][y]);                  
                        }
                }


                transProb = document.getElementById('transProb').value;
                if ((transProb > 1) || (transProb < 0))  {
                  transProb = .5;
                }     
// alert("transProb = " + transProb);

                totalCycles = 0;

                
// calculate normalization		
		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
                           myNorm[x][y] = 0; 
 		           for (z=0;z<N;z++) {
			      for (w=0;w<N;w++) {
                               if (!((x == z) && (y == w))) {
				  myNorm[x][y] = myNorm[x][y] +  (1/Math.sqrt((x-z)*(x-z) + (y-w)*(y-w)));
				  
                                  //myNorm[x][y] = myNorm[x][y] + transProb;
//alert("myNorm[" + x + "][" + y + "] = " + myNorm[x][y]);  
				}
                              }  
                            } 
                        } 
                }

// calculate transition probabilities		
		for (x=0;x<N;x++) {
			for (y=0;y<N;y++) {
 		              for (z=0;z<N;z++) {
			         for (w=0;w<N;w++) {
				  if (!((x == z) && (y == w))) {
				      
				    myTransProb[x][y][z][w] = transProb / (N * N - 1)
				    
				    
				    // (myNorm[x][y] * Math.sqrt( (x-z)*(x-z) + (y-w)*(y-w) ) );
				    // myTransProb[x][y][z][w] = transProb / myNorm[x][y];
				   
// alert("sqrt("  + x + "," + y +  ";"   + z + "," + w + ")  = " + Math.sqrt( (x-z)*(x-z) + (y-w)*(y-w) ));

//alert("myTransProb[" + x + "," + y +  ";"  + z + "," + w + "]  = " + myTransProb[x][y][z][w]);
				  } else { myTransProb[x][y][z][w] = 1 - transProb;
					  
			          }
                                 }
                              }
                        }
                  }

                
		document.getElementById('dist').innerHTML = HTMLoutput;
		displayOutput();
                // setTimeout('', 6000, true);
		
		maxCycles = document.getElementById('events').value;
		cycleTimer = setTimeout('transitions();', 1500, true);

	}
	
	function stopTransitions() {
//               HTMLoutput = myRandomString;
//	       document.getElementById('dist').innerHTML = HTMLoutput;
		
		clearTimeout(cycleTimer);
	}