/**
 *	twitterプラグイン
 *	
 *	@company	sunnet
 *	@author		matsubara
 *	@since		2010/10/26
 *	@version	1.0
 */
$(function() {
	$.fn.extend({
		/**
		 *	指定したユーザーのタイムラインを表示
		 *	
		 *	@param
		 *		userId	twitterユーザーID
		 *		count	表示件数
		 */
		twitUserTimeLine : function(userId, count) {
			var self = this;
			var url = 'http://api.twitter.com/1/statuses/user_timeline.json?callback=?';
			// JSONデータ取得
			$.getJSON(url, { id : userId }, function(json) {
				var length = json.length;
				if (length > count) {
					length = count;
				}
				for (var i=0; i < length; i++) {
					// イメージを取得
					var img = json[i].user.profile_image_url;
					
					// 日時データを取得
					var created_at = json[i].created_at;
					// 日時データを要素分解
					created_at = created_at.split(' ');
					// 投稿日時変換 "Mon Dec 01 14:24:26 +0000 2008" -> "Dec 01, 2008 14:24:26"
					var post_date = created_at[1] + ' ' + created_at[2] + ', ' + created_at[5] + ' ' + created_at[3];
					// 日時データ処理
					var date = new Date(post_date);		// 日付文字列 -> オブジェクト変換
					date.setHours(date.getHours() + 9);	// UTC -> JST (+9時間)
					var mon  = date.getMonth() + 1;		// 月取得
					var day  = date.getDate();			// 日取得
					var hour = date.getHours();			// 時取得
					var min = date.getMinutes();		// 分取得
					
					// つぶやきを取得
					var twit = json[i].text;
					
					// urlを変換
					twit = twit.replace(/(http:\/\/[\x21-\x7e]+)/ig, '<a href="$1" target="_blank">$1</a>');
					
					// hashタグを変換
					twit = twit.replace(/^#(\w+)\s/ig, '<a href="http://twitter.com/#search?q=%23$1" target="_blank">#$1 </a>');	// 先頭にある場合
					twit = twit.replace(/\s#(\w+)/ig, '<a href="http://twitter.com/#search?q=%23$1" target="_blank"> #$1</a>');		// 中間や最後にある場合
					
					// @userを変換
					twit = twit.replace(/^@(\w+)\s/ig, '<a href="http://twitter.com/$1" target="_blank">@$1 </a>');	// 先頭にある場合
					twit = twit.replace(/\s@(\w+)/ig, '<a href="http://twitter.com/$1" target="_blank"> @$1</a>');		// 中間や最後にある場合
					
					// idを取得
					var id = json[i].id;
					
					$(self).append('<li><p>' + twit + '<span class="time">' + mon + '月' + day + '日　' + hour + '時' + min + '分</span></p></li>');
				}
			});
		}
	});
});

