function nicofetch(elem,id,parent,link_id){
	if(elem&&elem.__fetching)return;
	var frm=elem,fetcher;
	for(;parent--;)frm=frm.parentNode;
	if(!~id.indexOf('&')){
		fetcher=new nlMovieFetcher(id,elem,frm,(link_id || "watch/"+id));
	}else{
		var params=id.split('&');
		id=params[0];
		fetcher=new nlMovieFetcher(id,elem,frm,(link_id || "watch/"+id));
		fetcher.params=params;
	}
	fetcher.fetch();
}

(function(){
// nlMovieFetcherの登録
if(!window.nlMovieFetcher){
	// 外部サムネやユーザースクリプトを考慮してPrototype.jsは使わずに実装
	window.nlMovieFetcher=function(){this.initialize.apply(this,arguments)}
	window.nlMovieFetcher.prototype={
		// 最低限、動画IDさえ渡せば動作OK
		initialize:function(id,elem,frame,anchor){
			this.id      =id;						// 動画ID(必須)
			this.elem    =this.__getElem(elem);		// ステータスを表示するElement
			this.frame   =this.__getElem(frame);	// 背景色を変更するElement
			this.anchor  =this.__getElem(anchor);	// 動画を開くアンカーElement
			this.params  =[id];						// APIに渡すパラメータ配列
			this.bgColor1='#FFFF99';				// 背景色(1段階目)
			this.bgColor2='#FFFF00';				// 背景色(2段階目)
			this.interval=2000;						// ステータス取得間隔(ミリ秒)
		},
		fetch:function(){
			var fetch_text="fetching...",
				self=this;
			if(this.elem){
				if(this.elem.__fetching)return;
				this.elem.__fetching=true;
				this.__setText(fetch_text);
			}
			if(this.frame){
				this.bgImage0=this.frame.style.backgroundImage;
				this.bgColor0=this.frame.style.backgroundColor;
				this.frame.style.backgroundImage='none';
				this.frame.style.backgroundColor=this.bgColor1;
			}
			this.callAPI(this.params.join('&'),function(r){
				var text=r.responseText;
				if(r.status==200 && !text.indexOf("OK")){
					if(~text.indexOf("accepted")){
						self.__updateStatus();
					}else{
						self.__setText(text.substring(3));
						self.__finish(~text.indexOf("cached"));
					}
				}else{
					self.__setText(fetch_text+" FAILED!");
					self.__finish(false);
				}
				self.__modifyAnchor();
			});
		},
		callAPI:function(param,callback){
			var FETCH_API_URL="http://"+document.location.host+"/cache/fetch?",
				xhr=new XMLHttpRequest();
			xhr.open('GET',FETCH_API_URL+param,true);
			xhr.onreadystatechange=function(){
				if(xhr.readyState==4 && callback)callback(xhr);
			}
			xhr.send(null);
		},
		__getElem:function(name){return(typeof name=='string') ?document.getElementById(name) :name},
		__setText:function(text){
			if(!this.elem)return;
			this.elem.textContent=text;
		},
		__updateStatus:function(){
			if(!this.elem)return;
			var self=this,
				timer=setInterval(function(){
				self.callAPI("status="+self.id,function(r){
					if(r.status!=200)return;
					var text=r.responseText;
					self.__setText(text);
					if(!/( wait|\d%)$/.test(text)){
						clearInterval(timer);
						self.__finish(/ done\.$/.test(text));
					}
				});
			},this.interval);
		},
		__modifyAnchor:function(){
			if(!this.anchor || this.anchor.__fetched)return;
			var self=this,
				rmfunc=function(){
				self.callAPI("rm="+self.id);
				if(self.frame && self.frame!=self.elem){
					self.frame.style.backgroundImage=self.bgImage0;
					self.frame.style.backgroundColor=self.bgColor0;
				}
			}
			this.anchor.addEventListener('click',rmfunc,true);
			this.anchor.target='_blank';
			this.anchor.__fetched=true;
		},
		__finish:function(cached){
			if(!this.elem)return;
			if(cached){
				if(this.frame && this.frame!=this.elem){
					this.frame.style.backgroundColor=this.bgColor2;
				}
				this.elem.removeAttribute('href');
				this.elem.removeAttribute('onclick');
			}
			this.elem.__fetching=false;
		}
	}
}

})();
