/* ================== */
/* kdi/www/js/home.js */
/* pkoSolutions       */
/* ====================/
init()
animate()
hideSpans()
showSpan(index)
fisherYates(myArray)
debug()
*/

// Event-Handling
//window.onload = init;
addLoadEvent(init);


// Variablen
var delayMS = 1000;
var showALL = 3;
var counter;
var countSpans;
var spans;
var indexes;
var root;

// Initiaisierung
function init() {
	counter = 0;
	countSpans = 0;
	spans = new Array();
	indexes = new Array();
	root = document.getElementById("home");
	if (root != null) {
		spans = this.root.getElementsByTagName("span");
		countSpans = spans.length;
		counter = countSpans + showALL;
	//hideSpans();
		for (var i = 0; i < countSpans; i++)
			indexes[i] = i;
		setInterval(animate, delayMS);
	}
//debug();
}

// Anmation
function animate() {
//alert("animate(" + counter + ")");
	if (counter == 0) {
		hideSpans();
		fisherYates(this.indexes);
	}
	if (counter >= 1
		&& counter <= countSpans) {
		showSpan(counter - 1);
	}
	counter++;
	if (counter >= countSpans + showALL) {
		counter = 0;
	}
}

// Texte verstecken
function hideSpans() {
//alert("hideSpans()");
	for (var i = 0; i < countSpans; i++)
		spans[i].style.visibility = "hidden";
}

// einen Text anzeigen
function showSpan(index) {
//alert("showSpan(" + index + ")");
	spans[indexes[index]].style.visibility = "visible";
}

// die Karten mischen
function fisherYates(myArray) {
	var i = myArray.length;
	if (i == 0)
		return false;
	while (--i) {
		var j = Math.floor(Math.random() * (i + 1));
		var tempi = myArray[i];
		var tempj = myArray[j];
		myArray[i] = tempj;
		myArray[j] = tempi;
	}
}

// Debug-Funktion
function debug() {
	var html = "";
	var i;
	
	if (root == null) {
		alert("root not found!");
		return false;
	}
	
	html += root.nodeName;
	html += "\n";
	html += countSpans;
	if (spans != null) {
		for (i = 0; i < countSpans; i++ ) {
			j = indexes[i];
			html += "\n";
			html += spans[j].nodeName
				+  ", " + spans[j].firstChild.nodeType
				+  ", " + spans[j].firstChild.nodeValue;
		}
		alert(html);
	}
}
