/**
 * Imtech JavaScript library | SorTable
 * Composed 2006 by Waldek Mastykarz | Imtech ICT BS
 * All rights reserved  
 * @created		2006-11-09 14:08:00
 * @modified	2006-11-09 14:08:00
 * @version		1.0 
 */

// set current namespace
Imtech.Visual = Imtech.Visual || {}

/**
 * ZebraTable
 * Copyright:	Copyright (C) 2006 Imtech ICT Business Solutions
 * Author:		Waldek Mastykarz | Imtech ICT BS
 * Datum:		09-11-2006
 * Version:		1.1
 *
 * USAGE:
 * 
 * <table class="zebraTable">
 *		...
 * </table>
 * 
 * @dependency	Imtech.isFunction
 * @dependency	Imtech.isObject 
 * @dependency	Imtech.Visual.getElementsByClassName
 * 
 * CHANGES LOG:
 * 1.1		~ stripping the even marker added as the tables can be sorted       
 **/
Imtech.Visual.ZebraTable = {
	init: function() {
		if (!Imtech.isFunction ||
			!Imtech.isFunction(Imtech.isObject) ||
			!Imtech.isObject(Imtech.Visual) ||
			!Imtech.isFunction(Imtech.Visual.getElementsByClassName))
			return false;
			
		// PRIVATE FUNCTIONS //
		
		function processRows(section) {
			var counter = 1;
	
			// process all child nodes within the current table section
			for (m = 0; m < section.childNodes.length; m++) {
				// add even class if row is even
				if (section.childNodes[m].nodeName == "tr" ||
					section.childNodes[m].nodeName == "TR") {
					// strip the even marker
					section.childNodes[m].className = section.childNodes[m].className.replace("even", "");
					
					if (counter % 2 != 0)
						section.childNodes[m].className += " even";										
						
					counter++;
				}
			}
		}
			
		// obtain all zebraTables
		var aTables = Imtech.Visual.getElementsByClassName("zebraTable", document, "table");
		
		// process zebraTables
		for (i = 0; i < aTables.length; i++) {
			/**
			 * control whether the table contains tbody sections or not
			 */
			var sTmp = aTables[i].innerHTML;
			var hasTbody = false;
			
			if (sTmp.indexOf("<TBODY") != -1 ||
				sTmp.indexOf("<tbody") != -1)
				hasTbody = true;
			
			if (hasTbody) {
				// obtain all tbody sections from the current table
				var aTbody = aTables[i].getElementsByTagName("tbody");
				
				for (j = 0; j < aTbody.length; j++)
					processRows(aTbody[j]);
			}
			else
				processRows(aTables[i]);
		}
	}
};
