/*
 * $Id: lr.js 132 2008-10-30 01:27:07Z obrienjw $
 *
 * Implements expand/collapse dynamics for long runs on home page
 */
 
var lr_before = '<a href="javascript:void 0">';
var lr_after = '</a>';
var lr_inprogress = '';

// Advance one item every 100ms
var lr_interval = 100;

// A handle for the interval timer
var lr_timer;

// The stuff currently being toggled
var lr_id = -1;
var lr_tog;
var lr_col;

// The function called upon toggle completion
var lr_callback;

// This array must be populated with LR objects prior to calling lr_init
var lr_list = new Array( );

function LR( more, less )
{
   this.more = lr_before + more + lr_after;
   this.less = lr_before + less + lr_after;
}


/*
   Locate all toggles, populate them, and then collapse related items.
*/
function lr_init( )
{
   if ( document.getElementById )
   {
      for ( var i = 0; i < lr_list.length; ++i )
      {
         if ( lr_list[ i ] == undefined )
            continue;
            
         var tog = document.getElementById( 'lr_tog_' + i );
         var col = document.getElementById( 'lr_col_' + i );
         var targets = col.getElementsByTagName( 'div' );

         for ( var j = 0; j < targets.length; ++j )
         {
            targets[ j ].style.display = 'none';
         }

         col.style.display = 'none';

         tog.innerHTML = lr_list[ i ].more;
         tog.onclick = lr_toggle;
      }
   }
}

/*
   Determine the current state of the collapsing items in question, and then
   initiate a transition to the opposite state.
*/
function lr_toggle( )
{
   var dir, state;

   if ( lr_id != -1)
   {  // Don't do more than one thing at a time
      return;
   }
   
   // Peel off the numeric portion of the ID from this
   var parts = this.id.split( '_' );
   lr_id = parseInt( parts.pop( ) );

   lr_tog = document.getElementById( 'lr_tog_' + lr_id );
   lr_col = document.getElementById( 'lr_col_' + lr_id );

   lr_tog.innerHTML = lr_inprogress;

   switch( lr_col.style.display )
   {
      case '':
         // Collapse
         dir = 1;
         state = 'none';
         lr_callback = function () {
            lr_col.style.display = 'none';
            lr_tog.innerHTML = lr_list[ lr_id ].more;
            lr_id = -1;
            };
         break;

      case 'none':
         // Expand
         lr_col.style.display = '';
         dir = -1;
         state = '';
         lr_callback = function () {
            lr_tog.innerHTML = lr_list[ lr_id ].less;
            lr_id = -1;
            };
         break;
   }

   lr_timer = window.setInterval(
      'lr_next(' + dir + ',\'' + state + '\' )',
      lr_interval );
}

/*
   Toggle the next element. If there are no more to toggle, clear the timer.

   Direction:
      >=0      : start at the beginning and go forward
      <0       : start that the end and go backward
   New State:
      ''       : display naturally
      'none'   : hide
*/
function lr_next( dir, state )
{
   var ch, i, start, end, inc;

   if ( lr_id == -1 )
   {
      window.clearInterval( lr_timer );
      return;
   }

   ch = lr_col.getElementsByTagName( 'div' );

   if ( dir >= 0 )
   {
      start = 0;
      end = ch.length;
      inc = 1;
   }
   else
   {
      start = ch.length - 1;
      end = -1;
      inc = -1;
   }

   for ( i = start; i != end; i += inc )
   {
      if ( ch[ i ].style.display != state )
      {
         ch[ i ].style.display = state;
         break;
      }
   }

   if ( i == end )
   {
      window.clearInterval( lr_timer );
      lr_callback( );
   }
}
