function IAMTOC_Object()
{
  this.mSelectedID      = null;
  this.mSelectedPage    = null;
  
  this.fInit           = IAMTOC_Init;
  this.fSetSelection   = IAMTOC_SetSelection;
  this.fSetBackground  = IAMTOC_SetBackground;
  this.fSelectNodeByID = IAMTOC_SelectNodeByID;
}

function IAMTOC_Init(ParamID)
{
  var VarURLParts;
  var VarLastPart;
  var VarTrailParts;
  var VarAnchor;
  
  VarURLParts = document.location.href.split('/');
  VarLastPart = VarURLParts[VarURLParts.length - 1];
  VarTrailParts = VarLastPart.split('#');

  VarNode = this.fSelectNodeByID(ParamID);
  
  this.fSetBackground(ParamID, true);
  this.mSelected = ParamID;
    
  this.mSelectedPage = VarTrailParts[0];
  
  return true;
}

function IAMTOC_SetSelection(ParamID, ParamPage, ParamAnchor)
{
  var VarNode;
  
  if (ParamPage == this.mSelectedPage)
  {
    this.fSetBackground(this.mSelected, false);
    this.fSetBackground(ParamID, true);
    this.mSelected = ParamID;
    
    document.location.href = ParamPage + ParamAnchor;
  }
  else
  {
    VarTargetPage = ParamPage;
    
    if (ParamAnchor.length > 0)
    {
      VarTargetPage += ParamAnchor;
    }
    
    VarURLParts = document.location.href.split('/');
    VarURLParts[VarURLParts.length - 1] = VarTargetPage;
    VarURLString = VarURLParts.join('/')
    
    document.location.href = VarURLString;

  }
}

function IAMTOC_SetBackground(ParamID, bParamSelect)
{
  var VarNode;
  var VarClass;
  var VarClassText;
  var VarNewClass;
  var VarClassTextNode;
  var VarReplaceExpression;
  
  VarReplaceExpression = new RegExp('_selected$', '');
  
  if ((typeof(document.all) != 'undefined') &&
      (typeof(document.all) != null))
  {
    VarNode = document.all[ParamID];
    VarClass = VarNode.getAttributeNode('class');
    VarClassText = VarClass.value;
    
    if (bParamSelect)
    {
      VarClassText = VarClassText.replace(VarReplaceExpression, '');
      VarClassText += '_selected';
    }
    else
    {
      VarClassText = VarClassText.replace(VarReplaceExpression, '');
    }
    
    VarClass.value = '';
    VarClass.value = VarClassText;
    
    // urge overkill
    if (bParamSelect)
    {
      VarNode.style.backgroundColor = '#E1E1E1';
    }
    else
    {
      VarNode.style.backgroundColor = '#FFFFFF';    
    }

  }
  else if ((typeof(document.getElementById) != 'undefined') &&
           (typeof(document.getElementById) != null))
  {
    VarNode = document.getElementById(ParamID);
    VarClass = VarNode.getAttribute('class');
    
    if (bParamSelect)
    {
      VarClass = VarClass.replace(VarReplaceExpression, '');
      VarClass += '_selected';
    }
    else
    {
      VarClass = VarClass.replace(VarReplaceExpression, '');
    }
    
    VarNode.setAttribute('class', VarClass);
  }
  
  return true;
}

function IAMTOC_SelectNodeByID(ParamID)
{
  var VarReturnNode;
  VarReturnNode = null;
  
  if ((typeof(document.all) != 'undefined') &&
      (typeof(document.all) != null))
  {
    VarReturnNode = document.all[ParamID];
  }
  else if ((typeof(document.getElementById) != 'undefined') &&
           (typeof(document.getElementById) != null))
  {
    VarReturnNode = document.getElementById(ParamID);      
  }
  
  return VarReturnNode;
}
