/* 文字コードはUTF-8 */
twitter_usertimeline = function(){
	this.init = function(tid, user_id){
		var limit = (typeof(arguments[2]) != 'undefined' ? arguments[2] : 10);
		var link_type = (typeof(arguments[3]) != 'undefined' ? arguments[3] : '_self');
		var dateobj = new Date();
		var now = dateobj.getTime();
		var tobj = $('#' + tid).eq(0);
		tobj.html('<div class="message">読込中...</div>');
		$.getJSON('http://twitter.com/statuses/user_timeline/' + user_id + '.json?count=' + limit + '&callback=?', function(json) {
			var html = '';
			tobj.css('display', 'none');
			if(json.length > 0){
				var count = json.length;
				html += '<ul>';
				$.each(json, function(i,item){
					var status = item.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
					return '<a href="'+url+'" target="'+link_type+'">'+url+'</a>';
					}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
					return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
					});
					var values = item.created_at.split(' ');
					var pubtime = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3]);
					dateobj.setTime(pubtime);
					var year = dateobj.getFullYear();
					var month = dateobj.getMonth() + 1;
					var day = dateobj.getDate();
					var date_str = year + '.' + (month < 10 ? '0' : '') + month + '.' + (day < 10 ? '0' : '') + day;
					var class_name = (i == 0 ? 'first' : ((count - i) == 1 ? 'last' : ''));
					if(now - pubtime < 86400000){
						class_name = (class_name != '' ? ' ' : '') + 'new';
					}
					html += '<li' + (class_name != '' ? ' class="' + class_name + '"' : '') + '>' + date_str + ' ' + status + '</li>';
				});
				html += '</ul>';
			}
			else{
				html += '<div class="message">現在、準備中です。</div>';
			}
			tobj.html(html);
			tobj.slideDown('fast');
		});
	}
}
