$(function() {
	var timeline = [];
	var lang = "ja"
	,	query = "lessthan_staff"
	;
	
	var init_param = {				// 送信データ
			"screen_name": query		// 検索文字列
		,	"lang": lang			// 言語
		,	"include_rts": 1			// 言語
		,	"count": 5			// 言語
		//,	"locale": lang			// 
		//	"alt": "json-in-script"	// jsonp
	},	latest_param = {
			"screen_name": query		// 検索文字列
		,	"lang": lang			// 言語
		,	"include_rts": 1			// 言語
		,	"count": 5			// 言語
		,	"since_id": ""			// 
		//,	"locale": lang			// 
		//	"alt": "json-in-script"	// jsonp
	};
	// 初期表示
	getResult(init_param);
	pushTweet();

	function getResult(param) {
		$.getJSON(
		    "http://api.twitter.com/1/statuses/user_timeline.json?callback=?",	// リクエストURL
			param ,
			function(data, status) {	// 通信成功時にデータを表示
				if (data.length == 0) return;
				latest_param.since_id = data[0].id;
//				console.log(data[0].id);
				for( var i in data){
					timeline.push(data[i]);
				}
		    }
		);
	}


	function pushTweet(){
		if(timeline.length == 0) return;
		var tweet = timeline.pop();
//		console.log(tweet);
		var tweetuser = (!tweet.retweeted_status) ? tweet.user : tweet.retweeted_status.user;
		var replace_text = tweet.text
				.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&amp;amp;amp;\'()*+,;=%]+)/ig,"<a href='$1' target='blank'>$1</a>")
				.replace(/@+([_A-Za-z0-9-]+)/ig,"<a href='http://twitter.com/$1' target='blank'>@$1</a>")
				.replace(/#+([_A-Za-z0-9-]+)/ig,"<a href='http://search.twitter.com/search?q=$1' target='blank'>#$1</a>");
		var icon = '<div class="tw_icon">' + tweetuser.screen_name.replace(/(.+)/ig,"<a href='http://twitter.com/#!/$1' target='blank'>") + '<img src="' + tweetuser.profile_image_url + '" /></a></div>'
		,   user = '<div class="tw_user">' + tweetuser.screen_name.replace(/(.+)/ig,"<a href='http://twitter.com/#!/$1' target='blank'>$1</a>") + '</div>'
		,   msg = '<div class="tw_msg">' + replace_text + '</div>';
//+ twitterdate(tweet.created_at)
		var text = '<li><div class="tw_tweet">' + icon + user + msg + '</div><div class="clearfloat"></div></li>';
		if($('#tweetList li').size() == 0){
			$('#tweetList').prepend(text).find('li:eq(0)').animate({'opacity':1}, 3000);
//			$('#tweetList img').toggle();
			$('#tweetList').parent().css({'overflow-x':'hidden'});
		}else{
			$('#tweetList').prepend(text)
				.find('li:eq(0)').animate({'opacity':1}, 3000);
/*
			$('#tweetList').prepend('<li>' + timeline.pop().text + '</li>')
				.find('li:eq(1)')
					.animate({'marginTop': $('#tweetList li:eq(0)').height()}, 1000, function() {
					$(this)
						.animate({'marginTop': $('#tweetList li:eq(0)').height()*-1}, 1000)
						.siblings(':eq(0)').animate({'opacity':1}, 1000)
				;
				});
			;
*/
		}
	}
	function twitterdate(td){
		//変換前「Tue Dec 16 23:48:56 +0000 2008」
		//変換後「2008年12月17日 8:48:56」
		re=/^(.+) (.+) (..) (..):(..):(..) (.+) (.+)$/;
		pat="$2 $3, $8 $4:$5:$6 UTC+0000";
		rep=new Date(td.replace(re,pat));
		//Fri May 20 2011 12:20:26 GMT+0900 (Japan Standard Time)
		re2=/^(.+) (.+) (..) (.+) (..):(..):(..) (.+)$/;
		pat2=" $1 $5:$6:$7";
		yy = rep.getYear();
		mm = rep.getMonth() + 1;
		dd = rep.getDate();
		if (yy < 2000) { yy += 1900; }
		if (mm < 10) { mm = "0" + mm; }
		if (dd < 10) { dd = "0" + dd; }
		daystring = [yy, "/", mm, "/", dd, rep.toLocaleString().replace(re2,pat2)].join('');
		return(daystring);
	}
/*	window.setInterval(function() {
		if(latest_param.since_id === '') return;
		getResult(latest_param);
	},40*1000);
*/
	window.setInterval(function(){
		pushTweet();
	},1*1000);
});

