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

/* サムネイル画像クリックでLargeサムネイル表示([動静]画) */
window.popThumb.extLThumb = {
  
  space:10,//Largeサムネイル画像の回りのスペース(px)
  
  getStyle(aElement, aStyle) {
    return (document.defaultView.getComputedStyle(aElement, '').getPropertyValue(aStyle).slice(0, -2)) -0
  },
  
  movie(aEvent) {
    const
      E = aEvent.target
      ,inIframe = window.popThumb.inIframe
      ,getStyle = this.getStyle;
    
    var popMovie = E.parentNode
    while (!popMovie.classList.contains('pop_movie')) popMovie = popMovie.parentNode;
    
    aEvent.preventDefault();
    
    if (!popMovie.lastElementChild.classList.contains('nl_largeThumb')) {
      let
        LImg = '<div class="nl_largeThumb_"'+
          ` style="line-height:calc(${getStyle(popMovie, 'height')}px + ${(this.space * 2)}px${inIframe? ' +  10px': ''});">`+
            'No LargeImage'+
        '<div>';
        
      //if (!popMovie.firstElementChild.firstElementChild.getElementsByTagName('a')[0].getAttribute('href').split('/').pop().indexOf('sm')) {
      const id = popMovie.parentNode.id.split('_').pop();
      if (!id.indexOf('sm') && (id.slice(2) -0) >= 16371888) {
        LImg = `<img class="nl_largeThumb_" alt="now Loading" onMouseDown="event.preventDefault();" onLoad="window.popThumb.extLThumb.imgLoaded.apply(window.popThumb.extLThumb, [this, event]);"`+
          ` src="${E.getAttribute('src').replace(/\..$/, '')}.L"`+
          ` style="line-height:calc(${getStyle(popMovie, 'height')}px + ${(this.space * 2)}px${inIframe? ' +  10px': ''});">`
      }
      //window.popThumb._XMLHttpRequest(`/pr_Redirect_${E.getAttribute('src').slice(7)}.L`, true, function(r){console.log('imgL: true'+' : '+r.status +' '+ r.statusText)})
      
      popMovie.insertAdjacentHTML("BeforeEnd",
        '<div class="nl_largeThumb" '+
          'onClick="window.popThumb.extLThumb.clickEvent(this, event);" '+
          'onMouseOut="window.popThumb.extLThumb.mouseOutEvent(this, event);">'+
          LImg+
        '</div>');
    } else {
      this.imgVisible(popMovie, inIframe);
    }
  },
  
  seiga(aEvent) {
    const
      E = aEvent.target
      ,inIframe = window.popThumb.inIframe;
    
    var popSeiga = E.parentNode;
    while (!popSeiga.classList.contains('pop_seiga_im')) popSeiga = popSeiga.parentNode;
    
    aEvent.preventDefault();
    
    if (!popSeiga.lastElementChild.classList.contains('nl_largeThumb')) {
      popSeiga.insertAdjacentHTML("BeforeEnd",
        '<div class="nl_largeThumb" '+
          'onClick="window.popThumb.extLThumb.clickEvent(this, event);" '+
          'onMouseOut="window.popThumb.extLThumb.mouseOutEvent(this, event);">'+
          `<img class="nl_largeThumb_" alt="now Loading" onMouseDown="event.preventDefault();" onLoad="window.popThumb.extLThumb.imgLoaded.apply(window.popThumb.extLThumb, [this, event]);"`+
            ` src="${E.getAttribute('src').replace('q?', 'l?')}"`+
            ` style="line-height:calc(${this.getStyle(popSeiga, 'height')}px + ${this.space *2}px${inIframe? ' + 10px': ''});"`+
          `>`+
        '</div>');
    } else {
      this.imgVisible(popSeiga, inIframe);
    }
  },
/*---------------------------------------*/
  imgLoaded(aThis, aEvent) {
    const
      getStyle = this.getStyle
      ,E = aThis
      ,H = E.naturalHeight
      ,W = E.naturalWidth
      ,space = this.space *2;
    var
      thumb = E.parentNode.parentNode.parentNode
      ,S = 0;
    
    if (window.popThumb.inIframe) {
      const
        thumbTopBoder = getStyle(thumb, 'border-top-width')
        ,thumbBottomBoder = getStyle(thumb, 'border-bottom-width');
      
      thumb.style.setProperty('height', `calc(100% - (${thumbTopBoder}px + ${thumbBottomBoder}px))`);
      thumb.parentNode.style.setProperty('margin', `0`);
      thumb.parentNode.style.setProperty('height', `100%`);/* nico_popThumb_container */
      document.body.style.setProperty('height', `100%`);
      document.documentElement.style.setProperty('height', `100%`);
      
      thumb = window.parent.document.getElementById(thumb.id);
      S = thumbTopBoder +thumbBottomBoder;
    }
    
    const thumbStyle = thumb.style;
    thumbStyle.setProperty('min-width', `calc(${W}px + ${space}px + ${S}px)`);
    thumbStyle.setProperty('min-height',`calc(${H}px + ${space}px + ${S}px)`);
    E.style.setProperty('line-height', `calc(${H}px + ${space}px + ${S}px)`);
    
    this.imgPosition({"Thumb":thumb, "Width":W +space +S, "Height":H +space +S,});
  },
  
  imgVisible(aElement, inIframe) {
    aElement.lastElementChild.style.removeProperty('display');
    
    let thumb = aElement.parentNode;
    if (inIframe) thumb = window.parent.document.getElementById(thumb.id);
    
    const
      getStyle = this.getStyle
      ,thumbStyle = thumb.style
      ,LThumbWidth = thumb.getAttribute('data-LThumb-width')
      ,LThumbHeight = thumb.getAttribute('data-LThumb-height')
      ,W = LThumbWidth.match(/\((\d+)px \+ (\d+)px \+ (\d+)px/)
      ,H = LThumbHeight.match(/\((\d+)px \+ (\d+)px \+ (\d+)px/);
    
    thumbStyle.setProperty('min-width', LThumbWidth);
    thumbStyle.setProperty('min-height', LThumbHeight);
    
    let
      Width = (W[1]-0) +(W[2]-0) +(W[3]-0)
      ,Height = (H[1]-0) +(H[2]-0) +(H[3]-0)
      ,thumbWidth = getStyle(thumb, 'width')
      ,thumbHeight = getStyle(thumb, 'height');
    if(Width < thumbWidth) Width = thumbWidth;
    if(Height < thumbHeight) Height = thumbHeight;
    
    this.imgPosition({"Thumb":thumb, "Width":Width, "Height":Height,});
  },
  
  imgPosition({Thumb, Width, Height,}){
    const
      getStyle = this.getStyle
      ,thumbStyle = Thumb.style
      ,win = window.popThumb.inIframe? window.parent: window
      ,thumbStyleTop = getStyle(Thumb, 'top')
      ,thumbStyleLeft = getStyle(Thumb, 'left')
      ,offsetBottom = win.pageYOffset +win.document.documentElement.clientHeight
      ,offsetRight = win.pageXOffset +win.document.documentElement.clientWidth
      ,diffBottom = (thumbStyleTop +Height +getStyle(Thumb, 'border-top-width') +getStyle(Thumb, 'border-bottom-width')) -offsetBottom
      ,diffRight = (thumbStyleLeft +Width +getStyle(Thumb, 'border-left-width') +getStyle(Thumb, 'border-right-width')) -offsetRight;
    if (diffBottom > 0) {
      thumbStyle.setProperty('top', thumbStyleTop -diffBottom +'px');
    }
    if (diffRight > 0){
      thumbStyle.setProperty('left', thumbStyleLeft -diffRight +'px');
    }
  },
  
  mouseOutEvent(aThis, aEvent) {
    var E = aEvent.relatedTarget;
    if (aEvent.ctrlKey || !E
         || E == aThis // nl_largeThumb_ から出たとき
         || E.parentNode == aThis // nl_largeThumb_ に入ったとき
         || aThis.style.getPropertyValue('display') == 'none' // ↓clickEvent() でnl_largeThumbを消したとき
       ) return;
    
    (window.popThumb.inIframe? window.parent: window).popThumb.isSticky[aThis.parentNode.parentNode.id.substring(6)] = false;
    
    this.clickEvent(aThis, aEvent);
  },
  
  clickEvent(aThis, aEvent) {
    if (aEvent.ctrlKey) return;
    aThis.style.setProperty('display', 'none');
    let
      thumb = (
          window.popThumb.inIframe?
            window.parent.document.getElementById(aThis.parentNode.parentNode.id)
            :aThis.parentNode.parentNode
          )
      ,style = thumb.style;
    
    thumb.setAttribute('data-LThumb-width', style.getPropertyValue('min-width'));
    thumb.setAttribute('data-LThumb-height', style.getPropertyValue('min-height'));
    style.removeProperty('min-width');
    style.removeProperty('min-height');
  },
};
/*---------------------------------------*/
popThumb.addPopThumbEventListener("onPopupMakeElementEnd", function (aElement) {
  if (aElement.firstElementChild.classList.contains('pop_movie')) {
    aElement.getElementsByClassName('video_img')[0]
      .addEventListener("click", window.popThumb.extLThumb.movie.bind(window.popThumb.extLThumb), true);
  } else if (aElement.firstElementChild.classList.contains('pop_seiga_im')) {
    aElement.getElementsByClassName('center_img_inner')[0].firstElementChild
      .addEventListener("click", window.popThumb.extLThumb.seiga.bind(window.popThumb.extLThumb), true);
  }
});
/*---------------------------------------*/
var link = document.createElement('link');
link.setAttribute('href', '/local/popThumb_FA-extension_largeThumb.css');
link.setAttribute('type', 'text/css');
link.setAttribute('charset', 'utf-8');
link.setAttribute('rel', 'stylesheet');
document.body.appendChild(link);
/*---------------------------------------*/
});
