/*
|----------------------------------------------------------------------------------------------------------------------------------
| Greasemonky chaff script
|----------------------------------------------------------------------------------------------------------------------------------
|
|	gets rid of chaff from twitter
|
|  2008-12-21
|  Author Nick Taylor 2008 - nick@tangerineworks.com
|
|  Released under the GPL license
|  http://www.gnu.org/copyleft/gpl.html
|
|  --------------------------------------------------------------------
|
|  This is a Greasemonkey user script.
|
|  To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
|  Then restart Firefox and revisit this script.
|  Under Tools, there will be a new menu item to "Install User Script".
|  Accept the default configuration and install.
|
|  To uninstall, go to Tools/Manage User Scripts,
|  select "twitBlocker", and click Uninstall.
|
|
*/

// set up the auto-loader bit
//
// ==UserScript==
// @name          Twitblocker
// @namespace     http://tangerineworks.com/
// @description   hides chatterers in your twitter display
// @include       http://twitter.com/*
// ==/UserScript==


loadjQuery();											// generic jquery loader
run_when_ready('twitBlocker()');		    // twitBlocker() is the function that will be run when jquery is ready



/*
|----------------------------------------------------------------------------------------------------------------------------------
| loadjQuery
|----------------------------------------------------------------------------------------------------------------------------------
|
|	creates a link in the <head> bit to the latest jquery script from :
|	http://jquery.com/src/jquery-latest.js
|
*/

function loadjQuery(){

	var jqueryLink = document.createElement('script');
	jqueryLink.src = 'http://jquery.com/src/jquery-latest.js';
	jqueryLink.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(jqueryLink);
}


/*
|----------------------------------------------------------------------------------------------------------------------------------
| run_when_ready
|----------------------------------------------------------------------------------------------------------------------------------
|
|	The normal $function onload thing doesn't work here, so we loop until jquery has loaded, then 
|   run a function who's name is passed in as a parameter
|	
|
*/
function run_when_ready(functionToRun) {

	if(typeof unsafeWindow.jQuery == 'undefined') { 
		window.setTimeout(run_when_ready(functionToRun),100); 
	}else { 
		$ = unsafeWindow.jQuery; 
		eval(functionToRun);
	}
}



/*
|----------------------------------------------------------------------------------------------------------------------------------
| twitBlocker
|----------------------------------------------------------------------------------------------------------------------------------
|
|	script to hide people who are bombarding twitter with so many messages its doing your head in.
|	
|	It puts down a cookie that expires with the browser, and is depending on the containing <TR> having 
|	a class which contains '.hentry' and an identifier for the person you want to block
|	
|
*/
function twitBlocker() {

	var blockList;
	var blockList=readCookie('block_twits');

		if (blockList!=null){

			var s=blockList.split(',');
			for (i=0;i<s.length ;i++){
				$('.' + s[i]).remove();
			}
		}	


	$('.hentry').dblclick(function(){
		
		var s=$(this).attr('class').split(' ');
		
		var twit=s[s.length-1];			// identifier of ther twitterer
		var tweets=$('.' + twit);		// all the objects containing the above identifier
				
		if ($(tweets).attr('id')){		// check it its got an id - would be better if it checked that its a <tr>

			if (blockList!=null){
				blockList=blockList+','+twit;					
			}else{
				blockList=twit;
			}	
			createCookie('block_twits',blockList,0);
			tweets.remove();
		}
	})
}


/*
|----------------------------------------------------------------------------------------------------------------------------------
| cookie handling stuff 
|----------------------------------------------------------------------------------------------------------------------------------
|
| Courtesy of : http://www.quirksmode.org/js/cookies.html
|
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

