// Auto-generated Breadcrumbs
// Version: 0.5
// Created: 2006/01/11
// Author: Trevor Phillips

/*
   Generates Breadcrumbs for the current (or specified) page, based on
   data in a complete Sitemap.

   Created for the Research site.

   As of this current version, the format for the XML file goes something
   like:
      <ul>
         <li>Top Level (<a href="/top/">/top/</a>)
           <ul>
              <li>Sub 1 (<a href="/sub1/">/sub1/</a>)</li>
              <li>Sub 2 (<a href="/sub2/">/sub2/</a>)</li>
           </ul>
         </li>
      </ul>

   This will probably change!

   Also, the sitemap must be valid XML! You can skip the top <?xml... stuff, but
   all list items MUST be validly closed. Otherwise, pedantic browsers will barf.

   To use in your site, make a <span> with an ID (default "breadcrumbs") which
   will contain the Breadcrumbs. ie;
      <span id="breadcrumbs"></span>
   Then, up the top in the <head> somewhere, put:
      

*/

// Call to set up Breadcrumbs. This should probably not be in this file eventually...
setTimeout(function() {
   GetBreadcrumbs('/inc/sitemap.xml','breadcrumbs');
}, 200);

function GetBreadcrumbs(url,bcspan,thisurl)
{
   var httpRequest = BC_NewHTTPRequest();
   if (!httpRequest)
      return;

   if (!thisurl)
      thisurl = location.pathname;

   httpRequest.open("GET", url, false);
   // httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   httpRequest.send('');

   //alert(httpRequest.responseText);
   var sitemap = httpRequest.responseXML;

   if (!sitemap)
   {
      alert('Oops! Problem getting Sitemap XML. (Is the data XML compliant?)');
      return;
   }

   //alert(location.pathname);
   var out = sitemap.getElementsByTagName('a');
   var matchnode = false;
   for (var i=0; i<out.length;i++)
   {
      if (out[i].getAttribute('href') && out[i].getAttribute('href')==thisurl)
      {
//         alert("Bingo! "+location.pathname);
         matchnode=out[i];
      }
//      if (out[i].firstChild)
//         outtxt+=out[i].firstChild.nodeValue;
      // Firefox spreads content over multiple child nodes.
      //for (var j=0; j<out[i].childNodes.length;j++)
      //   if (out[i].childNodes[j].nodeValue)
      //      outtxt+=out[i].childNodes[j].nodeValue;
   }

   // Default Text for No Breadcrumbs
   var outtxt = '';
   if (matchnode)
   {
      outtxt = '';
      var wnode = matchnode;
      // Skip THIS node (since we want the parents...)
      while (wnode.tagName!='li' && wnode.parentNode)
      {
         wnode=wnode.parentNode;
      }

      while (wnode.parentNode)
      {
         wnode = wnode.parentNode;
//            alert("DBG: "+wnode.tagName);
         if (wnode.tagName=='li')
         {
            var thisout = '';
            var thishref = '';
            var thislabel = '';
//               alert('Checking childs: '+wnode.childNodes.length);
            for (var j=0; j<wnode.childNodes.length && wnode.childNodes[j].tagName!='li';j++)
            {
               var tcn = wnode.childNodes[j]; // This Child Node
               if (tcn.tagName=='a')
               {
                  thishref = tcn.getAttribute('href');
                  if (!thislabel)
                     thislabel = BC_GetXMLNodeContent(tcn);
               }
               else if (tcn.nodeValue && !thishref)
                  thislabel = tcn.nodeValue
            }
            // Strip off bracket for label for this style of link...
            thislabel = thislabel.replace(/\s*\(\s*/,'');
            thisout = '<a href="'+thishref+'">'+thislabel+'</a>';
            outtxt = thisout+(outtxt?' &gt;&gt; '+outtxt:'');
         }
      }
   }

   var bcspan = document.getElementById(bcspan);

   if (bcspan)
      bcspan.innerHTML = outtxt;

}


// ---------------------------------------------------------------
// HTTP & XML stuff
function BC_NewHTTPRequest()
{
   if (window.XMLHttpRequest)
   {
      return new XMLHttpRequest();
   }
   else
   if (window.ActiveXObject)
   {
      var xmlhttp=false;
      /*@cc_on @*/
      /*@if (@_jscript_version >= 5)
      // JScript gives us Conditional compilation, we can cope with old IE versions.
      // and security blocked creation of the objects.
      try {
         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
         xmlhttp = false;
      }
      }
      @end @*/
      return xmlhttp;
   }
}

// Like innerHTML, but works on XML too?
function BC_GetXMLNodeContent(obj)
{
   var outtxt = '';
   for (var j=0; j<obj.childNodes.length;j++)
      outtxt+=obj.childNodes[j].nodeValue;
   return outtxt;
}
