// Highlights table rows

// ttocco 10/09/08
// modified initialize to accept a table object array instead of an id
// modified for loop to reset color for each array object
// modified to not add events for rows with th tags
// modified to accept a non array and $splat it into a single dimension array.

var tableHighlighter = new Class({	
	
	options: {
			rowColourClass: 'highlighted',
			rowHoverColourClass: 'hoverHighlighted',
			highlightRow: 'even',
			everyOther: 0
	},
	
	initialize: function( objarray, options ) {
		
		this.setOptions( options );
		
		objarray = $splat(objarray);
		
		for(var i=0; i < objarray.length; i++){

			if( this.options.highlightRow == 'odd' ){
				this.options.everyOther = 1;
			} else {
				this.options.everyOther = 0;
			}

			this.rows = objarray[i].getElementsByTagName('tr');
			this.rowsLength = this.rows.length;
			this.addHighlighting();
			
		}
	},
	
	addHighlighting: function(){
		
		var hoverClass = this.options.rowHoverColourClass;
			
		for( var i = 0; i < this.rowsLength; i++ ){

			if($(this.rows[i]).getElements('th').length == 0 ){
				$( this.rows[i] ).addEvents({
					'mouseover': function(){this.addClass(hoverClass)},
					'focus': function(){this.addClass(hoverClass)},
					'mouseout': function(){this.removeClass(hoverClass)},
					'blur': function(){this.removeClass(hoverClass)}
				});
			}
			
			if( this.options.everyOther != 0 ){
				this.rows[i].addClass( this.options.rowColourClass );
				this.options.everyOther = -1;
			}
			
			this.options.everyOther++;
			
		}
		
	}
	
});

tableHighlighter.implement(new Options);