/* required: layout/scripts/core.js */

/*
Alternating row color script by Joost de Valk ( http://www.joostdevalk.nl/ ) to add alternating row classes to a table.
Copyright (c) 2006 Joost de Valk.
*/

addEvent(window, "load", alternateInit);

function alternateInit() {
	// Find all tables with class sortable and make them sortable
	if (!document.getElementsByTagName) return;
	tbls = document.getElementsByTagName("table");
	for (ti=0;ti<tbls.length;ti++) {
		thisTbl = tbls[ti];
		if (((' '+thisTbl.className+' ').indexOf("alternate_rows") != -1)) {
			alternate(thisTbl);
		}
	}
}

function alternate(table) {
	var classOdd = 'dark';
	var classEven = 'light';

	// Take object table and get all it's tbodies.
	var tableBodies = table.getElementsByTagName("tbody");
	// Loop through these tbodies
	for (var i = 0; i < tableBodies.length; i++) {
		// Take the tbody, and get all it's rows
		var tableRows = tableBodies[i].getElementsByTagName("tr");
		// Loop through these rows
		for (var j = 0; j < tableRows.length; j++) {
			// Check if j is even, and apply classes for both possible results
			if ( (j % 2) == 0  ) {
				if (tableRows[j].className == classOdd || !(tableRows[j].className.indexOf(classOdd) == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, classOdd, classEven);
				} else {
					tableRows[j].className += " " + classEven;
				}
			} else {
				if (tableRows[j].className == classEven || !(tableRows[j].className.indexOf(classEven) == -1) ) {
					tableRows[j].className = replace(tableRows[j].className, classEven, classOdd);
				}
				tableRows[j].className += " " + classOdd;
			}
		}
	}
}
