/*




Introduction
------------

Script by Justin Bell - tablescript@soupisgoodfood.net

This script is a combination of two scripts so that they work
together. Changes have also been made to play better with rows
that already have a class applied.

Articles code was used from:
http://www.alistapart.com/articles/tableruler/
http://www.alistapart.com/articles/zebratables/



Usage:
------

Use use code to load the functions:

    if (document.getElementsByTagName && document.createTextNode) {
        window.onload = function() {
            stripe();
            tableRuler();
        }
    }


Add "ruler" to the class of any table where you want to use the
ruler effect.

Add "stripe" to the class of any table where you want
alternating rows.




CSS example:
------------

    .even    {background-color:#ccc;}
    .special {background-color:#f00;}
    .ruled   {background-color:#666;}

.even is the alternating TR class
.ruled is the hovering ruler TR class
.special is in there to show you the suggested order if you
have other classes applied to TRs. In this case .special will
override the alternating style, but not the ruler style




*/




//-------------------------------------------------------------
// See if element contains a certain class

	function hasClass(classString, className) {

		classString = String(classString);
		var classes = classString.split(' ');
		for (var i = 0; i < classes.length; i++) {
			if (classes[i] == className) {
				return true;
			}
		}
		return false;
	}

//use indexOf(), or maybe not because this would cause "hello" and "hellothere" to both return true.


//-------------------------------------------------------------
// Apply stripe class to table

	function stripe() {

		var i, j, k, even, oldClass, tbodies, trs;
		var tables = document.getElementsByTagName('table');
		for (i = 0; i < tables.length; i++) {

			if (hasClass(tables[i].className, 'stripe')) {

				even = false;
				tbodies = tables[i].getElementsByTagName('tbody');
				for (j = 0; j < tbodies.length; j++) {

					trs = tbodies[j].getElementsByTagName('tr');
					for (k = 0; k < trs.length; k++) {

						if (even) {
							oldClass = trs[k].className;
							trs[k].className = 'even ' + oldClass;
						}
						even = !even;
					}
				}
			}
		}
	}




//-------------------------------------------------------------
// Apply ruler to table

	function tableRuler() {

		var i, j, k, oldClass, tbodies, trs;
		var tables = document.getElementsByTagName('table');
		for (i = 0; i < tables.length; i++) {

			if (hasClass(tables[i].className, 'ruler')) {

				tbodies = tables[i].getElementsByTagName('tbody');
				for (j = 0; j < tbodies.length; j++) {

					trs = tbodies[j].getElementsByTagName('tr');
					for (k = 0; k < trs.length; k++) {

						trs[k].onmouseover = function() {
							oldClass = this.className;
							this.className = oldClass + ' ruled';
							return false;
						}
						trs[k].onmouseout = function() {
							this.className = oldClass;
							return false;
						}
					}
				}
			}
		}
	}



