
var SearchTweet = function (args){
	var args = args || {};
	this.div = args.div || 'twitter';
	this.url = args.url || 'http://search.twitter.com/search.json';
	this.words = args.words || ['%22saiten2011' , 'saiten2011' , encodeURI('土と平和の祭典')];
	this.option = args.option || {result_type : 'recent' , rpp : 40 , since_id : 0};
	this.staff = args.staff || [];
	this.interval = args.interval || 10000;
	$('#' + this.div).append('<ul id="tweets" style="padding:10px 20px;margin:0;"></ul><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.tanemaki.jp%2F2011%2F&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=20" scrolling="no" frameborder="0" style="position:absolute;right : 0;top:15px;border:none; overflow:hidden; width:110px; height:20px;" allowTransparency="true" id="facebook"></iframe>');
	var left = $('table:first').position().left + 20;
	$('#' + this.div).css({ position : 'absolute' ,left : left, 'background-color' : '#fff', height: '50px' , width : '1020px' , top : '20px'});
	var that = this;
	$(window).resize(function() {
  		$('#' + that.div).css({left : $('table:first').position().left + 20});
	});
	$('table:first').css({'padding-top':'60px'});
}

SearchTweet.prototype.setCss = function(){
	$("#tweets li").css({'margin-right' : '140px', position : 'absolute' , 'font-size' : '10px' , 'list-style-type' : 'none' , lineHeight : '1.4'});
	$("#tweets li a").css({color : '#78BB44'});
	$("#tweets li img").css({float : 'left' , marginRight : '5px'});
}

SearchTweet.prototype.timestamp = function (created_at) {
	var created_at = Date.parse(created_at);
	var now = (new Date()).getTime();
	
	var passage = (now - created_at) / 1000; //second
	
	if(passage < 60){
		passage = Math.ceil(passage);
		return passage + '秒前';
	}
	if(passage < 3600){
		passage = Math.ceil(passage / 60);
		return passage + '分前';
	}
	if(passage < 3600 * 24){
		passage = Math.ceil(passage / 3600);
		return passage + '時間前';
	}
	
	passage = Math.ceil(passage / ( 3600 * 24));
	return passage + '日前';

}

SearchTweet.prototype.getTweets = function(){
	var that = this;
	$.getJSON(that.url + '?q=' + that.words.join('+OR+') + '&callback=?', that.option , function(data) {
		if(data.results.length){
			that.option.since_id = data.max_id;
			$.each(data.results.reverse(),function(index,tweet){
				if(tweet.text.search(/^(RT|QT) /i) == -1 && index < 20){
					$('#tweets').prepend('<li style="display:none;"><a href="http://twitter.com/' + tweet.from_user + '"><img src="' + tweet.profile_image_url + '" alt="" height="30" width="30" border="0" /></a><a href="http://twitter.com/' + tweet.from_user + '">' + tweet.from_user + '</a> ' + that.addLink(tweet.text) + ' <a href="http://twitter.com/' + tweet.from_user + '/status/' + tweet.id_str + '">' + that.timestamp(tweet.created_at) + '</a> <a href="http://twitter.com/intent/tweet?in_reply_to=' + tweet.id_str + '">返信</a> <a href="http://twitter.com/intent/retweet?tweet_id=' + tweet.id_str + '">リツイート</a> <a href="http://twitter.com/intent/favorite?tweet_id=' + tweet.id_str + '">お気に入り</a></li>');
					$('#tweets li:first').data({id : tweet.id_str , user : tweet.from_user});
				}
			});
			that.setCss();
		}
		//ツイートが一つもなかった時のことを書く
		if($('#tweets li').length == 0){
			$('#tweets').prepend('<li>最近はつぶやかれていないようです</li>');
		}
		else{
			that.internalFade(0);
		}		
	});
}

SearchTweet.prototype.addLink = function(text){
	return text.replace(/(http:\/\/[^ 　]+)/ig, '<a href="$1" title="$1">$1</a>');

}

SearchTweet.prototype.internalFade = function(index){
	var that = this;
	var fade_time = that.interval * 0.1;
	var display_time = that.interval * 0.9;

	index = index || 0;
	$('#tweets li').eq(index).slideDown(fade_time,function(){
		var link = 'http://twitter.com/' + $(this).data('user') + '/status/' + $(this).data('id');
		var facebook = $('#facebook').attr('src').replace(/href=.+send=/,'href=' + encodeURIComponent(link) + '&send=');
		$('#facebook').attr({src : facebook});
	//	var mixi = $('#mixi').attr('src').replace(/href=.+service_key=/,'href=' + encodeURIComponent(link) + '&service_key=');
	//	$('#mixi').attr({src : mixi});


		if($('#tweets li').eq(index + 1).length > 0){//アイテムがあったら
			$('#tweets li').eq(index).delay(display_time).slideUp(fade_time,function(){
				that.internalFade(index + 1);
			});

		}
		else{//無かったらもどる
			$('#tweets li').eq(index).delay(display_time).slideUp(fade_time,function(){
				that.internalFade(0);
			
			});
		}


	});
	
}


