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

window.zenzaExt = {
  eventListenerMap: new Set(),

  addVideoLoadEventListener(aFunc) {
    if (Object.prototype.toString.call(aFunc).slice(1,-1).split(' ')[1] === 'Array') {
      aFunc.forEach(f => this.eventListenerMap.add(f));
    } else {
      this.eventListenerMap.add(aFunc);
    }
  },
  removeVideoLoadEventListener(aFunc) {
    if (Object.prototype.toString.call(aFunc).slice(1,-1).split(' ')[1] === 'Array') {
      aFunc.forEach(f => this.eventListenerMap.delete(f));
    } else {
      this.eventListenerMap.delete(aFunc);
    }
  },

  init() {
    window.zenzaExt.addVideoLoadEventListener([window.zenzaExt.getSmid.bind(window.zenzaExt)]);
    
    var zenzaVideoPlayerDialog = document.getElementsByClassName('zenzaVideoPlayerDialog');
    
    if (!zenzaVideoPlayerDialog.length) return requestAnimationFrame(this.init.bind(this));
    if (zenzaVideoPlayerDialog[0].getElementsByClassName('videoPlayer').length) return this.init2();
    
    const
      observer = new MutationObserver(mutations => {
        for (let mutation of mutations) {
          if (mutation.type !== "childList") break;
          for (let addedNode of mutation.addedNodes) {
            if (addedNode.nodeName.toLowerCase() === "video") {
              this.init2();
              observer.disconnect();
            }
          }
        }
      });
    
    observer.observe(
      zenzaVideoPlayerDialog[0]
      ,{childList:true, subtree:true,}
    );
  },

  LoadingFlag: false,
  init2() {
    const
      observer2 = new MutationObserver(mutations => {
        for (let mutation of mutations) {
          if (mutation.type !== "attributes") break;
          let loading = mutation.target.classList.contains('is-loading');
          if (loading && !this.LoadingFlag) {
            //console.log('_nlZenza1 [' + zenzaExt.eventListenerMap +']')
            this.eventListenerMap.forEach(aValue => Promise.resolve(aValue()));
          }
          this.LoadingFlag = loading;
          /*
          console.log('_nlZenzaOld '+mutation.oldValue);
          console.log('_nlZenzaNew '+mutation.target.classList);
          */
        };
      });
    
    observer2.observe(
      document.getElementsByClassName('zenzaVideoPlayerDialog')[0].getElementsByClassName('videoPlayer')[0],
      {attributes:true,/* attributeOldValue:true,*/ attributeFilter:['class'],}
    );
  },
  
  smid: '',
  getSmid() {
    this.smid = document.getElementsByClassName('zenzaWatchVideoHeaderPanel')[0].getElementsByClassName('uadLink')[0].getAttribute('href').split('=')[1];
  },
  
  $XHR(aUrl, aParam) {
    return new Promise((resolve, reject) => {
      var httpObj = new XMLHttpRequest();
      httpObj.open('GET', aUrl, true);
      httpObj.onload = () => resolve((aParam === undefined)? httpObj: [httpObj, aParam]);
      httpObj.onerror = () => reject(httpObj.error);
      httpObj.send();
    });
  },
};
window.zenzaExt.init();

var
  script = document.createElement('script')
  ,fragment = document.createDocumentFragment()
  ,A = [
      "/local/nlZenzaWatch-extension2.js",
      "/local/nlZenzaWatch-extension3.js",
      "/local/nlZenzaWatch-extension4.js",
      "/local/nlZenzaWatch-extension5.js",
    ];

for (let a of A) {
  let clone = script.cloneNode(true);
  clone.setAttribute('charset', 'utf-8');
  clone.setAttribute('src', `//${location.host}${a}`);
  fragment.appendChild(clone);
}
document.body.appendChild(fragment);

});
