RedSquare.CandyStriper = Class.create();

RedSquare.CandyStriper.prototype = 
{
	initialize: function(table, alternateClassName, options)
	{
		// find the correct table rows to candy stripe
		
		this.table = table;
		this.alternateClassName = alternateClassName;
		
		
		this.modulo = 1;
		
		if (options)
		{
			this.modulo = this.options.modulo == null ? 1 : this.options.modulo;
		}
		
		
		this.update();
	},
	
	update: function()
	{
		var tr;
		
		var tbody = this.table.getElementsByTagName("tbody")
		
		if (tbody.length != 0)
		{
			var tr = tbody[0].getElementsByTagName("tr")	
		}
		else
		{
			// check for thead - if it is present (but by now tbody isn't) don't process the rows (must use a tbody with a thead)
			
			var thead = this.table.getElementsByTagName("thead")
			
			if (thead.length == 0) // neither thead or tbody is present, so process all rows
				tr = this.table.getElementsBytagName("tr");
		}
		
		if (tr)
		{
			
			for (i=0; i<tr.length; i++)
			{
				if (i % 2 == this.modulo)
					Element.addClassName(tr[i], this.alternateClassName);	
				else
					Element.removeClassName(tr[i], this.alternateClassName);
			}
		}
	},
	
	animate: function(interval)
	{
		// utterly pointless bling - like a <blink> tag for the Web 2.0 kids ;)
		
		this.stopAnimate();
		this.animateInterval = window.setInterval(this.alternate.bindAsEventListener(this), interval);
	},
	
	stopAnimate: function()
	{
		if (this.animateInterval)
			window.clearInterval(this.animateInterval);
	},
	
	alternate: function()
	{
		this.modulo =  1 - this.modulo;
		this.update();
	}
	
}