/**
 * RSSクラス
 * RSSデータの取得、描画を行う
 */
function RSS() {
}

/**
 * htmlタグのテンプレート文字列を返す
 * 置換したい値は{$variable}とする事
 */
RSS.prototype.getTpl = function() {
	var htmlcode = '';
	htmlcode += '<div class="rss"><a href="{$link}">{$title} {$date}</a></div><br />';
	return htmlcode;
}

/**
 * htmlタグのテンプレートをjsonデータで置換した文字列を返す
 */
RSS.prototype.getReplace = function(data, code) {
	var htmlcode = "";
	if(data){
		for(var i=0;i<data.length;i++){
			if(typeof data[i] != "undefined"){
				var tmpcode = code;
				for(var j in data[i]){
					var pattern = "{$"+j+"}";
					tmpcode = tmpcode.split(pattern).join(data[i][j]);
				}
				htmlcode += tmpcode;
			}
		}
	}else{
		htmlcode += code;
	}
	return htmlcode;
}

/**
 * RSSデータをロードする
 *
 * url		String			RSSデータURL
 * target	JQueryObject	RSSデータ描画先
 * cnt		Number			RSSデータ表示数
 */
RSS.prototype.load = function(url, target, cnt) {
	var rss = this;
	$.ajax({
		type     : "POST",
		url      : "rss.php",
		dataType : "json",
		data     : "rssURL=" + url,
		success  : function(json) {
			var html = "";
			var tpl  = rss.getTpl();
			if(json.length > 0) {
				cnt  = json.length < cnt ? json.length : cnt;
				json = json.slice(0, cnt);
				html = rss.getReplace(json, tpl);
				target.html(html);
			}
		}
	});
}
