// Create dice face arrays
aDice0 = new Array("T", "O", "E", "S", "S", "I");
aDice1 = new Array("A", "S", "P", "F", "F", "K");
aDice2 = new Array("N", "U", "I", "H", "M", "Qu");
aDice3 = new Array("O", "B", "J", "O", "A", "B");
aDice4 = new Array("L", "N", "H", "N", "R", "Z");
aDice5 = new Array("A", "H", "S", "P", "C", "O");
aDice6 = new Array("R", "Y", "V", "D", "E", "L");
aDice7 = new Array("I", "O", "T", "M", "U", "C");
aDice8 = new Array("L", "R", "E", "I", "X", "D");
aDice9 = new Array("T", "E", "R", "W", "H", "V");
aDice10 = new Array("T", "S", "T", "I", "Y", "D");
aDice11 = new Array("W", "N", "G", "E", "E", "H");
aDice12 = new Array("E", "R", "T", "T", "Y", "L");
aDice13 = new Array("O", "W", "T", "O", "A", "T");
aDice14 = new Array("A", "E", "A", "N", "E", "G");
aDice15 = new Array("E", "I", "U", "N", "E", "S");

// Create dice face array
aFace = new Array(15);

function shakeit() {
	// Select random face for each dice
	aFace[0] = aDice0[RandNo(6)];
	aFace[1] = aDice1[RandNo(6)];
	aFace[2] = aDice2[RandNo(6)];
	aFace[3] = aDice3[RandNo(6)];
	aFace[4] = aDice4[RandNo(6)];
	aFace[5] = aDice5[RandNo(6)];
	aFace[6] = aDice6[RandNo(6)];
	aFace[7] = aDice7[RandNo(6)];
	aFace[8] = aDice8[RandNo(6)];
	aFace[9] = aDice9[RandNo(6)];
	aFace[10] = aDice10[RandNo(6)];
	aFace[11] = aDice11[RandNo(6)];
	aFace[12] = aDice12[RandNo(6)];
	aFace[13] = aDice13[RandNo(6)];
	aFace[14] = aDice14[RandNo(6)];
	aFace[15] = aDice15[RandNo(6)];
	
	// Jumble the dice
	for (d = 0; d < 16; d++) {
		// Get a random number between 0 and 15
		newnumber = (Math.random() * 16);
		newnumber = parseInt(newnumber, 10);
		temp = aFace[d];
		aFace[d] = aFace[newnumber];
		aFace[newnumber] = temp;
	}
	
	// Update the dice table
	for(t = 0; t < 16; t++) {
		if (document.getElementById('dice'+t)) {
			document.getElementById('dice'+t).innerHTML = '<strong>'+ aFace[t] +'</strong>';
		}
	}
}

// Generate random integer between 0 and limit
function RandNo(iLimit) {
    var ranNum= Math.floor(Math.random() * iLimit);
    return ranNum;
}

function addshakecontrol() {
	var shakebutton = document.getElementById('shakebutton');
	if (shakebutton) {
		addEvent(shakebutton, "click", shakeit);
	}
}

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

window.onload = function() {
    shakeit();
	addshakecontrol();
}





