var currRow = 0;
	var currCell = 0;
	var numCols=28;
	var numRows=28;
	var ourInterval;
	var cellColor;
	var arr = new Array(0);
	var size = "7px";	
	
	function buildDom(){
		var panel=document.getElementById("panel");
	
		var table=document.createElement("table");
		//table.style.width = "100%";
		//table.style.height = "100%";
		panel.appendChild(table);
	
		var tbody=document.createElement("tbody");
		table.appendChild(tbody);
		
		for (i=0; i<numRows; i++) {
			var row=document.createElement("tr");
			tbody.appendChild(row);
			
			for (j=0; j<numCols; j++) {
				var cell=document.createElement("td");
				row.appendChild(cell);
								
				cell.style.width= size;
				cell.style.height= size;

				var color = "white";
				
				cell.style.backgroundColor= color;
				var cellText = document.createTextNode(" ");
                cell.appendChild(cellText);
                cell.style.color="red";
                var id = i +":"+j;
                cell.id = id;
			}
		}
		cellColor = pickAColor();
		createWordArray();
		ourInterval = setInterval("randomChange()", 10);
	}
	
	function pickAColor() {
		
      var array = new Array ("f", "e", "d", "c", "b", "a", "9", "8", "7", "6", 
  						"5", "4", "3", "2", "1", "0"  );  // array of possible hex values.
      var endHex = "#";  // this is the hex color that will be returned

      for (   var i = 0; i < 6; i++   )   {  // loop 6 times...
            endHex += array[Math.floor(Math.random() * array.length)];  // and each time add a new character to the returned color.
      }
      return endHex;

	}

	function changeColor(rowNum, cellNum) {
		var cellId = rowNum +":"+ cellNum;
		var cell = document.getElementById(cellId);
		cell.style.backgroundColor=cellColor;
		
	}
	
	function createArray() {
	
		currRow = 0;
		currCell = 0;
		var cells = numCols*numRows;
	
		for (i=0; i<cells; i++){ 
		
			var value = currRow +":"+ currCell;
			arr.splice(i,0,value);
			currCell++
			if (currCell==numCols){
			currCell = 0;
			currRow++;
	    	}	
	    }
	}	
	
	function createWordArray() {

		arr = new Array(

			// the letter "m"
			"2:1", "3:1", "4:1", "5:1", "6:1", "1:2", 
			"2:2", "1:3", "1:4", "2:4", "3:4", "4:4", 
			"1:5", "1:6", "2:6", "2:7", "3:7", "4:7", 
			"5:7", "6:7",

			// the letter "o"
			"2:9", "3:9", "4:9", "5:9", "1:10", "6:10",
			"1:11", "6:11", "1:12", "6:12", "2:13", "3:13",
			"4:13", "5:13",

			// the letter "n"
			"2:15", "3:15", "4:15", "5:15", "6:15", "1:16",
			"1:17", "1:18", "2:19", "3:19", "4:19", "5:19",
			"6:19",

			// the letter "a"
			"2:21", "3:21", "4:21", "5:21", "1:22", "6:22",
			"1:23", "6:23", "1:24", "2:25", "3:25",
			"4:25", "5:25", "6:25", "6:26",

			// the letter "a"
			"12:4", "13:4", "14:4", "15:4", "11:5", "16:5",
			"11:6", "16:6", "11:7", "12:8", "13:8", "14:8",
			"15:8", "16:8", "16:9",

			// the letter "n"
			"12:11", "13:11", "14:11", "15:11", "16:11", "11:12",
			"11:13", "11:14", "12:15", "13:15", "14:15", "15:15",
			"16:15",

			// the letter "d"
			"12:17", "13:17", "14:17", "15:17", "11:18", "16:18",
			"11:19", "16:19", "12:20", "9:21", "10:21", "11:21",
			"12:21", "13:21", "14:21", "15:21", "16:21", "16:22",

			// the letter "c"
			"22:1", "23:1", "24:1", "25:1", "21:2", "26:2",
			"21:3", "26:3", "21:4", "26:4", "22:5", "25:5",

			// the letter "h"
			"19:7", "20:7", "21:7", "22:7", "23:7", "24:7", 
			"25:7", "26:7", "21:8", "21:9", "21:10", "22:11",
			"23:11", "24:11", "25:11", "26:11",

			// the letter "r"
			"21:13", "22:13", "23:13", "24:13", "25:13", "26:13",
			"22:14", "21:15", "21:16", "22:17",

			// the letter "i"
			"19:19", "21:19", "22:19", "23:19", "24:19",
			"25:19", "26:19",

			// the letter "s"
			"22:21", "23:21", "26:21", "21:22", "23:22", "26:22", 
			"21:23", "23:23", "24:23", "26:23", "21:24", "24:24",
			"26:24", "21:25", "24:25", "25:25"

			);

	}	

	function randomChange() {		
		
		var randomKey = Math.floor(Math.random()* arr.length);
		var randomValue = arr[randomKey];
		var randomRowNum = extractRandomRowNum(randomValue);
		var randomCellNum = extractRandomCellNum(randomValue);
		changeColor(randomRowNum, randomCellNum);
		arr.splice(randomKey,1);
		if (arr.length == 0) {
			cellColor = pickAColor();
			createWordArray();
		}
	}
	
	function extractRandomRowNum(randomValue){
	
		var index = randomValue.indexOf(":");
		var rowNum = randomValue.substring(0,index);
		return rowNum;
	
	}
	
	function extractRandomCellNum(randomValue){
	
		var index = randomValue.indexOf(":");
		var cellNum = randomValue.substring(index+1);
		return cellNum;
	
	}
