/* $Id: striper.js,v 1.4 2008/03/26 01:53:52 bob Exp $ */

// Splintered striper 1.3
// reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
// but as a general DOM means of assigning any number of classes to children of a parent element.
// Patrick H. Lauke aka redux / www.splintered.co.uk
// Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/

// Converted over to using Prototype to allow for nested lists
// by Robert Bottomley, UC Riverside.

/*
 * What we want striped.
 */
function striperLoadEvent() {
/*           Parent             Child
 *           Selector           Classes */
	striper ('tbody.striped',   'odd, .');
	striper ('ul.striped',      'odd,.');
	striper ('ul.big_links',    '.');
	striper ('ol.striped',      'odd,.');
	striper ('dl.striped',      'odd,.');
}

/*
 * Summary:      Function that applies any number of classes to all child elements
 *               contained in all occurrences of a parent element (either with or without a specific class)
 * Parameters:   parentSelector - parent selector (same as CSS selectors)
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Return:       none
 */
function striper(parentSelector, styleClasses)
{
	var i=0, currentParent, currentChild;

	// capability and sanity check
	if ((document.getElementsByTagName) && (parentSelector) && (styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');

		// get an array of all parent elements
		var parentItems = $$(parentSelector);

		// loop through all parent elements
		parentItems.each(function(currentParent) {
			var j=1, k=0;

			// get all child elements of the current parent element
			var childItems = currentParent.childElements();

			// loop through all child elements
			childItems.each(function(currentChild) {
				// based on the current element and the number of styles in the array, work out which class to apply
				k = (j+(styles.length-1)) % styles.length;
				j++;

				if (styles[k] != '.') {
					// add the class to the child element
					$(currentChild).addClassName(styles[k]);
				}

				// add mouseover rules for hover effect
				currentChild.onmouseover = function() {
					$(this).addClassName('ruled');
				}

				currentChild.onmouseout = function() {
					$(this).removeClassName('ruled');
				}
			});
		});
	}
}

//
// Adds event to window.onload without overwriting currently
// assigned onload functions.
function addLoadEvent(func) {
    if (window.addEventListener)
        window.addEventListener('load', func, false);
    else if (window.attachEvent)
        window.attachEvent('onload', func);
}

addLoadEvent (striperLoadEvent);

