jQuery(function($)
{
  $(".tablesorter").each(function(i)
  {
//    $.tablesorter.defaults.debug = true;
    if (this.id == 'manage_grants' || this.id == 'view_grants')
      $.tablesorter.defaults.sortList = [[1,0]];
    else if ($(this).parents('table').attr('id') == 'organization_aggregate') {
      $.tablesorter.defaults.headers = {1:{sorter : 'digit'}};
      $.tablesorter.defaults.widgets = ['zebra'];
    } else
    $.tablesorter.defaults.widgets = ['zebra'];
    // setup table sorter options
    var ts_options = {};
    ts_options['textExtraction'] = function(node)
    {
      if ($(node).children('a').html())
        return $(node).children('a').html();
      else if ($(node).children('div.calc').html())
        return $(node).children('div.calc').html();
      else
        return $(node).html();
    };

    // if th has class 'js-no-sort', turn off sorting via table sorter options
    var no_sort_indicies = [];
    $(this).find('thead tr th').each(function(i)
    {
      if ($(this).hasClass('js_no_sort'))
      {
        no_sort_indicies.push(i);
      }
    });

    if (no_sort_indicies.length > 0)
    {
      no_sort_opts = {};
      for (var i = 0; i < no_sort_indicies.length; i++)
      {
        no_sort_opts[no_sort_indicies[i]] = {sorter: false};
      }
      ts_options['headers'] = no_sort_opts;
    }
    $(this).tablesorter(ts_options);
  });
});