function showTracks(rowJamTrack) {
  var $jamTrackTracks = rowJamTrack.find("td.jam_track_tracks");

  var name=rowJamTrack.find("td.name").text()
  var count = $jamTrackTracks.find("table tbody tr").length;

  if (rowJamTrack.next().attr('id') == "jam_track_tracks_detail") {
    $(this).html("Show Tracks");
    rowJamTrack.next().remove();
  } else {
    $(this).html('Hide Tracks');
    if (count == 0) {
      rowJamTrack.after(
        $("<tr id=\"jam_track_tracks_detail\"></tr>").html(
          $("<td colspan=\"1\"></td>")
        ).append(
          $("<td colspan=\"4\"></td>").html(
            "No Tracks"
          )
        )
      );
    }
    else {
      rowJamTrack.after(
        $("<tr id=\"jam_track_tracks_detail\"></tr>").html(
          $("<td/><td colspan=\"1\"><em><strong>Tracks in '" + name + "':</strong></em></td>")
        ).append(
          $("<td colspan=\"4\"></td>").html(
            $jamTrackTracks.html()
          )
        )
      );
    }
  }
}

$(document).ready(function() {
  $("th.jam_track_tracks").css('display', 'none');
  $("td.jam_track_tracks").css('display', 'none');
  showTracks($("a.expand").parents("tr"))
   
  $(".show_tracks").click(function(e) {
    e.preventDefault();

    var $rowJamTrack = $(this).parents('tr');
    showTracks($rowJamTrack) 
  })
});