//googleAPI(vasrion1feedsAPI)ロード
google.load("feeds", "1");

//読み込み完了後の処理
google.setOnLoadCallback(initialize);
function initialize() {
	//feed1
	viewGoogleFeed(
		"http://www.bizign-blog.com/feed",
		'feed1',
		3,
		58
	);
	//feed2
	viewGoogleFeed(
		"http://www.bizign-blog.com/feed",
		'feed2',
		3,
		58
	);

	/* feedの追加の仕方
       上記の例に従って追加してください。
　　　　項目の内容は以下の内容です。

	viewGoogleFeed(              //一つの関数になっています。
		feedのURL,               //ダブルコーテーションで囲む
		feedの表示先のHTMLのID名,  //ダブルコーテーションで囲む
		表示するエントリー数,
		内容の表示文字数           //この行の最後にはカンマをつけない     
	);
	*/
}

//Google AJAX Feed APIを使ってRSSを表示する
/*
url : フィードのURL
id  : 表示先の要素のID属性
entryNum : 表示するエントリー数
discriptionNum : 内容の表示文字数
*/
function viewGoogleFeed(url, id, entryNum, discriptionNum ) {
	var feed = new google.feeds.Feed(url);
	feed.setNumEntries(entryNum); //エントリー数
	feed.load( function(result){
		viewFeed(result, id, discriptionNum)
	});
}

//googleFeedの結果を受け取ってidで指定したHTML要素内に表示する
function viewFeed(result, id, num) {
	//フィードの生成
	var feeds = createFeeds(result, num);
	$('#'+id).html(feeds);
}

//フィード用のHTML要素を生成する
function createFeeds(result, num){
	var html = '';
	//エントリーの数だけfeedを生成する
	var len = result.feed.entries.length;
	for (var i=0; i<len; i++) {
		html += createFeed(result.feed.entries[i], num);
	}
	return html;
}
//表示するFeedを1件生成する
function createFeed(entry, num){
	var html = '';
    //タイトル生成(設計2)
	var title = createTitle(entry);
    //説明生成(設計4)
	var description = createDescription(entry, num)

	html = title + '<br />' + description + '<br />';
	return html;
}
//タイトル生成(設計2)
function createTitle (entry) {
	var html  = '';
	var date  = formatDate(entry.publishedDate);
	var link  = entry.link;
	var title = entry.title
	html = '<a target="_blank" href="'+ link +'">'+ date +' - '+ title +'</a>'
	return html
}
//説明生成(設計4)
//エントリのDescriptionを文字数numで切り詰めます
function createDescription(entry, num) {
    //var html = entry.content;
    var html = entry.contentSnippet;
    //BRタグ削除
	//html = html.replace(/<br\s*\/?>/, '');
    //切り詰めて'...'を追加
    html = html.substr(0, num) + '...';
	return html
}

//日付のフォーマットの整形
//yyyy.mm.ddの日付の形式に変換します
function formatDate(date) {
    var dAte = new Date(date);
    var yearNum = dAte.getYear();
    if (yearNum < 2000)
		yearNum += 1900;
    date = yearNum + '.' + ( dAte.getMonth() + 1) + '.' + dAte.getDate();
	return date
}