(function(){
var getFullDesc;
/*------------------------------------------*/
/*------------------ 設定 ------------------*/
// video.array使用時の複数収得許可
getFullDesc.getAllID=true;
// 上記trueの場合、shiftを押しながらクリック時のみvideo.array複数収得
getFullDesc.getAllIDpressShift=true;
// video.arrayで複数収得時の最大件数
getFullDesc.maxGetID=10;
// 説明文収得時にタグ追加
getFullDesc.setTags=true;
// ページロード時にキャッシュ済みタグ追加
getFullDesc.setTagsLoad=true;
/*-------------- 設定ここまで --------------*/
/*------------------------------------------*/
getFullDesc.url=function(L){
	switch(L[0]){
		case 'ranking':return 'Ranking';
		case 'mylist':return 'Mylist';
		case 'my':return (L[1]=='history'?'History' :L[1]=='mylist'?'MyMylist' :null);
		case 'search':return 'Search';
		case 'tag':return 'Tag';
		default:return null;
	}
}(location.href.substring(24).split('/'));
getFullDesc.getThumbDesc=function(smid){
	var T=this,
		  P=window.popThumb,
		  data=P.data,
		  HttpRequest=P.HttpRequest,
		  to_link=P.use_in_JS_to_link;
	var node='',o;
	if((o=data['obj_'+smid]) && o[1].description_videoarray){
		node=o[1].description_videoarray;
	}else{
		var search=to_link?'':'&nlFilter';
		if(!T.getAllID || !(T.url && T.url!='Search' && T.url!='Tag') || (T.getAllIDpressShift && P.key_now!==16)){
				node=HttpRequest('http://'+location.host+'/pr_Redirect_i.nicovideo.jp/v3/video.array?v='+smid+search, function(r,h,o){return o.description_videoarray}, false, smid, ['array',smid]);
		}else{
			var smids=function(d){
				var A=[],A0=[],id,
					m=T.maxGetID,
					e=d.getElementsByClassName('nl_Desc'),
					flg=false;
				for(var i=0,l=e.length; i<l; i++){
					id=e[i].getAttribute('data-smid');
					if(data['obj_'+id] && data['obj_'+id][1].description_videoarray)continue;
					if(flg){
						A.push(id);
						if(!--m)break;
					}else if(id==smid){
						A.push(id);
						--m;
						flg=true;
					}else{
						A0.push(id);
					}
				}
				if(m)A.push(A0.slice(-m));
				return A.join(',');
			}(document);
			node=HttpRequest('http://'+location.host+'/pr_Redirect_i.nicovideo.jp/v3/video.array?v='+smids+search,function(res,smid,objects){for(var i=objects.length;i--;)if(objects[i].id==smid)return objects[i].description_videoarray}, false, smid);
		}
	}
	return to_link ? P.desc_to_link(node,'popThumb_FA_link',smid) : node;
};
getFullDesc.cacheSetTags=function(){
	var T=this,d=document,P=window.popThumb,data=P.data,url=T.url;
	var origin,clone,Item;
	switch(url){
		case'Ranking':
			origin=d.getElementsByClassName('video')[0].getElementsByClassName('list')[0];
			clone=origin.cloneNode(true);
			Item=clone.getElementsByClassName('item');
			break;
		case'Mylist':
			origin=d.getElementById('SYS_page_items');
			clone=origin.cloneNode(true);
			Item=clone.getElementsByClassName('SYS_box_item');
			break;
		case'MyMylist':
			origin=d.getElementById('SYS_page_items');
			Item=origin.getElementsByClassName('SYS_box_item');
			break;
		case'History':
			origin=d.getElementById('historyList');
			Item=origin.getElementsByClassName('outer');
			break;
	}
	for(var i=Item.length; i--;){
		var I=Item[i],id,elem;
		switch(url){
			case'Ranking':
				id=I.getAttribute('data-id');
				elem=I.getElementsByClassName('itemDescription')[0];
				break;
			default:
				id=I.getElementsByClassName('nl_Desc')[0].getAttribute('data-smid');
				elem=I.getElementsByClassName('nl_Desc')[0].parentNode;
				break;
		}
		if(data['obj_'+id])T.setTagsItem(data['obj_'+id][1].tags,elem);
	}
	if(url=='Ranking'||url=='Mylist')origin.parentNode.replaceChild(clone,origin);
};
getFullDesc.setTagsItem=function(tag,elem){
	var P=window.popThumb;
	var A=[],len=i=tag.length;
	for(;i--;){
		var tag_obj=tag[i],
			res=[],
			tag_encode=tag_obj.text.replace(/(<a)( [^>]+>[^<]+<\/a>)/g,'$1 class="nlTag"$2'),
			tag_text=tag_encode.replace(/<[^<>]+>/g,'');
		res.push('<span class="nlTagType'+(tag_obj.lock?' nlTagLock':'')+(tag_obj.category?' nlTagCategory':'')+'"></span>');
		if(P.use_in_JS_to_link)tag_encode=P.desc_to_link(tag_encode, 'popThumb_FA_link', pop_smid);
		res.push(tag_encode.replace(/(^|<\/[^>]+>)([^<>]+)/, '$1<a href="http://www.nicovideo.jp/tag/'+tag_text+'" class="nlTag">$2</a>'));
		if(tag_obj.dic||(popThumb.dic&&popThumb.dic[tag_encode]))res.push('<a href="http://dic.nicovideo.jp/a/'+tag_encode+'" class="nlTagDic"></a>');
		A.unshift('<li class="nlTagItem">'+res.join('')+'</li>');
	}
	A.unshift('<li class="nlTagItem"><strong>TAG</strong>:[ jp:'+len+' ]</li>');
	elem.insertAdjacentHTML('BeforeBegin','<ul class="nlTagList">'+A.join('')+'</ul>');
};
getFullDesc.setOnclick=function(){
	var d=document,E=d.getElementsByClassName('nl_Desc');
	for(var i=E.length;i--;){
		E2=E[i];
		E2.setAttribute('onclick',E2.getAttribute('onclick').replace(')', ','+(i+1)+')'));;
	}
};
getFullDesc.init=function(){
	var T=this;
	if(!(window.popThumb&&popThumb.addEvent))return alert('[popThumb_FA.js] not found.');
	switch(T.url){
		case 'Ranking':
		case 'History':
			if(T.setTagsLoad)T.cacheSetTags();
			return;
		case 'Mylist':
		case 'MyMylist':
			var C=0;
			var timer=function(){
				var C2=document.querySelectorAll('#SYS_page_items>.SYS_box_item').length;
				if(!C2 || C!=C2){
					C=C2;
					setTimeout(timer,100);
				}else{
					if(T.setTagsLoad)T.cacheSetTags();
					T.setOnclick();
				}
			};
			setTimeout(timer,100);
			if(T.url=='Mylist')window.addEventListener('hashchange',function(e){C=0,setTimeout(timer,100)},false);
			return;
		default:
			T.setOnclick();
			return;
	}
};
/*----------------------------------------------*/
function getFullDesc(elem_caller, smid){
	var T=arguments.callee,
		  data=window.popThumb.data;
	var origin=elem_caller.parentNode.parentNode,
		clone=origin.cloneNode(true),
		nl_Desc=clone.getElementsByClassName('nl_Desc')[0],
		elem=nl_Desc.parentNode;
	elem.innerHTML=T.getThumbDesc(smid);
	elem.setAttribute('style','height:auto; line-height:1.25;');
	if(T.url=='Search' || T.url=='Tag')elem.removeAttribute('title');
	if(T.setTags && !clone.getElementsByClassName('nlTagList')[0]){
		if(data['obj_'+smid])T.setTagsItem(data['obj_'+smid][1].tags,elem);
	}
	origin.parentNode.replaceChild(clone,origin);
};
/*----------------------------------------------*/
getFullDesc.init();
window.getFullDesc = getFullDesc;
})();