document.addEventListener("DOMContentLoaded",function(){setTimeout(function(){
	var D=document;
	if(typeof window.WatchApp.namespace.model.player.NicoPlayerConnector.playerAreaConnector==='undefined' || !D.getElementById('nl_itab'))return setTimeout(arguments.callee, 500);
	D.getElementById('nl_itab').insertAdjacentHTML("BeforeEnd",'<a href="javascript:void(0)" id="nl_itab_mediainfo" onClick="nl_itab(\'mediainfo\'),nlMediaInfo.nl_itab_MI()">キャッシュ情報</a>');
	
	window.WatchJsApi.video.onVideoChanged(function(){setTimeout(function(){
		var D=document;
		window.nlMediaInfo.smid=window.WatchApp.namespace.model.player.NicoPlayerConnector.watchInfoModel.id;
		if(D.defaultView.getComputedStyle(D.getElementsByClassName('mediainfo')[0],'').getPropertyValue('display')=='block')window.nlMediaInfo.nl_itab_MI();
	},0)});
//--------------------------------------
var nlMediaInfo={
	befSize:0,
	Arr:[],
	data:{},
	
	smid:window.WatchApp.namespace.model.player.NicoPlayerConnector.watchInfoModel.id,
	MIfunc:function(){
		const T=this,F=arguments.callee,D=document;
		T.HttpRequest('cache/info?'+T.smid,function(json){
			if(json=JSON.parse(json.responseText)[T.smid]){
				var {videoId
					,cacheId
					,economy
					,size
					,caching
					,cachingSize
					,movieType
					,title
					,subFolder
					,ts
					,normalSize
					,normalCachingSize
					}=json;
				if(!caching){
					T.data.cacheType=economy?'[low]':'';
					T.data.cacheTitle=title;
					T.data.subFolder=subFolder?subFolder:'';
					T.data.ts=ts?T.dateChange(ts):'';
					T.mediainfo(cacheId);
					if(normalCachingSize){
						T.Arr=[];
						setTimeout(function(){var E;
						if(E=D.getElementById('mediainfoTable')){
							E.insertAdjacentHTML("AfterEnd",'<div id="mediainfoLowUP">キャッシュ中です... '+
							((normalCachingSize*100/normalSize)|0)+'% '+
							'(['+(((normalCachingSize-T.befSize)/1024)|0)+' Kbps] '+
							T.str_k(normalCachingSize)+' / '+T.str_k(normalSize)+
							' ダウンロード 予想完了時間'+(TIME>60?((TIME/60)|0)+'分'+TIME%60+'秒':TIME+'秒')+')</div>');
							T.befSize=normalCachingSize;
							setTimeout(T.lowUpdate.bind(T), 1000);
						}else{setTimeout(arguments.callee,500);}
						},500);
					}
				}else{
					var TIME=((size-cachingSize)/T.ave(T.Arr))|0;
					D.getElementsByClassName('mediainfo')[0].textContent='キャッシュ中です... '+
						((cachingSize*100/size)|0)+'% '+
						'(['+(((cachingSize-T.befSize)/1024)|0)+' Kbps] '+
						T.str_k(cachingSize)+' / '+T.str_k(size)+
						' ダウンロード 予想完了時間'+(TIME>60?((TIME/60)|0)+'分'+TIME%60+'秒':TIME+'秒')+')';
					T.Arr.unshift(cachingSize-T.befSize);
					if(T.Arr.length>60){
						T.Arr=T.Arr.slice(0,60);
					}
					T.befSize=cachingSize;
					setTimeout(F.bind(T),1000);
				}
			}else{
				D.getElementsByClassName('mediainfo')[0].textContent='キャッシュがありません';
				setTimeout(F.bind(T),1000);
			}
		},true);
	},
	ave:function(A){
		var sum=0,
			len=i=A.length;
		for(;i--;)sum+=A[i];
		return (sum/len)|0;
	},
	mediainfo:function(_smid){
		const T=this,
			  D=T.data,
			  HttpRequest=T.HttpRequest,
			  smid=T.smid,
			  isDeleted=window.WatchApp.namespace.model.player.NicoPlayerConnector.playerInitializeModel.flashVars.videoDetail.isDeleted,
			  Url='api/getthumbinfo/'+smid;
		var p,d;
		if((p=window.popThumb)&&p.addEvent){
			if((d=p.data["obj_"+smid]) && (d=d[1]).usingAPI.getthumbinfo){
				D.TI_video_id  =d.video_id;
				D.TI_movie_type=d.movie_type;
				D.TI_length    =d.length;
				var TI_size_high=d.size_high,
					TI_size_low =d.size_low;
				D.size_high=TI_size_high>0?T.str_m(TI_size_high):'None';
				D.size_low =TI_size_low >0?T.str_m(TI_size_low ):'None';
				if(D.resolution)T.MIwrite();
			}else if(!isDeleted){
				p.HttpRequest('http://'+location.host+'/pr_Redirect_ext.nicovideo.jp/'+Url,function(xml,h,o){
					xml=new DOMParser().parseFromString(xml,'application/xml');
					var selector=['video_id','movie_type','length'];
					for (var i=selector.length;i--;){
						D['TI_'+selector[i]]=xml.getElementsByTagName(selector[i])[0].textContent;
					}
					var TI_size_high=xml.getElementsByTagName('size_high')[0].textContent-0,
						TI_size_low =xml.getElementsByTagName('size_low')[0].textContent-0;
					D.size_high=TI_size_high>0?T.str_m(TI_size_high):'None';
					D.size_low =TI_size_low >0?T.str_m(TI_size_low ):'None';
					if(D.resolution)T.MIwrite();
				},true,smid,['api',smid])
			}else{
				p.HttpRequest('http://'+location.host+'/pr_Redirect_i.nicovideo.jp/v3/video.array?v='+smid,function(xml,h,o){
					xml=new DOMParser().parseFromString(xml,'application/xml');
					var selector=['video_id','movie_type','length'];
					/**/
					D.TI_video_id  =xml.querySelector('video > id').textContent;
					D.TI_movie_type=xml.querySelector('video > movie_type').textContent;
					var lis=xml.querySelector('video > length_in_seconds').textContent;
					D.TI_length=((lis/60)|0)+':'+(('0'+lis%60).slice(-2));
					/**/
					var TI_size_low =xml.getElementsByTagName('size_low')[0].textContent-0;
					D.size_high='None';
					D.size_low =TI_size_low >0?T.str_m(TI_size_low):'None';
					if(D.resolution)T.MIwrite();
				},true,smid,['api',smid])
			}
		}else{
			if(!isDeleted){
				HttpRequest(Url,function(xml){
					xml=new DOMParser().parseFromString(xml.responseText,'application/xml');
					var selector=['video_id','movie_type','length'];
					for (var i=selector.length;i--;){
						D['TI_'+selector[i]]=xml.getElementsByTagName(selector[i])[0].textContent;
					}
					var TI_size_high=xml.getElementsByTagName('size_high')[0].textContent-0,
						TI_size_low =xml.getElementsByTagName('size_low')[0].textContent-0;
					D.size_high=TI_size_high>0?T.str_m(TI_size_high):'None';
					D.size_low =TI_size_low >0?T.str_m(TI_size_low ):'None';
					if(D.resolution)T.MIwrite();
				},true);
			}else{
				D.TI_video_id  ='None';
				D.TI_movie_type='None';
				D.TI_length    ='None';
				D.size_high    ='None';
				D.size_low     ='None';
				
			}
		}
		
		HttpRequest('cache/mediainfo?'+_smid,function(xml){
			xml=new DOMParser().parseFromString(xml.responseText,'application/xml');
			var selector={
					General:['Format','Format_profile','Overall_bit_rate','File_size'],
					Video:['Format','Format_profile','Bit_rate_mode','Bit_rate','Frame_rate','Stream_size'],
					Audio:['Format','Format_profile','Bit_rate_mode','Bit_rate','Channel_s_','Sampling_rate','Stream_size']
				};
			for(var x in selector){
				for(var i=selector[x].length;i--;){
					var S=selector[x][i];
					if(!(s=T.qSele(xml,x,S))){
						D['MI_'+x+'_'+S]='';
						continue;
					}
					switch(S){
						case'File_size':
							s=s.textContent.replace(/(\.\d)\d/g,'$1');break;
						case 'Overall_bit_rate':
							s=s.textContent.replace(/ (?=\d)/g,',');break;
						case 'Bit_rate_mode':
							s='('+s.textContent+')';break;
						case 'Bit_rate':
							s=s.textContent.replace(/ (?!K)/g,',').replace(/\.\d/g,'');break;
						case 'Stream_size':
							var R=s.textContent.replace(/(\.\d)\d/g,'$1').match(/^(.+B) (.+)$/);
							s=R[1];
							D['MI_'+x+'_'+ S +'_percent']=R[2];break;
						default:
							s=s.textContent;break;
					}
					D['MI_'+x+'_'+S]=s;
				}
			}
			var MI_Video_Width =T.qSele(xml,'Video','Width'),
				MI_Video_Height=T.qSele(xml,'Video','Height');
			if(MI_Video_Width&&MI_Video_Height){
				var width =MI_Video_Width.textContent.slice(0,-7).replace(' ',','),
					height=MI_Video_Height.textContent.slice(0,-7).replace(' ',',');
				D.resolution=(width+' x '+height);
			}
			if(D.size_low)T.MIwrite();
		},true);
	},
	MIwrite:function(){
		const D=this.data;
		document.getElementsByClassName('mediainfo')[0].innerHTML='<table id="mediainfoTable" cellspacing="0" cellpadding="0"><tbody><tr>'+
				'<td>タイトル</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td colspan="9"><span id="cacheType">'+D.cacheType+'</span> <span id="cacheTitle">'+D.cacheTitle+'</span></td>'+
			'</tr><tr>'+
				'<td>動画情報</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td><span id="TI_video_id">'+D.TI_video_id+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td colspan="2"><span id="TI_movie_type">'+D.TI_movie_type+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="size_high">'+D.size_high+'</span></td><td></td>' +
				'<td class="nl_MIsep">/</td>'+
				'<td>Economy : <span id="size_low">'+D.size_low+'</span></td>' +
			'</tr><tr>'+
				'<td>形　式</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td><span id="MI_General_Format">'+D.MI_General_Format+'</span><span id="MI_General_Format_profile">'+D.MI_General_Format_profile+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_General_Overall_bit_rate">'+D.MI_General_Overall_bit_rate+'</span></td><td></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_General_File_size">'+D.MI_General_File_size+'</span></td><td></td>' +
				'<td class="nl_MIsep">/</td>'+
				'<td><span id="TI_length">'+D.TI_length+'</span></td>' +
			'</tr><tr>'+
				'<td>映　像</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td><span id="MI_Video_Format">'+D.MI_Video_Format+'</span> <span id="MI_Video_Format_profile">'+D.MI_Video_Format_profile+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_Video_Bit_rate">'+D.MI_Video_Bit_rate+'</span></td>'+
				'<td class="nl_textL nl_paddingL"><span id="MI_Video_Bit_rate_mode">'+D.MI_Video_Bit_rate_mode+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_Video_Stream_size">'+D.MI_Video_Stream_size+'</span></td>'+
				'<td class="nl_textR nl_paddingL"><span id="MI_Video_Stream_size_percent">'+D.MI_Video_Stream_size_percent+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td><span id="resolution">'+D.resolution+'</span> @ <span id="MI_Video_Frame_rate">'+D.MI_Video_Frame_rate+'</span></td>' +
			'</tr><tr>'+
				'<td>音　声</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td><span id="MI_Audio_Format">'+D.MI_Audio_Format+'</span> <span id="MI_Audio_Format_profile">'+D.MI_Audio_Format_profile+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_Audio_Bit_rate">'+D.MI_Audio_Bit_rate+'</span></td>'+
				'<td class="nl_textL nl_paddingL"><span id="MI_Audio_Bit_rate_mode">'+D.MI_Audio_Bit_rate_mode+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td class="nl_textR"><span id="MI_Audio_Stream_size">'+D.MI_Audio_Stream_size+'</span></td>'+
				'<td class="nl_textR nl_paddingL"><span id="MI_Audio_Stream_size_percent">'+D.MI_Audio_Stream_size_percent+'</span></td>'+
				'<td class="nl_MIsep">/</td>'+
				'<td><span id="MI_Audio_Sampling_rate">'+D.MI_Audio_Sampling_rate+'</span> @ <span id="MI_Audio_Channel_s_">'+D.MI_Audio_Channel_s_+'</span></td>'+
			'</tr><tr>'+
				'<td>ﾀｲﾑｽﾀﾝﾌﾟ</td>'+
				'<td class="nl_MIsep">:</td>'+
				'<td><span id="MI_ts">'+D.ts+'</span></td>'+
				'<td class="nl_MIsep">|</td>'+
				'<td class="nl_textL nl_paddingL" colspan="7">フォルダ : <span id="MI_subFolder">'+D.subFolder+'</span></td>'+
		'</tr></tbody></table>';
	},
	/**/
	lowUpdate:function(){
		const T=this
			,D=document
			,F=arguments.callee
			,smid=T.smid;
		T.HttpRequest('cache/info?'+smid,function(json){
			var {videoId
				,cacheId
				,economy
				,size
				,caching
				,cachingSize
				,movieType
				,title
				,subFolder
				,ts
				,normalSize
				,normalCachingSize
				}=(JSON.parse(json.responseText))[smid];
			if(!normalCachingSize){
				var E=D.getElementById('mediainfoLowUP');
				E.parentNode.removeChild(E);
				T.data.cacheType='';
				T.data.ts=ts?T.dateChange(ts):'';
				T.mediainfo(cacheId);
				return;
			}
			
			var TIME=((normalSize-normalCachingSize)/T.ave(T.Arr))|0;
			D.getElementById('mediainfoLowUP').textContent='キャッシュ中です... '+
					((normalCachingSize*100/normalSize)|0)+'% '+
					'(['+(((normalCachingSize-T.befSize)/1024)|0)+' Kbps] '+
					T.str_k(normalCachingSize)+' / '+T.str_k(normalSize)+
					' ダウンロード 予想完了時間'+(TIME>60?((TIME/60)|0)+'分'+TIME%60+'秒':TIME+'秒')+')';
			T.Arr.unshift(normalCachingSize-T.befSize);
			if(T.Arr.length>60){
				T.Arr=T.Arr.slice(0,60);
			}
			T.befSize=normalCachingSize;
			setTimeout(F.bind(T),1000);
		},true);
	},
	/**/
	qSele:function(xml,x,y){return xml.querySelector('track[type="'+x+'"] > '+y)},
	str_k:function(V){
		V=((V/1024).toLocaleString())+'';
		return V.slice(0,V.indexOf('.')+2)+' KiB';
	},
	str_m:function(V){
		V=((V/(1024*1024)).toLocaleString())+'';
		return V.slice(0,V.indexOf('.')+2)+' MiB';
	},
	dateChange:function(V){
		V=((new Date(V*1000)).toISOString()).split('T');
		[year,month,date]=V[0].split('-');
		[hour,minute,second]=(V[1].split('.'))[0].split(':');
		return year+'年'+month+'月'+date+'日'+hour+'時'+minute+'分'+second+'秒';
	},
	nl_itab_MI:function(){
		const T=this,D=document;
		var E;
		if(!D.getElementsByClassName('mediainfo').length){
			D.getElementById('nl_itab_Frame').insertAdjacentHTML("BeforeEnd",'<div class="mediainfo" style="display:block"></div>');
			T.MIfunc();
		}else if(!(E=D.getElementById('TI_video_id'))||E.textContent!=T.smid){
			T.befSize=0,T.Arr=[],T.data={};
			T.MIfunc();
		}
	},
	HttpRequest:function(url,func,Async,h){
		var httpObj=new XMLHttpRequest(),R='';
		httpObj.abort();
		httpObj.open('GET',url,!!Async);
		httpObj.setRequestHeader('If-Modified-Since','Thu, 01 Jun 1970 00:00:00 GMT');
		httpObj.onload=function(){if(typeof func=='function')R=func(httpObj,h)}
		httpObj.send(null);
		return R;
	}
}
window.nlMediaInfo=nlMediaInfo;
//--------------------------------------
},0)},false);