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

window.nl_watch.nlContentsTree = {
  init() {
    if(!document.getElementById('nl_itab_Header')) return requestAnimationFrame(this.init.bind(this));
    
    this.nl_itab_CT(window.nl_watch.apiData.video.id);
  },
  
  nl_itab_CT(aSmid) {
    window.nl_watch.$XHR(`http://${location.host}/pr_Redirect_api.commons.nicovideo.jp/tree/summary/get?id=${aSmid}&limit=1000`, aSmid).then(this.CT.bind(this));
  },

  CT([aJson, aSmid]) {
    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.getElementById('nl_itab_tree'))) {
      if (!arr.length) return;
      document.getElementById('nl_itab_Header')
        .insertAdjacentHTML("BeforeEnd", `<a href="javascript:void(0)" id="nl_itab_tree" class="nl_itab_Headers" onClick="nl_watch.nl_itab(this, 'tree');">コンテンツツリー[${arr.join('|')}]</a>`);
      document.getElementById('nl_itab_Frame')
        .insertAdjacentHTML("BeforeEnd", '<div class="tree nl_itab_Bodies"></div>');
    }else{
      if (!arr.length) {
        V.setAttribute('style', 'display;none;');
        return;
      }else{
        V.textContent = `コンテンツツリー[${arr.join('|')}]`;
        V.setAttribute('style', 'display:inline-block;');
      }
    }
    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.getElementById('nl_itab_Frame').getElementsByClassName('tree')[0].innerHTML = arr.join('');
  },

  Template({thumbnail, registered_date, url, parentid, title}) {
    return (
    '<div class="treeFlame">'+
      `<div class="treeFlame_Left"><img src="${thumbnail}" class="treeFlame_Img"></div>`+
      '<div class="treeFlame_Right">'+
        `<div>登録日時：'${registered_date}</div>`+
        `<div><a href="${url}" onmouseover="return popThumb2('${parentid}',0)" onmouseout="return nd()">${title}</a></div>`+
      '</div>'+
    '</div>');
  },
};

//window.nl_watch.nlContentsTree.init.call(window.nl_watch.nlContentsTree);
window.nl_watch.eventListenerMap.add(window.nl_watch.nlContentsTree.init.bind(window.nl_watch.nlContentsTree))
});