//********** REQUIRES MOOTOOLS CORE ******************************************************************************************************************

function notice(which) 
/*
To Function with AutoTOC to higlight the selected item from the TOC by setting a starting "notice" color that fades to the background color
*/
{
  var howLong = 1500;
  var bright = '#FC6';
  var normal = '#FFF';
  
  x=$(which).parentNode;
    if (Browser.Engine.trident)  y=$(which).parentNode/*.nextSibling*/; else     y=$(which).parentNode/*.nextSibling.nextSibling*/;
      (function(){ window.scrollBy(0,-100); }).delay(50); 
      $(x).set('tween',{duration:howLong}); // sets the time of fade in 1/1000 of a second
      $(y).set('tween',{duration:howLong}); // sets the time of fade in 1/1000 of a second
      $(x).tween('backgroundColor',[bright,normal]); // sets the time of fade in 1/1000 of a second
      $(y).tween('backgroundColor',[bright,normal]); // sets the time of fade in 1/1000 of a second
      }

//****************************************************************************************************************************
/*
  Developed by Robert Nyman, http://www.robertnyman.com
  Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/  
var getElementsByClassName = function (className, tag, elm){
  if (document.getElementsByClassName) {
    getElementsByClassName = function (className, tag, elm) {
      elm = elm || document;
      var elements = elm.getElementsByClassName(className),
        nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
        returnElements = [],
        current;
      for(var i=0, il=elements.length; i<il; i+=1){
        current = elements[i];
        if(!nodeName || nodeName.test(current.nodeName)) {
          returnElements.push(current);
        }
      }
      return returnElements;
    };
  }
  else if (document.evaluate) {
    getElementsByClassName = function (className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;
      var classes = className.split(" "),
        classesToCheck = "",
        xhtmlNamespace = "http://www.w3.org/1999/xhtml",
        namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
        returnElements = [],
        elements,
        node;
      for(var j=0, jl=classes.length; j<jl; j+=1){
        classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
      }
      try {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
      }
      catch (e) {
        elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
      }
      while ((node = elements.iterateNext())) {
        returnElements.push(node);
      }
      return returnElements;
    };
  }
  else {
    getElementsByClassName = function (className, tag, elm) {
      tag = tag || "*";
      elm = elm || document;
      var classes = className.split(" "),
        classesToCheck = [],
        elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
        current,
        returnElements = [],
        match;
      for(var k=0, kl=classes.length; k<kl; k+=1){
        classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
      }
      for(var l=0, ll=elements.length; l<ll; l+=1){
        current = elements[l];
        match = false;
        for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
          match = classesToCheck[m].test(current.className);
          if (!match) {
            break;
          }
        }
        if (match) {
          returnElements.push(current);
        }
      }
      return returnElements;
    };
  }
  return getElementsByClassName(className, tag, elm);
};
//****************************************************************************************************************************
function loadArray(varX) //pass in CLASS being sought ;  must be uniquely used for this function.
  {
  whichClass=varX;
  testArray=new Array();
  testArray=getElementsByClassName(whichClass);
  }

//****************************************************************************************************************************
function showIt(tDiv,fClass)  //pass in target DIV ID to hold content and CLASS of each line for the displayed TOC
  {
  contentsItem="";
  holdArrayItem="";
  holdAnchorName="";
  targetDiv=tDiv;
  formatClass=fClass;
  

  for (i=0;i<testArray.length;i++)
    {

// does an anchor exist   
if (Browser.Engine.trident) aClose='<\/A>'; else aClose = '<\/a>';
  hasAnchor=testArray[i].innerHTML.test(aClose,'i'); 
  if (hasAnchor) // remove anchor
      {   
          testArray[i].innerHTML=testArray[i].innerHTML.replace(/<\/a>/gi,aClose); // replace close of anchor to clear case-sensitivity
          testArray[i].innerHTML=testArray[i].innerHTML.substr((testArray[i].innerHTML.indexOf(aClose,0)+4)); // delete anchor and reset document value
      }

      //takes innerHTML with no anchor, clears codes and compresses to single string
      newAnchor = testArray[i].innerHTML;
      newAnchor = newAnchor.replace(/['"]/g,'');
      newAnchor = newAnchor.replace(/<BR>/gi,''); // delete line break if it exists
      newAnchor = newAnchor.replace(/<BR \/>/gi,''); // delete line break if it exists
      if (newAnchor.test('<')) //check for one other markup, such as a <span>
          { whichMarkupBegin=newAnchor.indexOf('<')+1;
            whichMarkupEnd=newAnchor.indexOf(' ',whichMarkupBegin);
            whichMarkup=newAnchor.substring(whichMarkupBegin,whichMarkupEnd);
            endofMarkup=newAnchor.indexOf('>',whichMarkupEnd);
            newAnchor=newAnchor.replace('<\/'+whichMarkup+'>','');
            whichMarkup=newAnchor.substring(whichMarkupBegin-1,endofMarkup+1);
            newAnchor=newAnchor.replace(whichMarkup,'');
          }
      newAnchor = newAnchor.replace(/&nbsp;/gi,' '); //clear any non-breaking spaces 
      tocItemLabel = newAnchor.clean(); // set up the claan label for the TOC used below to construct the TOC
      newAnchor = newAnchor.replace(/\s/gi,''); // clear all spaces
      //finished new anchor name/id composed of the innerHTML compressed to a single string = unique

      anchorTarget = '<a name=\"' + newAnchor + '\" id=\"' + newAnchor + '\"><\/a>'; //the anchor to insert as the target
      testArray[i].innerHTML=anchorTarget + testArray[i].innerHTML.clean(); //the document now has a properly coded anchor

      //now build each item for the TOC
          holdAnchorName='<li><div class=\"' + formatClass + '\"><a href=\"#'  + newAnchor + '\" onClick=\"notice(\''+ newAnchor + '\')\"\>'; // build an href for contents

          holdArrayItem=tocItemLabel; // equate to keep earlier code
//alert(tocItemLabel); return false;




    holdArrayItem=holdAnchorName + holdArrayItem + '<\/a><\/div><\/li>'; //build new contents line

    
    contentsItem=contentsItem+holdArrayItem;  // concatentate builds

    holdAnchorName=""; //clear variable for iterations
    holdArrayItem=""; //clear variable for iterations
    }
  document.getElementById(targetDiv).innerHTML="<ul>" + contentsItem; + "<\/ul>" // insert built item into target on page

  }
  

