//	参考サイト
//	「Twitter API Viewer」
//		http://twitool-box.net/api-viewer/
//
//	「Fetching tweets with jQuery and the Twitter JSON API」
//		http://www.lupomontero.com/fetching-tweets-with-jquery-and-the-twitter-json-api/
//
//	「Adding Twitter to your site with jQuery Part 2」
//		http://think2loud.com/adding-twitter-site-jquery-part-2/
//
//	「jQueryでAJAX入門：正規表現でサイトに表示したTwitterにリンクをつける」
//		http://gihyo.jp/design/serial/01/jquery-site-production/0020
//
//	「jQuery.getJSON()」
//		http://api.jquery.com/jQuery.getJSON/
//
//	「Display JSON Data with jQuey and Ajax」
//		http://www.9lessons.info/2009/10/json-jquery-ajax-php.html

(function($){
	$.fn.tweetsQuery = function(options){

		var o = $.extend({
			//	表示タイプ、ユーザー名、リスト名、検索単語・ハッシュタグ、表示数は「basic.js」で定義します。
			type: null,
			user_name: null,
			list_name: null,
			search_term: null,
			post_number: null
		},options || {});
		
		$(this).each(function(){

			if(!o.type) {
				return false;
			}

			if (o.type == "user") {
				var twitter_api_url = "http://api.twitter.com/1/statuses/user_timeline.json?callback=?&screen_name=" + o.user_name + "&count=" + o.post_number;
			}

			$.ajaxSetup({
				cache: true
			});

			$.getJSON(twitter_api_url, function(data) {

				$.each(data.results || data, function(i, tweet) {

					
					if(tweet.text !== undefined) {

						var date_natural = tweet.created_at.split(" ");
						var date_array = date_natural[1]+" "+date_natural[2]+", "+date_natural[5]+" "+date_natural[3];
						var date_tweet = new Date(date_array); //"December 31, 1999 23:59:59"   Wed Oct 27 14:03:58 +0000 2010

						var date_now = new Date();
						var date_diff = date_now - date_tweet;
						var seconds = Math.round(date_diff/(1000));
						var minutes = Math.round(date_diff/(1000*60));
						var hours = Math.round(date_diff/(1000*60*60)-9);
						var days = Math.round(date_diff/(1000*60*60*24));

						if(seconds < 59) {
							var tweet_posted = seconds + "秒前";
						} else if(minutes < 59) {
							var tweet_posted = minutes + "分前";
						} else if(hours < 24) {
							var tweet_posted = "約" + hours + "時間前";
						} else if (days > 0 || days < 1) {
							var tweet_posted = days + "日前";
						} else if(days > 2) {
							var tweet_posted = date_tweet.toLocaleDateString();
						}

						var tweet_formatted = tweet.text;
						tweet_formatted = tweet_formatted.replace(/(http:\/\/[\x21-\x7e]+)/gi,"<a href='$1'>$1</a>").replace(/#(\w+)/g,"<a href='http://twitter.com/#search?q=%23$1'>#$1</a>").replace(/@(\w+)/g,"<a href='http://twitter.com/$1'>@$1</a>");

						var tweet_contents = "\n <article class='tweets-wrapper'>";
						
						if(o.type == "user" || o.type == "list") {
							tweet_contents += "<div class='tweet-contents_d'><span class='tweet-contents'><a href='http://twitter.com/" + tweet.user.screen_name + "' title='" + tweet.user.screen_name + " の Twitter ページへ' target='_blank'><img src=" + tweet.user.profile_image_url + " /></a>";
							tweet_contents += "<span class='user-name'><a href='http://twitter.com/" + tweet.user.screen_name + "' title='" + tweet.user.screen_name + " の Twitter ページへ' target='_blank'>" + tweet.user.screen_name + "</a></span>";
						}

						tweet_contents += tweet_formatted;

						if(o.type == "user" || o.type == "list") {
							tweet_contents += " <span class='tweet-meta'><a href='http://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str + "' target='_blank' title='この呟きを Twitter で表示する'>" + tweet_posted + "</a> ";
							tweet_contents += tweet.source + "から</span></span></div></article>\n";
						}

						if(o.type == "user") {
							$("#tweets-user1").children(".loading").hide().parent().append(tweet_contents);
						}
					}
				});
			});
		});
	}
})(jQuery);
