var svnwikiTagsLoader = new Object();

svnwikiTagsLoader.showTags = function (url, tag, id)
{
  var container = document.getElementById("list-pages-" + id);
  if (container.style.display == 'none') {
    if (!container.hasChildNodes()) {
      container.appendChild(svnwikiJavascript.makeElement('li', document.createTextNode("Loading ...")));
      svnwikiJavascript.loadXml(url + '/' + tag + '.xhtml', function (xmlDoc) { svnwikiTagsLoader.updateTags(url, tag, container, xmlDoc); });
    }
    container.style.display = 'inline';
  } else
    container.style.display = 'none';
}

svnwikiTagsLoader.makeLinkFile = function (link, text)
{
  var linkObj = svnwikiJavascript.makeLink(link, document.createTextNode(text));
  linkObj.setAttribute('class', 'related-pages');
  return (svnwikiJavascript.makeElement('li', linkObj));
}

svnwikiTagsLoader.iterateListOfLinks = function (xmlDocFiles, func)
{
  for (var i = 0; i < xmlDocFiles.childNodes.length; i ++) {
    if (xmlDocFiles.childNodes[i].nodeType == Node.ELEMENT_NODE) {
      var datum = xmlDocFiles.childNodes[i].childNodes[0];
      func(datum.getAttribute('href'), datum.firstChild.nodeValue, xmlDocFiles.childNodes[i].getAttribute('class'));
    }
  }
}

svnwikiTagsLoader.updateTags = function (url, tag, container, xmlDoc)
{
  svnwikiJavascript.cleanContent(container);
  svnwikiTagsLoader.iterateListOfLinks(xmlDoc.getElementById("files-for-tag"),
    function (href, name) {
      var link = svnwikiTagsLoader.makeLinkFile(url + '/' + href + '?tag=' + tag, name);
      link.setAttribute('class', 'related-pages');
      container.appendChild(link);
      container.appendChild(document.createTextNode("\n"));
    });
}

svnwikiTagsLoader.showRelated = function (tag)
{
  var info = new Object;
  info.tagsFound = new Object;
  info.tag = tag;
  info.div = document.getElementById("xsvnwiki-tags-related-body");
  info.filesFound = new Array;
  info.filesError = new Array;
  svnwikiTagsLoader.iterateListOfLinks(document.getElementById("files-for-tag"),
    function (href, name) {
      info.filesFound.push(href);
    });
  info.filesLoaded = 0;
  svnwikiTagsLoader.relatedUpdate(info);
}

svnwikiTagsLoader.relatedUpdate = function (info) {
  svnwikiJavascript.cleanContent(info.div);

  if (info.filesFound.length == 0) {
    info.div.appendChild(svnwikiJavascript.makeElement('p', document.createTextNode("Ooops, we don't know how to compute the tags related to an empty tag.")));
    return;
  }

  var p = svnwikiJavascript.makeElement('p');
  if (info.filesLoaded == info.filesFound.length) {
    p.appendChild(document.createTextNode("The following are tags related with the tag "));
    p.appendChild(svnwikiJavascript.makeElement('i', document.createTextNode(info.tag)));
    p.appendChild(document.createTextNode(":"));
  } else {
    p.appendChild(document.createTextNode("Loading tags for file "));
    p.appendChild(svnwikiJavascript.makeElement('i', document.createTextNode(info.filesFound[info.filesLoaded])));
    p.appendChild(document.createTextNode(" (" + Math.round(100 * info.filesLoaded / info.filesFound.length) + "%)"));
  }
  info.div.appendChild(p);

  var ul = svnwikiJavascript.makeElement('ul');
  ul.setAttribute("class", "tags-list");
  var copy = new Array;
  for (tag in info.tagsFound)
    copy.push(tag);
  copy.sort(function (a, b) { return info.tagsFound[b] - info.tagsFound[a]; });
  for (var i in copy) {
    var tag = copy[i];
    var linkObj = svnwikiJavascript.makeLink(tag, document.createTextNode(tag + " (" + info.tagsFound[tag] + ")"));
    //linkObj.setAttribute("class", "tag");
    var li = svnwikiJavascript.makeElement('li', linkObj);
    //li.setAttribute("class", "tag");
    ul.appendChild(li);
    ul.appendChild(document.createTextNode("\n"));
  }
  info.div.appendChild(ul);

  if (info.filesError.length > 0) {
    info.div.appendChild(svnwikiJavascript.makeElement('p', document.createTextNode("Errors were detected with the following files:")));
    var ul = svnwikiJavascript.makeElement('ul');
    ul.setAttribute("class", "files-list");
    for (var i = 0; i < info.filesError.length; i ++) {
      var li = svnwikiJavascript.makeElement('li', document.createTextNode(info.filesError[i]));
      li.setAttribute("class", "file");
      ul.appendChild(li);
    }
    info.div.appendChild(ul);
  }

  if (info.filesLoaded < info.filesFound.length) {
    var components = String.split(info.filesFound[info.filesLoaded++], "/");
    var last = components.pop(), file = "";
    components.push("xsvnwiki-tags-file/" + last);
    for (var i in components)
      file = file + (file == "" ? "" : "/") + components[i];
    file = file + ".xhtml";
    svnwikiJavascript.loadXml(file, function (xmlDoc) { svnwikiTagsLoader.relatedRegister(info, xmlDoc); });
  }
}

svnwikiTagsLoader.relatedRegister = function (info, xmlDoc) {
  var files = xmlDoc.getElementById("tags-for-file");
  if (files)
    svnwikiTagsLoader.iterateListOfLinks(files,
      function (href, name) {
        if (name != info.tag)
          info.tagsFound[name] = typeof(info.tagsFound[name]) == "undefined" ? 1 : info.tagsFound[name] + 1;
      } );
  else
    info.filesError.push(info.filesFound[info.filesLoaded - 1]);
  svnwikiTagsLoader.relatedUpdate(info);
}

svnwikiTagsLoader.hideById = function (id) {
  var obj = document.getElementById('tag-list-files');
  if (!obj)
    return;
  obj.style.display = 'none'
}

// TODO: The list of elements to hide should not be hardcoded here!
svnwikiTagsLoader.hideAll = function () {
  svnwikiTagsLoader.hideById('tag-list-files');
  svnwikiTagsLoader.hideById('tag-list-images');
  svnwikiTagsLoader.hideById('tag-list-googlemap');
}

svnwikiTagsLoader.displayFiles = function () {
  svnwikiTagsLoader.hideAll()
  document.getElementById('tag-list-files').style.display = 'inline';
}

svnwikiTagsLoader.onLoad = function (data) {
  svnwikiJavascript.queryStringInit();
  var view = svnwikiJavascript.queryString['view'];
  if (!data[view])
    return;
  data[view]();
}