var LeagueTable = function(table) {  
  table = $(table);
  
  // Add hover effects
  table.select('tbody tr')
    .invoke('observe', 'mouseover', Hover.over)
    .invoke('observe', 'mouseout', Hover.out);
  
  // Enable comparison if applicable
  if (table.up('.block').hasClassName('block_competition_league_table')) {    
    // Highlight previously selected rows again (this is useful when a user
    // hits refresh or presses the back button to this page).
    var checked = 0;
    $A(table.select('input[type="checkbox"]:checked')).each(function(checkbox) { 
      checkbox.up('tr').addClassName('highlight');
      checked += 1;
    });
    
    var button = table.select('input.compare-button')[0];
    
    // Determine if the button should be disabled or enabled
    button.disabled = (checked < 2);
    
    var selectHandler = function(evt) {
      var checkbox = evt.element();
      
      var row = checkbox.up('tr');
      if (checkbox.checked) {
        if (checked + 1 > 2) {
          // Rollback the action of the user. The user is not allowed to select more
          // than two teams.
          evt.stop();
          return;
        }
        
        checked += 1;
        row.addClassName('highlight');
      }
      else {
        checked -= 1;
        row.removeClassName('highlight');
      }
      
      button.disabled = (checked < 2);
    };
    
    table.select('input[type="checkbox"]').invoke('observe', 'click', selectHandler.bindAsEventListener());
  }
};
