//-----------------------------------
//
// window.DOJO.Write: write data to HTML
// -- Made by window.DOJO.Setup
//-----------------------------------

window.DOJO.Write = function(setup){
  this.share = setup.share;
};
window.DOJO.Write.prototype = {
  totalIds: 0,
  copy: function(id,i){
    return document.getElementById(id).children[i].cloneNode(1);
  },
  grandparent: function(el){
    return el.parentElement.parentElement;
  },
  grandkid: function(el,A){
    return A.reduce(function(el,i){
      return el.children[i];
    },el);
  },
  head: function(source,parent,cousin){
    var self = this.copy("proto",0);
    var offspring = this.grandkid(self,[1,1]);
    var id = this.totalIds++;
    var factsheet = [
      ["id", "in"+id],
      ["for", "in"+id],
      ["id", source.self]
    ];
    factsheet.forEach(function(tag,tagi){
      var temp = self.children[tagi];
      temp.setAttribute.apply(temp,tag);
    });
    self.children[0].checked = true;
    parent.appendChild(self);
    offspring.children[0].innerHTML = source.name;
    cousin.children[1].innerHTML = source.length;
  },
  body: function(source,parent,cousin){
    var grandparent = this.grandparent(cousin);
    var ancestor = this.grandparent(grandparent);
    var uncle = this.grandkid(ancestor,[1,1]);
    var size = source.dimensions;
    var dtype = source["data-type"];
    var channel = Number(!dtype.match(/float|uint8/)) + source.channel;
    var old = source.old.replace(/&channel=([^&]+)/,"&channel="+channel);
    var [w,h,d] = [Math.max(size.x,512),Math.max(size.y,512),size.z];
    var path = "viz.html?depth="+d+"&width="+w+"&height="+h;
    cousin.children[0].href = path + "&" + old;
    if (uncle.children[0].href) {
      uncle.children[0].href += "," + channel;
    }
    else {
      uncle.children[0].href = path +"&"+old;
    }
    cousin.children[1].innerHTML = dtype;
    uncle.children[1].innerHTML = [w,h,d].join(", ");
    grandparent.children[0].checked = false;
    ancestor.children[0].checked = false;
    var factsheet = [
      ["description", source["short-description"] || source["name"]],
      ["datasource", source.datasource || "unknown"],
      ["path", source.path || "unknown"]
    ];
    factsheet.forEach(function(items){
      var temp = this.copy("proto",2);
      var info = this.grandkid(temp,[0,0]);
      info.children[0].innerHTML = items[0];
      info.children[1].innerHTML = items[1];
      parent.appendChild(temp);
    },this);
  },
  main: function(terms){
    var source = this.share(terms,{self:terms.self.join(",")});
    var parent = document.getElementById(terms.parent.join(",") || "0");
    var cousin = this.grandkid(parent.parentElement,[1,1]);
    this[source.target](source,parent,cousin);
    return terms;
  }
}

@@ -1640,60 +1640,237 @@
     
-cousin.children%5B0%5D.href = path + %22&%22 + old;%0A    if (
+// Get the path for this channel%0A    this_path = path + %22&%22 + old;%0A    // Set the current group href%0A    current = cousin.children%5B0%5D;%0A    current.setAttribute(%22href%22, this_path);%0A    // Set the containing group href%0A    group = 
 uncl
@@ -1878,24 +1878,38 @@
 .children%5B0%5D
+%0A    if (group
 .href) %7B%0A   
@@ -1915,33 +1915,104 @@
     
-uncle.children%5B0%5D
+// Add the current channel to the containing group%0A      group.setAttribute(%22href%22, group
 .href +
-=
  %22,%22
@@ -2017,24 +2017,25 @@
 ,%22 + channel
+)
 ;%0A    %7D%0A    
@@ -2050,47 +2050,97 @@
     
+ 
- uncle.children%5B0%5D.href = path +%22&%22+old
+// Add the full path to the containing group%0A      group.setAttribute(%22href%22, this_path)
 ;%0A  

use addAttribute to set href