/***** rss.js ver 0.4 *****/
/**
 * XmlReader Constructor
 * @param name オブジェクトの名前。patternによっては使う場合がある。
 * @param Feed pattenオブジェクト
 * @param elem 表示先のターゲット指定に必要
 */
var XmlReader=function(name,Feed,rss,elem){
	this.name=name;
	this.Feed=Feed;
	this.rss=rss;
	this.target=elem;
};
XmlReader.prototype={
	target:null,
	complete:false,
	name:null,
	rss:null,
	data:null,
//初期動作。onloadとかで呼ばれる想定。
	init:function()
	{
		if(!(this.target)){throw new Error("Don't have Target Element.");}
		if(!(this.rss)){throw new Error("Don't have Feed.");}
		if(!(this.Feed)){throw new Error("Don't have Feed's pattern Object.");}
		if(!(this.Feed.proxy)){throw new Error("Don't have proxy.");}
		var script = document.createElement('script');
		this[this.Feed.dataPoint]=null;
		script.charset = 'UTF-8';
		script.src = this.Feed.url(this.name,this.rss);
		document.getElementsByTagName('head')[0].appendChild(script);
		this.target.innerHTML="Now loading...";
		return true;
	},
	onload:function(data)
	{
		data= 		!(data)?this[this.Feed.dataPoint]:data;
		this[this.Feed.dataPoint]=!(this[this.Feed.dataPoint])?data:this[this.Feed.dataPoint];
		try{
			this.createList(data);
		}catch(e){
			this.target.innerHTML=e.toString();
		}
		return true;
	},
	type:null,
//	正規表現格納用
	pattern:{
		obj:null,
		check:function(str){
		if(this.obj){return !(this.obj.test(str));}
		return 	true;
		}
	},
//	追加する事が可能。
	Feed:null,
//	リストのタイトルを入れる為のを作る為の奴。実装予定
	createLTitle:function(){
	},
	template:{
			list:"<li><a href='%(url)'>%(title)</li>",
			bace:"<ul>%(contents)</ul>"
		},
//	リストの作成関数
	createList:function(data)
	{
		if(!(data)){throw new Error("Don't have Feed Data");}
		if(!(document.createElement)){throw new Error("This browser isn't appendChild");}
		var title=document.createElement;
		var i=null;
		var rss=null;
		for(i=0;i<this.Feed.pattern.length;i++){
			rss=this.Feed.pattern[i];
			if(rss && rss.check(data))
			{	
				this.type=rss.type;
				this.target.innerHTML=rss.comp(data,this.pattern,this.template);
				return true;
			}
		}
		throw new Error("This Data is Not applicable.");
	},
	toString:function(){
		return 'これはrssを読み込み表示させる為のスクリプトです。';
	}
};
function RND(tmpl, ns, scope) {
  scope = scope || window;
  var fn = function(w, g) {
    g = g.split("|");
    var cnt = ns[g[0]];
    for(var i=1; i < g.length; i++)
      cnt = scope[g[i]](cnt);
    if(cnt == 0 || cnt == -1)
      cnt += '';
    return cnt || w;
  };
  return tmpl.replace(/%\(([A-Za-z0-9_|.]+)\)/g, fn);
}
var feed_reader ={
	url:null,
	proxy:null,
	dataPoint:"data",
	pattern:null,
	toString:function(){
		return "nodata";
	}
}

var feed_nomadscafe_jp_json={
	url:function(name,rss){
		return this.proxy + '&url=' + escape(rss)+"?cb="+name+"."+this.targetataPoint;
	},
	proxy:'http://feed.nomadscafe.jp/json/',
	dataPoint:"data",
	pattern:[
		{
//			タイプの宣言
			type:"RSS 2.0",
			template:{bace:"",list:""},
//			判断材料
			check:function(data){
				return (data.version=="2.0");
			},
//			処理系
			comp:function(data,cp,tmp)
			{	
				var _item=data.channel.item,str="";
				for(i in _item)
				{
					var item=_item[i];
					if(cp(item.title))
					{
						str+=RND(tmp.list,{
											"title":item.title,
											"url":item.link,
											"text":item.description
											});
					}
				}
				str=RND(tmp.bace,{"title":null,"contents":str});
				return str;
			}
		},
		{
			type:"Atom",
			check:function(data){
				return ((data.xmlns && 0<=data.xmlns.indexOf("http://www.w3.org/2005/Atom")) || data.entry);
			},
			comp:function(data,cp,tmp)
			{
				var _item=data.entry,str="";
				for(i in _item)
				{
					var item=_item[i];
					if(cp(item.title))
					{
						str+=RND(tmp.list,
							{"title":(item.title?item.title.content:null),
							"contents":(item.link?item.link.href:null),
							"text":(item.summary?item.summary.content:null)});
					}
				}
				str=RND(tmp.bace,{"title":null,"contents":str});
				return str;
			}
		}
	],
	toString:function(){
		return 'Powerd by feed.nomadscafe.jp';
	}
};
var app_drk7_jp_xml2json={
	url:function(name,rss){
		return this.proxy + 'var=' + name + '&url=' + escape(rss);
	},
	proxy:'http://app.drk7.jp/xml2json/',
	dataPoint:"data",
	pattern:[
		{
//			タイプの宣言
			type:"RSS 2.0",
//			判断材料
			check:function(data){
				return (data.version=="2.0");
			},
//			処理系
			comp:function(data,cp,tmp)
			{	
				var _item=data.channel.item,str="";
				for(i in _item)
				{
					var item=_item[i];
					if(cp.check(item.title))
					{
						str+=RND(tmp.list,{"title":item.title,"url":item.link,"text":item.description});
					}
				}
				str=RND(tmp.bace,{"title":title,"contents":str});
				return str;
			}
		},
		{
			type:"Atom",
			template:{bace:"",list:""},
			check:function(data){
				return ((data.xmlns && 0<=data.xmlns.indexOf("http://www.w3.org/2005/Atom")) || data.entry);
			},
			comp:function(data,cp,tmp)
			{
				var _item=data.entry,str="";
				for(i in _item)
				{
					var item=_item[i];
					if(cp.check(item.title.content))
					{
						str+=RND(tmp.list,
							{"title":(item.title?item.title.content:null),
							"url":(item.link?item.link.href:null),
							"text":(item.summary?item.summary.content:null)});
					}
				}
				str=RND(tmp.bace,{"title":null,"contents":str});
				return str;
			}
		}
	],
	toString:function(){
		return 'Powerd by drk7.jp';
	}
};

var pipes_yahoo_com_yager_rss2json={
	url:function(name,rss){
		return this.proxy +"?_render=json&_callback="+name+".onload"+'&url=' + escape(rss);
	},
	proxy:'http://pipes.yahoo.com/yager/rss2json',
	dataPoint:"data",
	pattern:[
		{
			type:"Yahoo Feed to json",
			check:function(data){
				return (data.value.generator=="http://pipes.yahoo.com/yager/rss2json/");
			},
			comp:function(data,cp,tmp)
			{
				var _item=data.value.items,str="";
				for(i in _item)
				{
					var item=_item[i];
					
					if(cp.check(item.title))
					{
						str+=RND(tmp.list,
							{"title":(item.title?item.title:null),
							"url":(item.link?item.link:null),
							"text":(item.summary?item.summary.content:null)});
					}
				}
				str=RND(tmp.bace,{"title":null,"contents":str});
				return str;
			}
		}
	],
	toString:function(){
		return 'Powerd by yager/rss2json';
	}
};
