// Absconded and adapted from http://www.alistapart.com/articles/zebratables

// Stripe colors.
var TABLE_ROW_COLOR_ODD    = '#C1E3D3';
var TABLE_ROW_COLOR_EVEN   = '#FFF';
var TABLE_ROW_COLOR_SELECT = '#6DC8A5';

// Performs zebra striping of the specified table's rows.
function stripe(table_id) {
  // Flag indicating whether the current row is odd or even.
  var even = false;

  // Obtain a reference to the desired table; if no such table exists, abort.
  var table = document.getElementById(table_id);
  if (! table) { return; }
  
  // By definition, tables can have more than one tbody element, so iterate through all of them.
  var tbodies = table.getElementsByTagName('tbody');
  for (var h = 0; h < tbodies.length; h++) {
    // Process all table rows.
    var trs = tbodies[h].getElementsByTagName('tr');
    for (var i = 0; i < trs.length; i++) {
      var current_row = trs[i];
      // Skip hidden rows.
      if( current_row.style.display == 'none' ) { continue; }
      
      // Stripe the row.
      stripeRow(current_row, even ? TABLE_ROW_COLOR_EVEN : TABLE_ROW_COLOR_ODD);
      
      // Change the row color on mouse over.
      current_row.onmouseover = 
          function(){ stripeRow(this, TABLE_ROW_COLOR_SELECT); this.style.cursor = 'pointer'; };
      current_row.onmouseout = even ?
          function(){ stripeRow(this, TABLE_ROW_COLOR_EVEN); this.style.cursor = 'default'; } :
          function(){ stripeRow(this, TABLE_ROW_COLOR_ODD);  this.style.cursor = 'default'; };
          
      // Flip from odd to even, or vice-versa
      even =  ! even;
    }
  }
}

// Stripes the given table row's table data cells.
function stripeRow(current_row, row_color) {
  // Process all the row's table data elements.
  var tds = current_row.getElementsByTagName('td');
  for (var j = 0; j < tds.length; j++) {
    tds[j].style.backgroundColor = row_color;
  }  
}

