$(document).ready(function()
{
    var plus = '/images/design/icon-table-open.gif';
    var minus = '/images/design/icon-table-close.gif';
    $('.expandable').click(function()
    {
      var k = $(this).hasClass('expanded');

       if(k == 1)
           {
               //needs to collapse
               
                $(this).removeClass('expanded').parent().parent().removeClass('tbl-row-open').next().hide();
                if($(this).parent().parent().next().next().hasClass('tbl-row-first'))
                   {
                       $(this).parent().parent().next().next().removeClass('tbl-row-first');
                   }
                $(this).children('img').attr('src', plus).attr('alt', 'Close');
                $(this).parent().parent().removeClass('tbl-row-open-first');
                var z = $(this).parent().parent().next().next().hasClass('tbl-row-open-first');
                if(z)
                    {
                        $(this).parent().parent().next().next().removeClass('tbl-row-open-first');
                    }
           }
           else
           {
               //needs to expand

                $(this).addClass('expanded');

                //OK, change the class of this row and open the info panel
                $(this).parent().parent().addClass('tbl-row-open').next().show();

                //OK, now check the next row to see if it has the class of first. if so, steal it and put it on this row
                var test = $(this).parent().parent().next().next().hasClass('tbl-row-first');
                if(!test)
                {
                    $(this).parent().parent().next().next().addClass('tbl-row-first');
                }
                $(this).children('img').attr('src', minus).attr('alt', 'Expand');

                var o = $(this).parent().parent().prev().prev().children().children('a').hasClass('expanded');
                if(o)
                    {
                        $(this).parent().parent().addClass('tbl-row-open-first');
                    }
                var c = $(this).parent().parent().next().next().children().children('a').hasClass('expanded');
                var t = $(this).parent().parent().next().next().hasClass('tbl-row-open-first');
                if(c && !t)
                    {
                        $(this).parent().parent().next().next().addClass('tbl-row-open-first');
                    }

                    var jeff = $(".data-tbl tr:eq(1)").children().children('a').hasClass('expanded')
                    if(jeff)
                    {
                        $(".data-tbl tr:eq(1)").addClass('tbl-row-open-first')
                    }

           }
    return false;
    });
});
