google.load("feeds", "1");
google.setOnLoadCallback(main);

function main() {
	// ブログ取得
	blog();
	twitter();
}

/***************************************************************************************
 * ブログ取得
 ***************************************************************************************/
function blog() {
		
	var html = "";
		
	// ブログ取得
	var feed = new google.feeds.Feed("http://rssblog.ameba.jp/to-zemi/rss20.xml");
  feed.setNumEntries(6);
	feed.load(function(result) {
		if (!result.error) {
			html += "";
			var container = document.getElementById("blog_list");
			for (var i = 0; i < 6; i++) {
				
				if (i != 1) {
					var entry = result.feed.entries[i];
					var reg = new Regexies();
	
					var img = "";
					var rex = new RegExp("src[\s]*=[\s]*\"(.*?)\"", "i");
					var imgs = entry.content.match(rex);
					if (imgs != null) {
						if (imgs.length > 0) {
							img = imgs[0];
						}
					}
	
					if (i != 5) {
						html += "<div style='font-size:10px;margin-bottom:5px;border-bottom:1px dotted #333333;padding:3px;'>";
					} else {
						html += "<div style='font-size:10px;margin-bottom:5px;padding:3px;'>";
					}
					html += "<table width='195'>";
					html += "<tr><td width='195' align='left' style='background-color:#eeeeee;'><div style='padding:5px 0;'>" + "<a href='" + entry.link + "' target='_blank' style='font-size:12px;'>" + entry.title + "</a>";
					if (i == 0) {
						html += "&nbsp;<img src='http://t-cup.jp/imgs/icon/new.gif' />";
					}
					html += "</div></td></tr>";
					//html += "<tr><td width='150'><a href='" + entry.link + "' target='_blank'><img " + img + " width='140' /></a></td></tr>";
					html += "<tr><td width='195' align='left'><div style='padding:5px 0;'>" + "" + entry.contentSnippet + "</div></td></tr>";
					html += "</table>";
					html += "</div>";
					
				}
			}
			container.innerHTML = html;	
		}
	});
}

/***************************************************************************************
 * ブログ取得
 ***************************************************************************************/
function twitter() {
		
	var html = "";
		
	// ブログ取得
	var feed = new google.feeds.Feed("http://twitter.com/statuses/user_timeline/136171014.rss");
	feed.setNumEntries(6);
	feed.load(function(result) {
		if (!result.error) {
			html += "";
			var container = document.getElementById("twitter_list");
			for (var i = 0; i < 6; i++) {
				
				if (i != 1) {
					var entry = result.feed.entries[i];
					var reg = new Regexies();
		
					if (i != 5) {
						html += "<div style='font-size:10px;margin-bottom:5px;border-bottom:1px dotted #333333;padding:3px;'>";
					} else {
						html += "<div style='font-size:10px;margin-bottom:5px;padding:3px;'>";
					}
					html += "<table width='195'>";
					html += "<tr><td width='195' align='left'><div style='padding:5px 0;'>" + "<a href='" + entry.link + "' target='_blank' style='font-size:12px;'>" + entry.title + "</a>";
					if (i == 0) {
						html += "&nbsp;<img src='http://t-cup.jp/imgs/icon/new.gif' />";
					}
					html += "</div></td></tr>";
					//html += "<tr><td width='150'><a href='" + entry.link + "' target='_blank'><img " + img + " width='140' /></a></td></tr>";
					//html += "<tr><td width='195' align='left'><div style='padding:5px 0;'>" + "" + entry.contentSnippet + "</div></td></tr>";
					html += "</table>";
					html += "</div>";
					
				}
			}
			container.innerHTML = html;	
		}
	});
}

/***************************************************************************************
 * 正規表現クラス
 ***************************************************************************************/
var Regexies = function() {
	this.pattern = {
		'image' : /<div style="float:left">.*<\/a><\/div>/,
		'title' : /<span>(<a href=.*<\/a>)<\/span>/,
		'author' : /<span>([^<].+?)<\/span>/,
		'ranking' : /<\/strong>.*?<\/font>([^<]+)<br>/,
		'published' : /<em>([^<]+?\d+\/\d+\/\d+)<\/em>/,
		'star' : /<br> (<img src=".*)\(.*\)<br>/,
		'link' : /<a href="(http:\/\/.*?)">/,
		'price' : /<b>(￥.*?)<\/b>/,
		'remarks' : /<br>(\(.*?\))/
	}
	
	this.isnull = {
		'image' : "",
		'current' : "",
		'title' : "",
		'author' : "",
		'ranking' : "",
		'published' : "出版日: -",
		'star' : "評価: -",
		'link' : "",
		'price' : "",
		'remarks' : ""
	}
	
	this.prefix = {
		'image' : "",
		'current' : "",
		'title' : "",
		'author' : "",
		'ranking' : "",
		'published' : "",
		'star' : "評価:",
		'link' : "",
		'price' : "",
		'remarks' : ""		
	}

	//トリムがないので定義
	this.trim = function(text) {
	    return text.replace(/^[\s　]+|[\s　]+$/g, '');
	}
	
	this.match = function(target, text) {
		res = "";
		regex = this.pattern[target];
		cap = regex.exec(text);
		if (cap) {
			if (cap[1]) {
				res = this.trim(cap[1]);
			} else {
				res = cap;
			}
		}
		return res;
	}
}
