// ==UserScript==
// @name           twitter-throttle
// @namespace      twitter-throttle
// @description    throttle tweets
// @include        http://twitter.com/home
// ==/UserScript==

/* Twitter-Throttle by Jon Fox (http://jonefox.com/blog - http://twitter.com/jfox85) */

var TWITTER_NAME = "YOUR_TWITTER_NAME";
var TWITTER_PASS = "YOUR_TWITTER_PASS";

function GM_wait() {  
    if(typeof unsafeWindow.jQuery == 'undefined')
		window.setTimeout(GM_wait,100);
    else {
		$ = unsafeWindow.jQuery;
		throttleTweets();
	}
};

GM_wait(); 	 

function throttleTweets() {
	var username_list = [];
	var last_username = '';
	var need_to_get = 0;
	var last_id = '';
	var d = new Date();
	var now = d.getTime();
	var in_a_row = 0;
	var last_url = '';
	
	$('li.status a.url').each( function () {	
		var username = $(this).attr('href').substr( 19 );
		if( !username_list[ username ] ) {
			username_list[ username ] = 1;
			in_a_row = 0;
			last_url = "";
		}
		else {
			if( username == last_username ) {				
				in_a_row++;
				last_url += "gs_showTweet(\"" + $(this).parent().parent().attr('id') + "\");";
				if( in_a_row > 1 )
					$(this).parent().parent().before( "<li style='text-align:center;' id='hidLink_" + $(this).parent().parent().attr('id') + "'><a style='color: #999999;' href='javascript: " + last_url + "'> " + in_a_row + " hidden tweets by " + username + "</a></li>" );
				else
					$(this).parent().parent().before( "<li style='text-align:center;' id='hidLink_" + $(this).parent().parent().attr('id') + "'><a style='color: #999999;' href='javascript: gs_showTweet(\"" + $(this).parent().parent().attr('id') + "\" );'> hidden tweet by " + username + "</a></li>" );
				$( "#hidLink_" + last_id ).hide();
			}
			else {
				in_a_row = 1;
				$(this).parent().parent().before( "<li style='text-align:center;' id='hidLink_" + $(this).parent().parent().attr('id') + "'><a style='color: #999999;' href='javascript: gs_showTweet(\"" + $(this).parent().parent().attr('id') + "\" );'> hidden tweet by " + username + "</a></li>" );
				last_url = "gs_showTweet(\"" + $(this).parent().parent().attr('id') + "\");";
			}
			need_to_get++;			
			$(this).parent().parent().hide();
		}
		last_id = $(this).parent().parent().attr('id');
		last_username = username;
	} );
	
	$.getJSON("http://" + TWITTER_NAME + ":" + TWITTER_PASS + "@twitter.com/statuses/friends_timeline/7441552.json?page=2",
        function(data){			
			$.each(data, function(i,item) {
				if( !username_list[ item.user.screen_name ] && need_to_get > 0 ) {
					username_list[ item.user.screen_name ] = 1;
					$( "#" + last_id ).after('<li class="hentry status u-' + item.user.screen_name + '" id="status_' + item.id + '"><span class="thumb vcard author"><a href="http://twitter.com/' + item.user.screen_name + '" class="url"><img alt="' + item.user.name + '" class="photo fn" height="48" src="' + item.user.profile_image_url + '" width="48" /></a></span><span class="status-body"><strong><a href="http://twitter.com/' + item.user.screen_name + '" class="screen-name" title="' + item.user.name + '">' + item.user.screen_name + '</a></strong><span class="entry-content">' + item.text + '</span><span class="meta entry-meta"><a href="http://twitter.com/' + item.user.screen_name + '/status/' + item.id + '" class="entry-date" rel="bookmark"><span class="published">' + timeDiffToStr( now - Date.parse(item.created_at) ) + ' ago</span></a> <span>from ' + item.source + '</span> </span></span><span class="actions"><div><a class="fav-action non-fav" id="status_star_' + item.id + '" title="favorite this update">&nbsp;&nbsp;</a><a class="reply" href="/home?status=@' + item.user.screen_name + '%20&amp;in_reply_to_status_id=' + item.id + '&amp;in_reply_to=' + item.user.screen_name + '" title="reply to ' + item.user.screen_name + '">&nbsp;&nbsp;</a></div></span></li>');
					need_to_get--;
				}
			});
		}
    );
};

function timeDiffToStr(time) {
	var weeksConv   = 60 * 60 * 24 * 7 * 1000;
	var daysConv    = 60 * 60 * 24 * 1000;
	var hoursConv   = 60 * 60 * 1000;
	var minutesConv = 60 * 1000;
	var secondsConv = 1000;
	
	var weeks   = Math.floor( time / weeksConv );
	var days    = Math.floor( time / daysConv );
	var hours   = Math.floor( time / hoursConv );
	var minutes = Math.floor( time / minutesConv );
	var seconds = Math.floor( time / secondsConv );
	
	if( weeks > 1 )
		return "about " + weeks.toString() + " weeks";
	else if( weeks == 1 )
		return "about " + weeks.toString() + " week";
	else if( days > 1 )
		return "about " + days.toString() + " days";
	else if( days == 1 )
		return "about " + days.toString() + " day";
	else if( hours > 1 )
		return "about " + hours.toString() + " hours";
	else if( hours == 1 )
		return "about " + hours.toString() + " hour";
	else if( minutes >  1 )
		return minutes.toString() + " minutes";
	else if( minutes == 1 )
		return minutes.toString() + " minute";
	else
		return "less than 1 minute";
};

unsafeWindow.gs_showTweet = function (id) {
	$( "#" + id ).show();
	$( "#hidLink_" + id ).hide();
};
