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

 /* キャッシュアイコンの表示＆色変え */
window.zenzaExt.zenzaExt3 = {
  addE1(aElement, aClassName) {
    var E = aElement.getElementsByClassName(aClassName);
    if (!E.length || !(E = E[0].getElementsByClassName('videoListFrame')).length) {
      return requestAnimationFrame(this.addE1.bind(this, aElement, aClassName));
    }
    
    const 
      iframeWin = E[0].contentWindow
      ,iframeDoc = iframeWin.document;
    if (!iframeDoc.getElementById('listContainer')) {
      return requestAnimationFrame(this.addE1.bind(this, aElement, aClassName));
    }
    
    var
      script = iframeDoc.createElement('script')
      ,link = document.createElement('link')
      ,fragment = document.createDocumentFragment();
    script.setAttribute('charset', 'utf-8');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', `//${location.host}/local/popThumb_FA.js`);
    
    link.setAttribute('type', 'text/css');
    link.setAttribute('charset', 'utf-8');
    link.setAttribute('rel', 'stylesheet');
    link.setAttribute('href', '/local/nlZenzaWatch-extension3.css');
    
    fragment.appendChild(script);
    fragment.appendChild(link);
    iframeDoc.body.appendChild(fragment);
    
    const
      Fnc = () => {
        if (!iframeWin.popThumb) return requestAnimationFrame(Fnc);
        //console.log('_nlZenza3 '+ 'Script1');
        iframeWin.popThumb.use_iframe = true;
        iframeWin.popThumb.inIframe = true;
        
        const ops = ("wait,use_SenBroStyle,use_VideoArrayAPI,use_VideoArrayAPI_so,use_cachedVideoArrayAPI,use_NicoCache_nl,use_NicoFetch,use_always_check,use_getCacheTitle,AllowCookie,use_in_JS_to_link,use_getOwner,key_now,recent_connect,isDebug,debugLog,nicofetch,nlMovieFetcher").split(",");
        ops.forEach(o => iframeWin.popThumb[o] = window.popThumb[o]);
      };
    new Promise(Fnc);
    /**/
    const
      observer = new MutationObserver(mutations => {
        for (let mutation of mutations) {
          if (mutation.type !== "childList") break;
          //console.log('ZWext : [obs] :'+ mutation.target.tagName);
          this.F1(mutation.addedNodes);
        };
      });
    observer.observe(iframeDoc.getElementById('listContainerInner'), {childList:true});
  },
  
  F1(aElements) {
    const smids = [for (i of aElements) i.getAttribute('data-watch-id')];
    
    window.zenzaExt
      .$XHR(`/cache/info/v2?${smids.join(',')}`, aElements)
      .then(([aJson, aElem]) => {
        const
          json = JSON.parse(aJson.responseText)
          ,_F1 = E => {
            const
              smid = E.getAttribute('data-watch-id')
              ,data = json[smid]
              ,videoLink = E.getElementsByClassName('videoLink')[0];
            //console.log('_nlZenza3 cache: '+ E.getAttribute('data-watch-id') +' ['+ !!data +']')
            
            videoLink.setAttribute('onMouseOver', `popThumb2('${smid}', 0)`);
            videoLink.setAttribute('onMouseOut', `nd()`);
            
            if (!!data) {
              const preferred = data["preferred"];
              if (!preferred) return;
              
              const
                isEconomy = data["caches"][preferred]["economy"]
                ,isDmc = data["caches"][preferred]["dmc"];
              
              E.getElementsByClassName('thumbnail')[0].insertAdjacentHTML("beforeBegin", `<div class="Z_cacheIcon Z_${!isDmc? (!isEconomy? 'cache': 'economy'): (!isEconomy? 'dmcCache': 'dmcEconomy')}IconImgMin"></div>`);
              E.getElementsByClassName('title')[0].firstElementChild.setAttribute('style', `color:#${!isDmc? (!isEconomy? 'C00000': 'C08000'): (!isEconomy? '00A000': 'A0A000')};`);
            };
          };
        Array.from(aElem).forEach(e => Promise.resolve(_F1(e)));
      });
  },

  addZenzaEvent(){
    const zenzaWatchVideoInfoPanel = document.getElementsByClassName('zenzaWatchVideoInfoPanel')[0];
    window.zenzaExt.zenzaExt3.addE1(zenzaWatchVideoInfoPanel, 'relatedVideoContainer');
    window.zenzaExt.zenzaExt3.addE1(zenzaWatchVideoInfoPanel, 'playlist-frame');
    
    window.zenzaExt.removeVideoLoadEventListener([window.zenzaExt.zenzaExt3.addZenzaEvent]);
  },
};

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