function changeTRs(atr, aclass) {
    atr.className = aclass;
    var trsinside=atr.getElementsByTagName('tr');
    for (var k=0; k<trsinside.length; k++) {
        trsinside[k].className=aclass;
    }
}

function tablecolored() {
    if (!document.getElementById || !document.createTextNode) return;
    oldclass = '';

    // go through all tables
    var tables=document.getElementsByTagName('table');
    for (var i=0; i<tables.length; i++) {
        if (tables[i].className!='colored' && tables[i].className!='ruler') continue;

        var colordark = true;

	// find all direct childnodes
        var childs=tables[i].childNodes;
        for (var j=0; j<childs.length; j++) {
	    tbody = false;
            if (childs[j].nodeName=='TBODY') {
	        tbody = childs[j];
	    	break;
	    }
        }

	// do we have a tbody? 
        if (tbody==false) continue;

        childs=tbody.childNodes;
        for (var j=0; j<childs.length; j++) {
            // find all TRs in the table
            if (childs[j].nodeName!='TR') continue;

            // don't color dividers...
            if (childs[j].className=='divider') continue;

            // class "same" stays the same color as before
            if (childs[j].className=='same') {
		colordark = !colordark;
	    }

	    if (childs[j].className!='' && childs[j].className!='same' &&
	        (childs[j].className!='colordark' || 
		 childs[j].className!='colorlight')) continue; 

            switch (tables[i].className) {
            case 'ruler': 
                if (colordark) changeTRs(childs[j], 'colordark');
                else           changeTRs(childs[j], 'colorlight');
                childs[j].onmouseover=function() { 
                    oldclass=this.className; 
                    changeTRs(this, 'ruled')
                    return false 
                }
                childs[j].onmouseout=function() { 
                    changeTRs(this, oldclass)
                    return false 
                }
                break;
            case 'colored': 
                if (colordark) changeTRs(childs[j], 'colordark');
                else           changeTRs(childs[j], 'colorlight');
		/* FIXME
                // change the first td background of each row
                var tds=childs[j].getElementsByTagName('td');
		if (tds.length<=0) continue;
                if (colordark) tds[0].className='colordark';
                else           tds[0].className='colorlight';
		*/
                break;
            } // end of switch (tables[i].className)

	    // switch color
	    colordark = !colordark;

        } // end of for (var j=0; j<childs.length; j++)
    } // end of for (var i=0; i<tables.length; i++)
}

// window.onload=function(){tableruler();}
window.onload = function() { tablecolored(); }
