
  /** TODO:
      schauen, dass/ob die prevClosed-Klassen richtig gebaut werden beim Umsortieren
  */

var expandedIds = new Object();

$(document).ready(function(){
  $("#overview").tablesorter({textExtraction: 'complex',
                              cssAsc: 'headerASC',
                              cssDesc: 'headerDESC',
                              sortList: [[4,1]]});


  $("a.expander").click(function() {
    context = $(this).parents('tr').attr('id');
    if (expandedIds[context] == true) {
    
      /* collapse */
      collapseRow(context);
     
    } else {

      /* expand */
      expandRow(context);

    }
    return false;
  })


});

/** user function zum Entfernen der Detail-Zeilen aus der Tabelle */
function tablesortStart(theader) {
  if (sortState == 1) {
    sortState = 0;

    $(theader).parents("table").find('tr.details').remove();
    $(theader).parents("table").find('tr.nextpanded').removeClass('nextpanded');
    $(theader).parents("table").find('tr.prevClosed').removeClass('prevClosed');
  }
}

function collapseRow(context) {
  $('#'+context).next('tr.details').remove();
  if ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent)) {
    $('#'+context).find('td').css('filter','');
/*    $('#'+context).find('td').iunfixpng();*/
  }

  $('#'+context).removeClass('expanded');
  $('#'+context).prev('tr').removeClass('nextpanded');
  $('#'+context).removeClass('prevClosed');

  $('#'+context).find('td.first img.expand').attr('src', 'img/arbeiten/expand.gif');

  
  /* add nextpanded class if the next row is currently expanded so the line at bottom gets drawn. 
  */

  if ($('#'+context).next('tr').hasClass("expanded") || $('#'+context).next('tr').hasClass("prevClosed")) {
    $('#'+context).next('tr').addClass('prevClosed');
    $('#'+context).next('tr.prevClosed').ifixpng();
    $('#'+context).addClass('nextpanded');
  }
  
  expandedIds[context] = false;
}

function expandRow (context) {

  if ($('#'+context).next('tr').hasClass('details')) { 
    return 0;
  }
  $('#'+context).find('td.first img.expand').attr('src', 'img/arbeiten/collapse.gif');
  
  $('#'+context).after('<tr class="details"><td colspan="6"></td></tr>');
  $('#'+context).removeClass('nextpanded'); 
  $('#'+context).prev('tr').addClass('nextpanded');
  
  if ( $('#'+context).prev('tr').hasClass('details') == 0) {
    $('#'+context).addClass('prevClosed');
  } else {
    $('#'+context).addClass('expanded');
  }
  if ($('#'+context).next('tr').next('tr').hasClass('prevClosed')) {
    $('#'+context).next('tr').next('tr').removeClass('prevClosed');
    if ($('#'+context).next('tr').next('tr').next('tr').hasClass('details'))
      $('#'+context).next('tr').next('tr').addClass('expanded');
  }
  
  expandedIds[context] = true;
  
  if ($.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent)) {
    $('tr.prevClosed td').ifixpng();
    $('tr.details td').ifixpng();
  }
  
  
  aop = $.ajaxSetup({data:{id:context.substring(3)},
               url:'overview_detail.php',
               type:'POST',
               context:context,
               success:function(data) {
                 $('#'+this.context).next('tr.details').find('td').html(data);
                 $('#'+this.context).next('tr.details').find('a').hover(function(){$(this).addClass('hover');}, function(){$(this).removeClass('hover')});
               }
  });
  $.ajax(aop);


}

/** user function zum erneuten expanden der Detail-Zeilen aus der Tabelle */
function tablesortStop(theader) {
  sortState = 1;
  for (var context in expandedIds)
  {
    if (expandedIds[context] == true)
    {
      aop = $.ajaxSetup({data:{id:context.substring(3)},
                   url:'overview_detail.php',
                   type:'POST',
                   context:context,
                   success:function(data) {
                     expandRow(this.context, data);
                   }
      });
      $.ajax(aop);
    }
  }
}
