/**
 * -----------------------------------------------------
 * Importer is a singleton object that imports document
 * elements from external files into the current document.
 * In addition to invoking document.import(), it adjusts
 * urls so that relative urls are resolved relative to the
 * imported document rather than the current document.
 */
Importer = {
  // It might be necessary in the future to add more tag
  // types and attributes to this list.
  // The only tags that should be added are tags that can
  // appear inside a <body> tag.
  // The only attributes that should be added for each tag
  // should be attributes that have url values.
  //
  // The url adjustment should be made into a separate
  // function.
  // This function can be used for getting style sheets.
  urlAttributeLists: {
    a: [
    "href"
    ],
    img: [
    "dynsrc",
    "longdesc",
    "lowsrc",
    "src",
    "usemap"
    ],
    object: [
    "codebase",
    "data"
    ]
  },
  /**
   * Importer.importElement(element, url) returns a new
   * element like the argument element that is suitable for
   * insertion into the current document.
   * Relative urls in the returned document are resolved
   * relative to the url argument, which should usually be
   * the url of the document from which the element argument
   * came.
   */
  importElement: function(element, url) {
    var children = element.getElementsByTagName("*");
    for (var i = 0; i < children.length; i++) {
      var child = children.item(i);
      var tagName = child.tagName.toLowerCase();
      var urlAttributeList = this.urlAttributeLists[tagName];
      if (urlAttributeList) {
        for (var j = 0; j < urlAttributeList.length; j++) {
          var attributeName = urlAttributeList[j];
          var oldURL = child.getAttribute(attributeName);
          if (oldURL) {
            var newURL = url.resolve(oldURL).toString();
            child.setAttribute(attributeName, newURL);
          }
        }
      }
    }
    element = document.importNode(element, true)
    return element;
  }
};



