document.write ("<script type='text/javascript' src='/common/script/jquery-dateformat.js'></script>");

function substr(text, len, truncation) {
var count = 0;
var str = '';
for (i=0; i<text.length;i++) {
n = escape(text.charAt(i));
if (n.length < 4) count++; else count+=2;
if (count>len) {return str+truncation;}
str += text.charAt(i);
}
return text;
}

formatRelativeDate = function (date) {
	var second = (new Date ().getTime () - date.getTime ()) / 1000;
	if (second < 60) {
		return Math.max (parseInt (second), 1) + "秒前";
	} else if (second < 60 * 60) {
		return parseInt (second / 60) + "分前";
	} else if (second < 24 * 60 * 60) {
		return "約" + parseInt (second / 3600) + "時間前";
	} else {
		return date.format ("mm/dd HH:MM");
	}
};

/* [blog] ------------------------------------------------------------------- */
google.load ("search", "1");
google.setOnLoadCallback (function () {
	var query = getSearchQuery () || $.cookie ("query");
	if (query) {
		query = unescape (query);
		var control = new google.search.SearchControl ();
		var search = new google.search.WebSearch ();
		control.addSearcher (search);
		// search.setResultSetSize (google.search.Search.LARGE_RESULTSET);
		search.setSiteRestriction ("yusukekamiyamane.com/blog/");
		search.setSearchCompleteCallback (null, function () {
			var results = $.grep (search.results, function (result) {
				result.titleNoFormatting = result.titleNoFormatting.split (" | ")[0];
				return result.titleNoFormatting != "Blog";
			});
			if (results.length) {
				var list = $ ("<ul class='link' />").insertAfter ("div#blog h2").wrap ("<div class='box'><div /></div>").before ("<p>検索キーワード「<em>" + query + "</em>」に関連するブログ記事</p>");
				$.each (results, function (i, result) {
					$ ("<li><a href='" + result.url + "'>" + result.titleNoFormatting + "</a></li>").appendTo (list);
				});
			}
		});
		search.execute (query);
		$.cookie ("query", escape (query), { path: "/" });
	}
});
$ (function () {
	$.ajax ({
		url: "/blog/rss.xml",
		dataType: "xml",
		success: function (result) {
			var table = $ ("<table />").appendTo ("div#blog").wrap ("<div class='table' />");
			$ ("item", result).each (function (i) {
				if (i > 9) {
					return;
				}
				var row = $ ("<tr />").appendTo (table);
				$ ("<th>" + formatRelativeDate (new Date ($ ("pubDate", this).text ())) + "</th>").appendTo (row);
				$ ("<td><ul class='link'><li><a href='" + $ ("link", this).text () + "'>" + $ ("title", this).text () + "</a></li></ul></td>").appendTo (row);
			});
			if ($.browser.msie) {
				$ ("div#blog div.table table tr:last-child").addClass ("last-child");
			}
		}
	});
});

/* [twitter] ---------------------------------------------------------------- */
$ (function () {
	$ ("<script type='text/javascript' src='http://twitter.com/statuses/user_timeline/16510545.json?callback=twitterCallback&amp;count=10'></script>").appendTo ("body");
	twitterCallback = function (result) {
		if (result.length) {
			var table = $ ("<table />").appendTo ("div#twitter").wrap ("<div class='table' />");
			$.each (result, function (i, entry) {
				if (entry.text) {
					var row = $ ("<tr />").appendTo (table);
					$ ("<th>" + formatTwitterDate (entry.created_at) + "</th>").appendTo (row);
					var text = entry.text;
					$ ("<td><ul class='link'><li><a href='http://twitter.com/ykamiyamane/statuses/" + entry.id + "' title='" + text + "'>" + substr (text, 42, "...") + "</a></li></ul></td>").appendTo (row);
				}
			});
			if ($.browser.msie) {
				$ ("div#twitter div.table table tr:last-child").addClass ("last-child");
			}
			$ ("div#twitter div.table table tr td ul.link li a").click (function () {
				urchinTracker ("/external/" +  $ (this).attr ("href"));
			});
		}
	};
	formatTwitterDate = function (string) {
		var date = new Date ();
		var array = string.split (" ");
		date.setTime (Date.parse (array [1] + " " + array [2] + ", " + array [5] + " " + array [3]) - date.getTimezoneOffset () * 60 * 1000);
		return formatRelativeDate (date);
	};
});

/* [lastfm] ----------------------------------------------------------------- */
$ (function () {
	$ ("<script type='text/javascript' src='http://pipes.yahoo.com/pipes/pipe.run?_id=oD3BXyWy3BG_YIxjiXrL0A&amp;_render=json&amp;feed=http://ws.audioscrobbler.com/2.0/user/y4e_kamiyamane/recenttracks.rss&amp;_callback=lastfmCallback'></script>").appendTo ("body");
	lastfmCallback = function (result) {
		var table = $ ("<table />").appendTo ("div#lastfm").wrap ("<div class='table' />");
		$.each (result.value.items, function (i, track) {
			var row = $ ("<tr />").appendTo (table);
			$ ("<th>" + formatRelativeDate (new Date (track.pubDate)) + "</th>").appendTo (row);
			var title = track.title;
			$ ("<td><ul class='link'><li><a href='" + track.link.replace ("last.fm", "lastfm.jp") + "' title='" + title + "'>" + substr (title, 41, "...") + "</a></li></ul></td>").appendTo (row);
		});
		if ($.browser.msie) {
			$ ("div#lastfm div.table table tr:last-child").addClass ("last-child");
		}
		$ ("div#lastfm div.table table tr td ul.link li a").click (function () {
			urchinTracker ("/external/" +  $ (this).attr ("href"));
		});
	};
});