/* Breadcrumb.js 
  Functions that generate a breadcrumb from an XML site hierarchy file. */

var bcRequest = null;
var breadcrumbPageId = null;
var breadcrumbContainerId = null;

/* getBreadcrumb
  Request breadcrumb info.
  Parameters:
    pageId = Breadcrumb identifier of the current page. */
function getBreadcrumb(containerId) {
  breadcrumbPageId = getPageIdentifier();
  breadcrumbContainerId = containerId;
  bcRequest = initRequest(handleBreadcrumbRequest);
  submitRequest(bcRequest, "GET", "inc/Breadcrumb.xml", true);
}

/* handleBreadcrumbRequest Function
  Event handler that retrieves breadcrumb XML and assembles the breadcrumb. */
function handleBreadcrumbRequest() {
  if (bcRequest.readyState == 4) {
    if (bcRequest.status == 200) {
      var responseXml = bcRequest.responseXML;
      insertBreadcrumb(responseXml)
    } else {
      alert("The application is unable to communicate with the server. Response status: " + request.status + " " + request.statusText);
    }
  }
}

/* insertBreadcrumb Function
  Assembles a breadcrumb from XML and inserts it into the specified page element. */
function insertBreadcrumb(xmlDoc) {
  var root = xmlDoc.documentElement;
  var bcHtml = "";
  var nodes;
  var foundNode = null;
  var parentId;
  if (root.hasChildNodes()) {
    nodes = xmlDoc.getElementsByTagName("document");
    if (nodes == null) {
      alert("Unable to assemble breadcrumb. The site map contains no document nodes.")
    } else {
      foundNode = FindNodeByAttribute(nodes, "id", breadcrumbPageId);
      if (foundNode == null) {
        alert("Unable to assemble breadcrumb. The site map does not contain the specified page identifier: " + breadcrumbPageId);
      } else {
        bcHtml = foundNode.getAttribute("title");
        parentId = foundNode.getAttribute("parent");
        while (parentId != null) {
          foundNode = FindNodeByAttribute(nodes, "id", parentId);
          if (foundNode == null) {
            alert("Unable to assemble breadcrumb. The site map does not contain the specified parent identifier: " + parentId);
          } else {
            bcHtml = "<a href=\"" + foundNode.getAttribute("url") + "\">" + foundNode.getAttribute("title") + "</a> | " + bcHtml;
          }
          parentId = foundNode.getAttribute("parent");
        }
      }
      // Load the assembled breadcrumb into the container:
      var bcContainer = document.getElementById(breadcrumbContainerId);
      if (bcContainer == null) {
        alert("Unable to assemble breadcrumb. Container [" + breadcrumbContainerId + "] not found on page.");
      } else {
      	if (bcHtml.length > 0) bcContainer.innerHTML = bcHtml;
      }
    }
  } else {
    alert("Site hierarchy xml file is empty.");
  }
}

/* getPageIdentifier Function
  Extracts the current page name from the url and converts it to lower case. */
function getPageIdentifier() {
  var pageId = null;
  var pos;
  pageId = document.URL;
  // Strip the path:
  pos = pageId.lastIndexOf("/") + 1;
  if (pos > 0 && pos < pageId.length) {
    pageId = pageId.substr(pos);
    // Strip the extension:
    pos = pageId.lastIndexOf(".");
    if (pos > -1) {
      pageId = pageId.substr(0, pos);
    }
  } else {
    pageId = "default"
  }
  return pageId.toLowerCase();
}