/**
 * Checkerboard table highlighting
 * @version 1.0
 *
 * Based on Zebra Tables <http://www.alistapart.com/articles/zebratables/>
 *
 * @author David F. Miller <http://www.fivevoltlogic.com/> 05/03/2004
 * @author Andrew Ramsden <http://tinymac.com/> 12/03/2006
 */
function highlight(class) {
	
	// Should we tint columns also?
	// must be false by default, otherwise will always be changed to true
	var tintColumns = arguments[1] ? arguments[1] : false;
	
	// if arguments are provided to specify the colours
	// of the even & odd rows, then use the them;
	// otherwise use the following defaults:
	var evenRowClass = arguments[2] ? arguments[2] : "even-row";
	var evenColClass = arguments[3] ? arguments[3] : "even-col";
	
	// obtain a reference to the desired table
	// if no such table exists, abort
	var tables = getElementsByClass(class,'table');
	for(n=0; n < tables.length; n++) {
		table = tables[n];
		if (! table) { return; }
		
		// the flag we'll use to keep track of 
		// whether the current row is odd or even
		var evenRow = false;
		var evenCol = false;
		
		// by definition, tables can have more than one tbody
		// element, so we'll have to get the list of child
		// &lt;tbody&gt;s 
		var tbodies = table.getElementsByTagName("tbody");
		
		// and iterate through them...
		for (var h = 0; h < tbodies.length; h++) {
			
			
			// positions (used in rowspan compensation)
			// Should only be reset in between <tbody>s
			// 
			// keys represent column numbers
			// values array[0] represent number of rowspans left for that position
			//        array[1] represent the number of colspans on that rowspan
			positions = {
				0: [0,0],
				1: [0,0],
				2: [0,0]
			};
					
			// find all the &lt;tr&gt; elements... 
			var trs = tbodies[h].getElementsByTagName("tr");
			
			// ... and iterate through them
			for (var i = 0; i < trs.length; i++) {
			
				
				if (evenRow) {
					addClass(trs[i], evenRowClass);
				} else {
					removeClass(trs[i], evenRowClass);
				}
				
				if (tintColumns) { 
					evenCol = false;
					
					
					
					// get all the cells in this row...
					var tds = trs[i].getElementsByTagName("td");
					
					// marks the current column position
					currentPosition = 0;
					
					// and iterate through them...
					for (var j = 0; j < tds.length; j++) {
						
						// var mytd = tds[j];
						
						// compensate for rowspans
						if (
							positions[currentPosition] && 
							positions[currentPosition][0] && 
							positions[currentPosition][0] > 1
						) {
							// decrement the rowspans left at this position
							positions[currentPosition][0]--;
							
							//alert('rowspan before: '+tds[j].innerHTML);
							//alert('cols spanned by that rowspan = '+positions[currentPosition][1]);
							colSpans =positions[currentPosition][1];
							//alert(colSpans);
							
							// swap the class, same number of times as colspans for this rowspan
							//positions[currentPosition][1] = positions[currentPosition][1] || 1;
							
							for (m=0;m<colSpans; m++) {
								evenCol =  ! evenCol;
								currentPosition++; // were now an extra col along
							}
						}
						
						// detect and record rowspans
						rowSpan = tds[j].rowSpan ? parseInt(tds[j].rowSpan) : 1 ;
						if (rowSpan > 1) {
							colSpan = tds[j].colSpan ? parseInt(tds[j].colSpan) : 1 ;
							
							positions[currentPosition] = []; // initialise as empty array
							positions[currentPosition][0] = rowSpan;
							positions[currentPosition][1] = colSpan;
						}
						
						
						if (evenCol) {
							addClass(tds[j], evenColClass);
						} else {
							removeClass(tds[j], evenColClass);
						}
						
						// test
						//tds[j].innerHTML = '<strong>'+currentPosition+'</strong>';
						
						// swap even class for each colspan, default of 1
						// detect and compensate for colspans
						colSpan = tds[j].colSpan ? parseInt(tds[j].colSpan) : 1;
						for (k = 0; k < colSpan; k++) {
							evenCol =  ! evenCol;
							currentPosition++; // were now an extra col along
						}
						
						
						
						// next col/cell
						//currentPosition++;
					}
				}
				// flip from odd to even, or vice-versa
				evenRow =  ! evenRow;
			}
		}
	}
}





// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj)
{
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}


function removeClass (el, className)
{
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
}

function addClass (el, className)
{
	removeClass(el, className);
	el.className += " " + className;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


// http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,tag,node) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


