/* Initial zebra striping code from David F. Miller posted at http://www.alistapart.com/articles/zebratables/ 
   Modifed for use at http://lib.calpoly.edu/ */
   
// 07/15/09 JV Removed restrictions that will avoid striping cells with a table class specified since classes are sometimes used for other purposes like text-alignment.
// 07/15/09 JV Added colAlign function based on zebratable function for post-construction table column alignment
// 07/17/09 JV Added column width function that can be used to line columns up with each other.
// 07/20/09 JV Added a custom column style script. This renders the column text align script useless.

/* 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 zebratable(id) {
	
	/* the flag we'll use to keep track of whether the current row is odd or even */
	var even = false;
	
	/* Check whether the table has TH tags with row scope */
	var thRows = 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 evenColor = arguments[2] ? arguments[2] : "#fff";
	var oddColor = arguments[3] ? arguments[3] : "#f8fbff";
	
	/* obtain a reference to the desired table if no such table exists, abort */
	var table = document.getElementById(id);
	if (! table) {
		return;
	}
	
	/* 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++) {
	
		/* 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++) {
			
			/* avoid rows that have a backgroundColor style specified */
			if ( ! trs[i].style.backgroundColor) {
				
				/* get all the cells in this row... */
				var ths = trs[i].getElementsByTagName("th");
				var tds = trs[i].getElementsByTagName("td");
				
				/* change the th (row scope) background color */
				if (thRows) {	
					ths[0].style.backgroundColor = even ? evenColor : oddColor;
				}
				
				/* and iterate through them... */
				for (var j = 0; j < tds.length; j++) {
					var mytd = tds[j];
					
					/* avoid cells that have a backgroundColor style specified */
					if ( ! mytd.style.backgroundColor) {
						mytd.style.backgroundColor = even ? evenColor : oddColor;
					}
				}
			}
			/* flip from odd to even, or vice-versa */
			even =  ! even;
		}
	}
}

function colWidth(id) {
	/* Create a new array to hold all the column widths */
	var colSize = new Array();
	
	/* Count and collect all column widths that exist. Store to colSize array. */
	for (var i = 0; i < (arguments.length - 1); i++) {
		colSize[i] = arguments[i + 1];	// colsize[0] = arguments[1]
	}
	
	/* Store table object to variable if it exists */
	var table = document.getElementById(id);
	if (! table) {
		return;
	}
	
	/* store th tags to array */
	var thtags = table.getElementsByTagName("th");
	
	/* Set the width for every TH tag in the table. All the TD cells in the column should inherit this width. */
	for (var j = 0; j < colSize.length; j++) {
		/* change the alignment of the jth column if and only if no width value has already been specified. */
		if ( ! thtags[j].style.width) {
			thtags[j].style.width = colSize[j];
		}
	}
}

function colCustomStyle(id, colNum, cssProperty, propValue, applyToHeader) {
	/* Store table object to variable if it exists */
	var table = document.getElementById(id);
	if (! table) {
		return;
	}
	
	/* column to align - subtract 1 for zero indexing purposes*/
	colNumber = colNum - 1;
	
	/* Convert CSS Properties to JavaScript Commands*/
	
		var words = cssProperty.split("-");	// Split on dashes
		
		var jsCommand = words[0];	// The first part of the jsCommand matches the CSS property
		
		/* For every word after the first word, capitalize the first letter and add on to jsCommand */
		for (var g = 1; g < words.length; g++) {
			 jsCommand += words[g].substring(0,1).toUpperCase() + words[g].substring(1,words[g].length);
		}
		
	/* Table Work */

		/* If the option to apply to the header is set to true, do it */
		if (applyToHeader) {	
			/* store th tags to array */
			var thtags = table.getElementsByTagName("th");
			
			/* change the style of the colNumberth TH cell if the same style is not already specified somewhere else */
			if ( ! thtags[colNumber].style[jsCommand]) {
				thtags[colNumber].style[jsCommand] = propValue;	
			}
		}

		/* by definition, tables can have more than one tbody element, so we'll have to get the list of child TBODYs */
		var tbodies = table.getElementsByTagName("tbody");
		
		/* and iterate through them... */
		for (var h = 0; h < tbodies.length; h++) {
		
			/* find all the TR elements... */
			var trs = tbodies[h].getElementsByTagName("tr");
			
			/* ... and iterate through them */
			for (var i = 0; i < trs.length; i++) {
				
				/* avoid rows that have the style specified */
				if ( ! trs[i].style[jsCommand]) {
					
					/* get all the cells in this row... */
					var tds = trs[i].getElementsByTagName("td");
					
					/* set the style if no style is specified style is specified */
					if ( ! tds[colNumber].style[jsCommand]) {
						tds[colNumber].style[jsCommand] = propValue;
					}				
					
				}
			}
		}
}
