// JavaScript for zebra tables
// Addapted, modified, and heavily added to, so it would run inside the WebCT enviroment, from the script in the article at http://alistapart.com/articles/zebratables. Thanks guys
// Should be used with the template called "main" and style.css

function hasClass(obj)
{ //we need to see if the tr or td has an already defined class, if so, use that instead
	if (obj.getAttributeNode)
	{
		var result = false;
		if (obj.getAttributeNode("class") != null)
		{
			result = obj.getAttributeNode("class").value;
		}
		return result;
	} else
	{ // Explorer 5 and 5.5 do not understand "getAttributeNode", we will just feed them "false" so there tables will get striped
		return result;
	}
}

function stripe()
{ // the function that does the striping
	var even = false; //lets start odd, because that is what 1 is
	var evenColor = "tdZebra" // the fall back color if the style sheet cannot be found
	var table = document.getElementsByTagName("table"); // how many tables do we have in the document
	if(!table)
	{ // if there are no tables, why bother?
		return;
	}
	//alert(table);
	for (var f=0; f<table.length; f++)
	{ // loop for each table
		var tbodies = table[f].getElementsByTagName("tbody"); // how many tbody tags are there, because we can have more than on in each table
		//alert(tbodies);
		for (var h=0; h<tbodies.length; h++)
		{ // loop through each tbody tag
			var trs = tbodies[h].getElementsByTagName("tr"); // how many rows are there
			//alert(trs);
			for (var i=0; i<trs.length; i++)
			{ // loop through each row
				//alert(trs[i]);
				if (!hasClass(trs[i]) && !trs[i].style.backgroundColor)
				{ // is there a class or background color assigned to this row? If not, then continue
					var tds = trs[i].getElementsByTagName("td"); // how many td tags are there
					//alert(tds);
					for (var j=0; j<tds.length; j++)
					{ // loop through each td
						var mytd = tds[j]; 
						if (!hasClass(mytd) && !mytd.style.backgroundColor)
						{ // is there a class or background color assigned to this td? If not, then continue
							//alert(mytd);
							//alert(arguments[0]);
							if(arguments[0])
							{
								mytd.className = even ? evenColor:""; // are we even or odd, then apply the correct background color
								//alert(mytd.className);
							}
							else 
							{
								mytd.style.backgroundColor = even ? "#ECECEC":"";
							}
						}
					}
					tds.length?even = !even:""; // the switch that helps us go from even to odd and back again only if there are tds in a tr
				}
			}
			even = false; // reset value back to default for the next table
		}
	}
}

function getColor()
{ // we need to correct color from the correct style sheet
	var hasSheet = 0;
	var docSheets = document.styleSheets;
	//alert(docSheets.length);
	for(var i=0; i < docSheets.length; i++)
	{
		var aSheet = (docSheets.item(0).cssRules?docSheets.item(i).cssRules:docSheets.item(i).rules);
		//alert(aSheet.length + " is the length of aSheet");
		for (var l=0; l < aSheet.length; l++)
		{
			if(aSheet.item(l).selectorText == ".tdZebra")
			{
				//alert(aSheet.item(l).selectorText);
				hasSheet = 1;
				break;
			}
		}
	}
	if(hasSheet) // if the document does not have any style sheets, why bother?
		{ stripe(hasSheet); } // get the correct code depending if you are everybody else, or IE. Then get the correct style sheet depending on the count.
	else
		{ stripe(hasSheet); } // start a striping
}

window.onload = function()
{ // this fires when the page is loaded
	getColor();
}
window.onresize = function()
{ // this fires when the page is resized
	getColor();
}
