/* Automatically promote the first row to table header if there are no header
   fields in the table */
$(document).ready(function () {
  /* Only do this for tables inside product description */
  $('#product-description').find('table').each(function (index) {
    if ($(this).find('th').size() > 0) {
      return;
    }

    $(this).find('tr').eq(0).find('td').addClass('header');
  });  
});

/* Automatically insert zebra stripes for tables in the product description */
$(document).ready(function () {
  /* Only do this for tables inside product description */
  $('#product-description').find('table').each(function (index) {
    $(this).find('tr').each(function (index) {
      if ($(this).hasClass('even') || $(this).hasClass('odd')) {
        return;
      }

      $(this).addClass(index % 2 == 0? 'even': 'odd');
    });
  });
});

/* Automatically expand rows which are shorter than the maximum row in the
   product description */
$(document).ready(function () {
  /* Only do this for tables inside product description */
  $('#product-description').find('table').each(function (index) {
    /* Find the maximum number of cells */
    var rows = new Array();
    var maximum = 0;
    $(this).find('tr').each(function (index) {
      var current = 0;
      $(this).find('td, th').each(function (index) {
        current += $(this).attr('colSpan') || 1;
      });
      rows[index] = current;
      if (maximum < current) {
        maximum = current;
      }
    });

    /* Add an empty cell to the end of the row, or expand a single element */
    $(this).find('tr').each(function (index) {
      var current = rows[index];
      if (current == maximum) {
        return;
      }

      $(this).append('<td>&nbsp;</td>');
      if (maximum - current != 1) {
        $(this).find('td').slice(-1).attr('colSpan', maximum - current);
      }
    });
  });
});

