/************************************************************
* window_shade.js
* Javascript support file for using the window_shade templates
* Works in conjuction with window_shade.tpl & window_shade.css
* CVS: $Id$
*************************************************************/

/**
* Toggle a window shade because title bar was clicked
* @access public
* @param  string    control_id        id of the window shade control
* @param  string    window_shade_id   id of the shade to toggle
* @param  int       shade_height      full height of open shade
* @param  bool      multiple_open     allow multiple shades to be open
*                                     at the same time
* @param  bool      animate           animate the open and close?
* @param  bool      prevent_all_close prevent all shades from closing.
*/
function wsToggle( control_id, window_shade_id, shade_height, multiple_open, animate, prevent_all_close )
{
  //alert( control_id  +  window_shade_id );
  if( !multiple_open )
  {
    // unhighlight other title bars...  
    control_node = document.getElementById( control_id ).childNodes;
    for( i=0; i<control_node.length; i++ )
    {
      if( control_node[i].className == 'window-shade' && control_node[i].style.display == '' && control_node[i].id != window_shade_id )
        wsCloseShade( control_id, control_node[i].id, shade_height, animate );
    }
  }
  
  // highlight bar..
  shade = document.getElementById( window_shade_id );
  if( shade.style.display == '' )
  {
    if( !prevent_all_close )
      wsCloseShade( control_id, window_shade_id, shade_height, animate );
  }
  else
  {
    wsOpenShade( control_id, window_shade_id, shade_height, animate );
  }
}

/**
* Close a shade
* @access public
* @param  string    control_id        id of the window shade control
* @param  string    window_shade_id   id of the shade to toggle
* @param  int       shade_height      full height of open shade
* @param  bool      animate           animate the open and close?
*/
function wsCloseShade( control_id, window_shade_id, shade_height, animate )
{
  title_bar = document.getElementById( window_shade_id + '-title-bar' );
  title_bar.className = 'title-bar';
  document.getElementById( window_shade_id + '-expand-collapse' ).innerHTML = '+';
  document.getElementById( window_shade_id + '-click-to-change' ).innerHTML = '(click to expand)';
  
  if( animate )
    _wsCloseShadeAnimation( window_shade_id, shade_height, shade_height, 25 );
  else
    document.getElementById( window_shade_id ).style.display = 'none';
}

/**
* Open a shade
* @access public
* @param  string    control_id        id of the window shade control
* @param  int       shade_height      full height of open shade
* @param  string    window_shade_id   id of the shade to toggle
* @param  bool      animate           animate the open and close?
*/

function wsOpenShade( control_id, window_shade_id, shade_height, animate )
{
  title_bar = document.getElementById( window_shade_id + '-title-bar' );
  title_bar.className = 'title-bar title-bar-selected';
  document.getElementById( window_shade_id + '-expand-collapse' ).innerHTML = '-';
  document.getElementById( window_shade_id + '-click-to-change' ).innerHTML = '(click to collapse)';
  
  if( animate )
    _wsOpenShadeAnimation( window_shade_id, shade_height, 0, 25 );
  else
    document.getElementById( window_shade_id ).style.display = '';    
}

/**
* Animate the closing of a shade
* @access private
* @param  string    window_shade_id   id of the shade to toggle
* @param  int       shade_height        full height of shade
* @param  int       cur_height        current height to animate
* @param  int       wait_mill         amount of time to wait between updates
*/
function _wsCloseShadeAnimation( window_shade_id, shade_height, cur_height, wait_mill )
{
  //alert( cur_width );
  var shade = document.getElementById( window_shade_id );
  shade.style.height = (cur_height) + 'em';        
  if( cur_height > 0 )
    setTimeout( '_wsCloseShadeAnimation( \'' + window_shade_id + '\', ' + shade_height + ', ' + (cur_height - 2) + ', ' + wait_mill + ' )', wait_mill )
  else
  {
    shade.style.display = 'none';
    shade.style.height = shade_height + 'em';
  }
}

/**
* Animate the openning of a shade
* @access private
* @param  string    window_shade_id   id of the shade to toggle
* @param  int       shade_height        full height of shade
* @param  int       cur_height        current height to animate
* @param  int       wait_mill         amount of time to wait between updates
*/
function _wsOpenShadeAnimation( window_shade_id, shade_height, cur_height, wait_mill )
{
  //alert( cur_width );
  var shade = document.getElementById( window_shade_id );
  shade.style.height = (cur_height) + 'em';        
  document.getElementById( window_shade_id ).style.display = '';    
  if( cur_height < shade_height )
    setTimeout( '_wsOpenShadeAnimation( \'' + window_shade_id + '\', ' + shade_height + ', ' + (cur_height + 2) + ', ' + wait_mill + ' )', wait_mill )
  else
    shade.style.height = shade_height + 'em';
}
