﻿(function($) {
$(document).ready(function(){
	var url="http://twitter.com/statuses/user_timeline/250060518.json?count=5&callback={callback}";
	$.getJSONP(url, function(obj){
		var _tweets = "";
		for(var i in obj){
				var _txt = obj[i].text;
				var date_natural = obj[i].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);
				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();
				}
				_tweets += '<li>' + _txt +' - '+ tweet_posted + '</li>';
		}
		$("#marqueeArea").append(_tweets);
		startMarquee();
	});
	function startMarquee(){
		$("#marqueeArea").marquee({
			loop: -1
			,scrollSpeed:20
			,pauseSpeed:4000
			,beforeshow: function ($marquee, $li){
			  var $author = $li.find(".author");
			  if( $author.length ){
				 $("#marquee-author").html("<span style='display:none;'>" + $author.html() + "</span>").find("> span").fadeIn(850);
			  }
		   }
		   , show: function (){
		   }
		   , aftershow: function ($marquee, $li){
			  var $author = $li.find(".author");
			  if( $author.length ) $("#marquee-author").find("> span").fadeOut(250);
		   }
		});
	}
});
})(jQuery);
