var words = 
 [ 
     [ "Multilateral", "Correction", "Mechanism" ],
     [ "Interactive", "Development", "Procedure" ],
     [ "Comprehensive", "Reaction", "Capability" ],
     [ "Proactive", "Sovereignty", "Deployment" ],
     [ "Worldwide", "Preemption", "Adjustment" ],
     [ "Cross-cultural", "Resolution", "Process" ],
     [ "Transnational", "Diversity", "Revision" ],
     [ "Progressive", "Cooperation", "Facility" ],
     [ "Universal", "Consensus", "Program" ],
     [ "Un-Bush-like", "Totally-not-wimpy", "Solution" ],
     [ "Transformative", "Hope", "Changes" ],
     [ "Heavenly", "Fiendish", "Scripture" ],
     [ "Overseas", "Contingency", "Operation"]
  ];
var NUMWORDS = words.length;

var delays = [ 2, 2, 0, 0, 0, 60, 10, 10, 10 ];
var blink = [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 ];
var sounds = [ "crowdlaughapplause.wav", 
                   "APPLAUSE.au", 
                   "boohiss.wav",
                   "chant.wav",
                   "donkey.wav" ];
var colors = [ "green",
               "navy",
               "teal",
               "purple",
               "gray",
               "maroon"
               ];

var useSound;
var lastix = 0;
var sp = null;
var loopcnt = 0;
var spincnt = 0;
var grey = "#333333";
var tickArg = -1;

var sound2Embed = null;

function stopSound() {
	  if ( sound2Embed && !sound2Embed.removed ) {
	    window.document.body.removeChild(sound2Embed);
	    sound2Embed.removed = true;
	  }
	}
function playSound(sound) {
  if ( !sound2Embed ) {
    sound2Embed = document.createElement("embed");
    sound2Embed.setAttribute("src", sound);
    sound2Embed.setAttribute("hidden", true);
    sound2Embed.setAttribute("autostart", true);
  } else {
	  stopSound();
  }
  sound2Embed.removed = false;
  document.body.appendChild(sound2Embed);
}


function log(txt){
    var newtext = document.createTextNode(txt);
    var div = document.getElementById("log");
    div.appendChild(newtext);
    div.appendChild(document.createElement("br"));
  } 
function setSpan(ix, back, col) {
	var iid = "A_" + ix;	
	var ssp = window.document.getElementById(iid);
	if ( null === ssp ) {
		return;
	}
	ssp.style.textDecoration = "underline";
	ssp.style.backgroundColor = back;
	ssp.style.color = col;
	ssp.style.fontSize = 28 + "px";
	ssp.style.textDecoration = "none";
}
function hideSpan(ix) {
	var iid = "A_" + ix;	
	var ssp = document.getElementById(iid);
	ssp.style.visibility = 'hidden';
}
function cleanit(ix) {
	//log("cleanit(" + ix + ")");
	setSpan(ix, grey, "white");
}
function doTimeout(fun, ix, msec) {
	//alert("doTimeout(," + ix + "," + msec + ") tickArg:" + tickArg);
	tickArg = ix;
	setTimeout(fun, 100);
}

function setupLoop(ix) {
	var iid = "A_" + ix;
	var x;
	var sf;
	lastix = ix;
	sp = document.getElementById(iid);
	sp.style.backgroundColor = grey;
	sp.style.color = "black";
	if ( blink[ix]) {
		sp.style.color = "#FF8888";
		sp.style.textDecoration = "blink";
		sf = sounds[Math.floor(Math.random()*sounds.length)];
		//log("sf:" + sf);
		if (useSound) {
			playSound(sf);
		}
	} else {
		stopSound();
	}
	spincnt = delays[ix];
	if ( spincnt < 1 ) {
		spincnt = Math.floor(Math.random()*10) + 5; 
	}
	loopcnt = spincnt;
    //log("loopcnt = " + loopcnt + " for " + iid + "\n");
}

function tick() {
	var ix = tickArg;
	var zz = Math.floor(Math.random()*NUMWORDS);
	if ( ix >= 2 && ix <= 4 ) {
		//log("NW:" + NUMWORDS + "  zz:" + zz + "\n");
		var s = words[zz%NUMWORDS][lastix-2] + " ";
		sp.innerHTML = s;
		sp.style.fontSize = (32 - loopcnt*2) + "px";
		sp.style.color = colors[Math.floor(Math.random()*colors.length)];
	}
	if ( loopcnt-- <= 0 ) {
		sp.style.backgroundColor = "red";
		sp.style.color = "white";
		sp.style.textDecoration = "blink";
		if ( ++ix > 5 ) {
			cleanit(5);
			stopSound();
			return;
		}
		setupLoop(ix);
		if ( ix > 1 ) {
			cleanit(ix - 1);
		}
	}
	doTimeout(tick, ix, 100);
}


function spinIt() {
	var ix;
	var url = window.location.href;
	useSound = url.indexOf("?sound=on", 0) > -1;
	setupLoop(1);
	for ( ix = 1 ; ix < 8 ; ix++ ) {
		setSpan(ix, grey, grey);
	}
	
	setupLoop(1);
	doTimeout(tick, 1,100);


//	putWord(1);
//	putWord(2);
}
