var chcTables = function() {
  var config = {
    tableDelay : 10000,
    fadeDelay : "fast",
    table : ["mensTable","womensTable"],
    currentTable : 0
  };

  var switchTable = function() {
    $('#progressBar').css('width', '1%');
    $('#' + config.table[config.currentTable]).fadeOut("slow");
    config.currentTable++;

    if(config.currentTable >= config.table.length) {
      config.currentTable = 0;
    }

    $('#' + config.table[config.currentTable]).fadeIn("slow");
    $('#progressBar').animate({'width': '100%'},{ duration: config.tableDelay, complete: function() {
      switchTable();
    }});
  };

  var formatFixtures = function() {
    var fixtureRows = '.fixtureContainer tr';
    var fixtureSpreadCells = 'spread';
    var fixtureFirstRowCells = fixtureRows + ':eq(0) .' + fixtureSpreadCells;
    var fixtureRowCellsCount = $(fixtureFirstRowCells).length + 1;

    $(fixtureRows).each(function() {
      $(this).children('td, th').each(function(index, ele) {
        if($(ele).hasClass(fixtureSpreadCells)) {
          if(index == 0) {
            $(ele).attr('colspan',fixtureRowCellsCount);
          } else {
            $(ele).remove();
          }
        } else if(index == 1) {
          $('<td> - </td>').insertAfter($(ele));
        }
      });
    });
  };

  var init = function() {
    formatFixtures();
    $('#progressBar').animate({'width': '100%'},{ duration: config.tableDelay, complete: function() {
      switchTable();
    }});	
  };

  return {
    init: init
  };
}();



$(document).ready(function() {
  chcTables.init();
});

