/*
 * No Twitter Trending Bots - userscript by jazzychad
 * (c) 2009
 * You may edit and redistribute this script freely
 * as long as this attribution text is kept intact.
 */
// ==UserScript==
// @name          No Twitter Trending Bots
// @namespace     http://jazzychad.com/
// @description   Removes Twitter Trending bots from Twitter Search results
// @include       http://search.twitter.com/*
// ==/UserScript==

var allLIs, thisLI, resultsUL;
var trendbots = [];
var zapped = 0;


/*
 * This is the list of Twitter Trend Bots.
 * You can add bots that you want to remove from search results,
 * just follow the pattern by adding
 *
 * trendbots.push("botname");
 *
 * to the list.  Case does not matter.
 * If you want to leave one or more of the
 * bots in the twitter search results, just delete the line
 * from the list below.
 */
trendbots.push("tweet_trends");
trendbots.push("realtimetrends");
trendbots.push("twopular");
trendbots.push("twopularfeed");
trendbots.push("twopularalert");
trendbots.push("twithority");
trendbots.push("attrending");
trendbots.push("trending");
trendbots.push("tweetingtrends");
trendbots.push("retweettrends");
trendbots.push("daymix");
trendbots.push("trendingtopics");
trendbots.push("trendsy");

//test to see if searching for bot
var found = 0;
for (var j in trendbots) {
	var bot = trendbots[j];
	if (window.location.toString().indexOf(bot) != -1) {
		found = 1;
	}
} // end for

if (found == 0) {
	removetweets();
}

function removetweets()
{

	allLIs = document.evaluate(
		"//li[@class='result']/div[@class='avatar']/a[@href]",
		document,
		null,
		XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);	


	for (var i = 0; i < allLIs.snapshotLength; i++) {
		var temp = allLIs.snapshotItem(i);

		for (var j in trendbots) {
			if (temp.href.toLowerCase() == ("http://twitter.com/" + trendbots[j].toLowerCase())) {
			
				thisLI = temp.parentNode.parentNode;
				if (thisLI.parentNode) {
					thisLI.parentNode.removeChild(thisLI);
				}
				
				zapped += 1;
			}
		} // end for	
	}


	/* show info window on page to tell how many were removed */
	if (zapped > 0) {
		var report = document.createElement('div');
		var b = document.getElementsByTagName('body')[0];
		var t = "tweets were";
		if (zapped == 1) { t = "tweet was"; }
		report.style.position = "absolute";
		report.style.left = "0px";
		report.style.top = "80px";
		report.style.background = "#3cf";
		report.style.width = "100px";
		report.style.border = "1px solid #777";
		report.style.padding = "5px";
		report.style.margin = "5px";
		
		report.innerHTML = "<b>" + zapped.toString() + "</b> trending bot " + t + " zapped!";
		b.appendChild(report);
	}
} // end func