
// gets the first parent of type_ for the given element src_. returns the parent or null
// nodeType Node.ELEMENT_NODE == 1 (but IE does not support the object Node? strange.
// but using 1 will work in both
function getAncestor( src_, type_ )
{
  var target = null;
  var tmp = src_.parentNode;
  var found = false;
  while ( tmp && !found )
  {
    // protective test around tag checks - we want to make sure only html element objects
    // are tested (these elements support tagName whereas other types do not)
    if ( tmp.nodeType == 1 )
    {
      if ( tmp.tagName == type_ )
      {
        target = tmp;
        found = true;
      }
    }
    tmp = tmp.parentNode;
  }
  return target;
}

// finds a child, grandchild etc of the given object src_ that is of type type_.
// finds immediate children first, then grand children, great grand children etc
function getDescendent( src_, type_ )
{
  if ( null == src_ )
    return null;
  
  var children = src_.childNodes;
  var target = null;
  var i = 0;
  // perform pass of immediate children before searching for grandchildren
  while ( i < children.length )
  {
    if ( children[i].nodeType == 1 )
    {
      if ( children[i].tagName == type_ )
      {
        target = children[i];
        i = children.length;
      }
    }
    i++;
  }
  // found our target - eject eject
  if ( target )
    return target;
  
  // search each child for node
  for ( i = 0; i < children.length; i++ )
  {
    target = getDescendent( children[i], type_ );
    if ( target )
      i = children.length; // found target, abandon search
  }
  return target;
}
/*
function swapClass( obj_, class1_, class2_, parentType_, childType_ )
{
  var anc = getAncestor( obj_, parentType_ );
  var tgt = getDescendent( anc , childType_ );
  
  if ( tgt )
    tgt.className = ( ( tgt.className == class1_ ) ? class2_ : class1_ ); 
}
*/
function swapClass( obj_, class1_, class2_ )
{
  if ( obj_ )
    obj_.className = (( obj_.className == class1_ ) ? class2_ : class1_ );
}

