new Promise(()=>{
 "use strict";

window.zenzaExt.zenzaExt4 = {
  addZenzaEvent(){
    var link = document.createElement('link');
    link.setAttribute('href', '/local/nlZenzaWatch-extension4.css');
    link.setAttribute('type', 'text/css');
    link.setAttribute('charset', 'utf-8');
    link.setAttribute('rel', 'stylesheet');
    document.body.appendChild(link);
    
    window.zenzaExt.zenzaExt4.addZenzaEvent2.call(window.zenzaExt.zenzaExt4);
    window.zenzaExt.removeVideoLoadEventListener([window.zenzaExt.zenzaExt4.addZenzaEvent]);
    window.zenzaExt.addVideoLoadEventListener([window.zenzaExt.zenzaExt4.addZenzaEvent2.bind(window.zenzaExt.zenzaExt4)]);
  },
  
  addZenzaEvent2(){
    const smid = window.zenzaExt.smid;
    window.zenzaExt.$XHR(`/pr_Redirect_api.commons.nicovideo.jp/tree/summary/get?id=${smid}&limit=1000`, smid).then(this.CT.bind(this));
  },
//------------------------------------------------  
  CT([aJson, aSmid]) {
    const
      zenzaWatchVideoInfoPanel = document.getElementsByClassName('zenzaWatchVideoInfoPanel')[0]
      ,tabSelectContainer = zenzaWatchVideoInfoPanel.getElementsByClassName('tabSelectContainer')[0];
    
    var
      json = JSON.parse((aJson.responseText).replace(/^.+?\((\{.+\})\);$/, '$1'))
      ,arr = []
      ,V;
    
    if (V = json.countparent - 0) arr.push('親:' + V);
    if (V = json.countchild - 0)  arr.push('子:' + V);
    
    if (!(V = document.getElementsByClassName('zwContentsTreeTab')[0])) {
      if (!arr.length) return;
      tabSelectContainer
        .insertAdjacentHTML("BeforeEnd", `<div class="tabSelect zwContentsTreeTab" data-tab="zwContentsTree" id="nl_itab_tree">ツリー[${arr.join('|')}]</div>`);
      tabSelectContainer
        .insertAdjacentHTML("AfterEnd", '<div class="tabs zwContentsTree"></div>');
    } else {
      if (!arr.length) {
        V.setAttribute('style', 'visibility:collapse;');
        return;
      }else{
        V.textContent = `ツリー[${arr.join('|')}]`;
        V.setAttribute('style', 'visibility:visible;');
      }
    }
    arr = [for(a of json.parent) this.Template(a)];
    arr.push(`<div style="background-color:#444; text-align:center; border-radius: 0px 0px 25px 25px;"><a href="http://commons.nicovideo.jp/tree/${aSmid}" target="_tab" style="color:#FFF;">コンテンツツリーを見る</a></div>`);
    document.getElementsByClassName('zwContentsTree')[0].innerHTML = arr.join('');
  },

  Template({thumbnail, registered_date, url, parentid, title}) {
    return (
    '<div class="zw_treeFlame">'+
      `<div class="zw_treeFlame_Left"><img src="${thumbnail}" class="zw_treeFlame_Img"></div>`+
      '<div class="zw_treeFlame_Right">'+
        //`<div>${registered_date}</div>`+
        `<div><a href="${url}" onmouseover="return popThumb2('${parentid}',0)" onmouseout="return nd()">${title}</a></div>`+
      '</div>'+
    '</div>');
  },
};

window.zenzaExt.addVideoLoadEventListener([window.zenzaExt.zenzaExt4.addZenzaEvent]);
});