// JavaScript Document

/* Code for producing breadcrumbs


   It iterates through the drop down menus, finding <a> tags, 

   keeping track of what the breadcrumbs would look like to that link.

   

   It compares the current page's URL to each menu option, looking ideally for an exact match.

   If it can't find an exact match, it chooses the menu link whose URL has the most in common

   with the current URL.

   

   For example, if the current URL is /products/artix/features.htm, then

   /products/ and /products/artix/ would both match but the latter would be a better match

   

   If no menu option has anything in common with the URL, e.g. /mtositoolkit/ might not

   match any menu option, then only 'Home' is displayed.

   

*/



var myURL=location.pathname;

var bestMatch='/';

var bestBreadcrumbs='<li class="home"><a href="/">Home</a></li>';

var previousLink='0';

function printBreadcrumbs() {

  var men=document.getElementById('mainNavigation');

  handleDiv(men,'<li class="home"><a href="/">Home</a></li>');

  bestBreadcrumbs = '<ul>' + bestBreadcrumbs + '</ul>';

  document.write(bestBreadcrumbs);

//  document.write(parseTags(men,''));

}

// Used for the secondary navigation

function printSecondaryBreadcrumbs() {

  var men=document.getElementById('linksLeft');

  handleSecDiv(men,'<li class="home"><a href="/">Home</a></li>');

  bestBreadcrumbs = '<ul>' + bestBreadcrumbs + '</ul>';

  document.write(bestBreadcrumbs);

//  document.write(parseTags(men,''));

}



// Handle a <ul> tag - assume it only has <li> subelements



function handleList(ul,prefix) {

  for (var i=0; i<ul.childNodes.length; i++) {

    handleListelem(ul.childNodes[i],prefix);

  }

}

function handleSecList(ul,prefix) {

  for (var i=0; i<ul.childNodes.length; i++) {

    handleSecListelem(ul.childNodes[i],prefix);

  }

}



// Handle a <div> or other container HTML tag

// Basically ignore it, and look for it's descendants till a <ul> is found



function handleDiv(div,prefix) {

  for (var i=0; i<div.childNodes.length; i++) {

    if ("DIV TABLE TBODY TR TD".indexOf(div.childNodes[i].tagName)!=-1) {

      handleDiv(div.childNodes[i],prefix);

    }

    if (div.childNodes[i].tagName=='UL')

      handleList(div.childNodes[i],prefix);

  }

}

function handleSecDiv(div,prefix) {

  for (var i=0; i<div.childNodes.length; i++) {

    if ("DIV".indexOf(div.childNodes[i].tagName)!=-1) {

      handleSecDiv(div.childNodes[i],prefix);

    }

    if (div.childNodes[i].tagName=='UL')

      handleSecList(div.childNodes[i],prefix);

  }

}



// Handle an <li> - will have a <a>, followed by potentially a submenu in the form of a <div>



function handleListelem(li, prefix) {

  var listtext='';

  for (var i=0; i<li.childNodes.length; i++) {

    if (li.childNodes[i].tagName=='A') {

      prefix=handleLink(li.childNodes[i],prefix);

    }

    var boringelements='DIV TABLE TBODY TR TD';

    if (boringelements.indexOf(li.childNodes[i].tagName)!=-1)

      handleDiv(li.childNodes[i],prefix);

  }

}

function handleSecListelem(li, prefix) {

  var listtext='';

  for (var i=0; i<li.childNodes.length; i++) {

    if (li.childNodes[i].tagName=='A') {

      prefix=handleSecLink(li.childNodes[i],prefix);

    }
    else if (li.childNodes[i].tagName=='SPAN') {
    
    	for (var n=0; n<li.childNodes[i].childNodes.length; n++) {

      		if (li.childNodes[i].childNodes[n].tagName=='A') {

      			prefix=handleSecSpanLink(li.childNodes[i].childNodes[n],prefix);

    		}
            
        }

    }

    else {

      handleSecList(li.childNodes[i],prefix);

    }

  }

}



// Handle a <a> - first ensure it begins with a /

// Then iterate over its child nodes looking for the text

// Add this link to the breadcrumbs that were passed into it

// Check if the current page URL begins with the URL of the current menu option

// If it does, compare it with the best match found so far, if it's longer

// (and thus a better match), replace the current best breadcrumbs with these ones.



function handleLink(a,prefix) {

  var url=a.pathname;

  if (url.charAt(0)!='/')

    url='/' + url;

  var linkText;

  for (var i=0; i<a.childNodes.length; i++) {

    if (a.childNodes[i].nodeType==3)

      linkText=a.childNodes[i].data;

  }

  var html=prefix + '<li><a href="' +url + '">' + linkText + '</a></li>';
  
  if (url.indexOf('welcome.htm')!=-1) {
  	url = url.toLowerCase().split("welcome.htm")[0];
  }

  if (myURL.indexOf(url)==0 && url.length>bestMatch.length) {

    bestMatch=url;

    bestBreadcrumbs=html;

  }

  return html;

}

function handleSecLink(a,prefix) {

  var url=a.pathname;

  if (url.charAt(0)!='/')

    url='/' + url;

  var linkText;

  for (var i=0; i<a.childNodes[0].childNodes[0].childNodes.length; i++) {

    if (a.childNodes[0].childNodes[0].childNodes[i].nodeType==3)

      linkText=a.childNodes[0].childNodes[0].childNodes[i].data;

  }

  var html=prefix + '<li><a href="' +url + '">' + linkText + '</a></li>';
  
  if (url.indexOf('welcome.htm')!=-1) {
  	url = url.toLowerCase().split("welcome.htm")[0];
  }

  if (myURL.indexOf(url)==0 && url.length>bestMatch.length) {

    bestMatch=url;

    bestBreadcrumbs=html;

  }

  return html;

}

function handleSecSpanLink(a,prefix) {

  var url=a.pathname;

  if (url.charAt(0)!='/')

    url='/' + url;

  var linkText;

  for (var i=0; i<a.childNodes.length; i++) {

    if (a.childNodes[i].nodeType==3)

      linkText=a.childNodes[i].data;

  }
  var newLink='<li><a href="' +url + '">' + linkText + '</a></li>';

  if (previousLink=='0') {
	var html=prefix + newLink;  
  }
  else {
    var html=prefix.split(previousLink)[0] + newLink;
  }
  
  previousLink=newLink;
  
  if (url.indexOf('welcome.htm')!=-1) {
  	url = url.toLowerCase().split("welcome.htm")[0];
  }

  if (myURL.indexOf(url)==0 && url.length>bestMatch.length) {

    bestMatch=url;

    bestBreadcrumbs=html;

  }

  return html;

}




// Show a parse tree of a given HTML node and its children



function parseTags(node,indent) {

  var s=indent;

  if (node.nodeType==3)

    s=s+"text:" + node.data + "<br />\n";

  else

    s=s+node.nodeType + "-" + node.tagName+"<br />\n";

  for (var i=0; i<node.childNodes.length; i++) {

      s=s+parseTags(node.childNodes[i],indent+'.');

  }

  return s;

}