var URL_BASE="index.php?group_id=";

function makeRowActive( tr )
{
  if(!$(tr).hasClass('selected'))
  { 
    $(tr).siblings('tr').removeClass('selected');
    $(tr).addClass('selected');
    
    var percentage = 1;
    var total = parseFloat($(tr).attr('total'));
    var goal = parseFloat($(tr).attr('goal'));
    
    var formatted_goal = $(tr).attr('goal_formatted');
    var currently_raised = $(tr).children('.total').text();
    
    if( total > 0 )
    {
      percentage = (total / goal);
      percentage = Math.min( percentage, 1 );
      percentage = 1 - percentage;
    }
  
    var empty_house = $('.house_thermo .house_empty');
    var height = empty_house.parent().height();
    var targetHeight = parseInt(height*percentage)+'px';
    empty_house.animate({'height': targetHeight });
    $('.indv_goal .amt').text(formatted_goal);
    $('.indv_total .amt').text(currently_raised);
    $('.joinbox_inner .name').text("Team");
    
    $('#corp_logo').replaceWith( $(tr).find('.logo').clone().attr('id', 'corp_logo') );
    
    var url = URL_BASE + $(tr).attr('group_id');
    $('a.joinbtn').attr('href', url);
  }
}

$(document).ready(function() {
  $('table.donation_leaderboard tbody tr').hover(
    function(e){ $(this).addClass('hover'); },
    function(e){ $(this).removeClass('hover'); }
  ).click(function(e){
    makeRowActive( $(this) );
  });
});