// A TitanTV redesign has made this script obsolete.  As I find myself using
// TitanTV less and less since the school year began, I do not intend to 
// update it.  In its place, I recommend using "Remove ads from TitanTv" which
// does much of what this script did.
//  --Tom, Dec 17, 2006

// ==UserScript==
// @name           TitanTV Zeus -- smiting the beast!
// @namespace      http://freecog.net/2006/
// @description    Removes ads and excess chrome from the TitanTV grid.  Also restyles the listings table to be a bit more low-key.  (I could use some help from a real designer here, my color scheme isn't much of an imrovement.)  Grid listings are given logical class names, so targeting them from userContent.css should be much easier (again, if you have a good color scheme, please contact me!).  Finally, listings are filtered based on their text content (you will likely want to edit this part to your tastes).
// @include        *.titantv.com/ttv/Grid/grid.aspx
// ==/UserScript==

(function(){

// EDIT THIS:
var filterStrings = [
	'Off-Air', // PBS
	'Paid Programming',
	'Infomercials',
	'(Religious', // The whole category
	'Jack Van Impe Presents', // Not real news
	'Fox News Sunday', // Ditto
	'Insider(News)', // Again
	'Web Junk', // Called 'junk' for a reason
	'I Love the \'70s',
	'Golf',
	'Maximum Exposure',
	'Reba',
	'Big Brother',
	'Auto Racing',
	'Elimidate',
	'Entertainment Tonight',
	'Access Hollywood',
	'Sportsman Channel',
	'People\'s Court',
	'Divorce Court',
	'Judge Alex',
	'Maury',
	'Tyra Banks',
	'The View',
	'Judge Mathis',
	'Judge Joe Brown',
	'Judge Judy',
	'Eye for an Eye',
	'Judge Hatchett',
	'Home Shopping',
	'My Wife and Kids', // Annoying
	'Everybody Loves Raymond', // HATEHATEHATEHATEHATE!!!
	'The Parkers',
	'Blind Date',
	'Inside Edition',
	'Ellen DeGeneres', // HATEHATEHATEHATEHATE!!!
	'Tony Danza Show', // DIEDIEDIEDIEDIE!!!
	'Oprah Winfrey', // Best of the bunch.
	'Yes, Dear',
	'Bernie Mac',
	'8 Simple Rules',
	'Wheel of Fortune',
	'Dukes of Hazzard', // Dead boring
	'CSI: Miami',
	'EntertainmentStudios.com', // WTF?
	'King of Queens',
	'Will & Grace',
	'Supernanny',
	'Wife Swap',
	'Who Wants to Be a Millionaire',
	'Roseanne',
	'Cheaters',
	'Sex and the City',
	'America\'s Funniest Home Videos',
	'Next(Game)',
];


var hasRun = false;
var removeIDs = [
	'leaderboard',
	'leaderboard',
	'footernav',
	'footer'
];

// Color scheme:
// http://wellstyled.com/tools/colorscheme2/index-en.html?tetrad;84;0;150;0.3;-0.8;0.3;0.5;0.1;0.9;0.5;0.75;0.3;-0.8;0.3;0.5;0.1;0.9;0.5;0.75;0.3;-0.8;0.3;0.5;0.1;0.9;0.5;0.75;0.3;-0.8;0.3;0.5;0.1;0.9;0.5;0.75;0

var stylesheet = [
	'.general { background-color: #FFF2E6; }',
	'.action, .c1 { background-color: #FFEF80; }',
	'.children, .c2 { background-color: #E6FFBF; }',
	'.comedy, .c3 { background-color: #BFE3FF; }',
	'.drama, .c4 { background-color: #FFBFEF; }',
	'.educational, .c6 { background-color: #F5FFE6; }',
	'.game, .c7 { background-color: #CFDCE6; }',
	'.music, .c9 { background-color: #FFE6F9; }',
	'.nature, .c10 { background-color: #CCFF80; }',
	'.news, .c11 { background-color: #E6CFE0; }',
	'.talk { background-color: #E6F4FF; }',
	'.soap { background-color: #FFC080; }',
	'.documentary { background-color: #80C8FF; }',
	'.sports { background-color: #efe9a1; }', // Default
	'.how-to { background-color: #ffe1ff; }', // Default
	'.religious { background-color: #f3fff0; }', // Default
	'.sci-fi { background-color: #bac8f3; }',
	
	'.filtered > * { visibility: hidden; }',
	
	'td.filtered:hover > * {',
		'visibility: visible !important;',
		'opacity: .5;',
	'}',
	
	'.filtered + .filtered {',
		'border-left-width: 0;',
	'}',
	
	// Hide that stupid little ad in the tabs
	'td.gridbutton iframe {',
		'display: none !important;',
	'}',
	
	'td.gC a img {',
		'opacity: .2;',
	'}',
	'td.gC:hover a img {',
		'opacity: .6;',
	'}',
	
	'.gridTable, .gridTable td {',
		'border-color: #596F80 !important;',
	'}',
].join(' ');

var colorClasses = {
	"#eeeee0": "general",
	"#ffec8b": "action",
	"#dbf0fc": "children",
	"#c5e6ff": "comedy",
	"#ffbbff": "drama",
	"#eee9bf": "educational",
	"#ffe4e1": "game",
	"#ffe4c4": "music",
	"#d7ead8": "nature",
	"#eecbad": "news",
	"#c1cdc1": "talk",
	"#ffc1c1": "soap",
	"#e0dbef": "documentary",
	"#efe9a1": "sports",
	"#ffe1ff": "how-to",
	"#f3fff0": "religious",
	"#bac8f3": "sci-fi",
};


function main() {
	if (hasRun) return;
	hasRun = true;
	var n;
	
	for (var i = 0; i < removeIDs.length; i++) {
		n = document.getElementById(removeIDs[i]);
		n.parentNode.removeChild(n);
	}
	
	n = document.getElementById('adbar');
	n.parentNode.parentNode.removeChild(n.parentNode);
	
	var nodes = unsafeWindow.document.evaluate(
		'//td[@class="gridBannerRowCell"]/..', 
		unsafeWindow.document.body, null, 
		XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

	n = nodes.iterateNext();
	var toRemove = [];
	while (n) {
		toRemove.push(n);
		n = nodes.iterateNext();
	}
	for (var i = 0; i < toRemove.length; i++) {
		toRemove[i].parentNode.removeChild(toRemove[i]);
	}

	// Change styled buttons to normal ones
	//   Not that they're really all that ugly, but the lack
	//   of hover and especially "clicked" states drives me crazy.
	var iterator = unsafeWindow.document.evaluate(
		'//input[@class="buttonblue"]', 
		unsafeWindow.document.body, null, 
		XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
	var nodes = [];
	n = iterator.iterateNext();
	while (n) {
		nodes.push(n);
		n = iterator.iterateNext();
	}
	for (var i = 0; i < nodes.length; i++) {
		nodes[i].className = '';
	}
	// Same for the "Go" button
	n = document.getElementById('ucGrid_ucQS_btnSearch');
	n.className = '';
	n.removeAttribute('style');

	// Switch inline colors on the grid to classes
	for (var k in colorClasses) {
		var iterator = unsafeWindow.document.evaluate(
			'//td[@bgcolor="' + k + '"]', 
			unsafeWindow.document.body, null, 
			XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
		n = iterator.iterateNext();
		var nodes = [];
		while (n) {
			nodes.push(n);
			n = iterator.iterateNext();
		}
		for (var i = 0; i < nodes.length; i++) {
			var n = nodes[i];
			n.removeAttribute('bgcolor');
			// These handle mouseover coloring--we replace them with a 
			// :hover pseudoclass.
			n.removeAttribute('onmouseout');
			n.removeAttribute('onmouseover');
			// Remove any inline background-color, in case the cell has
			// been hovered already.
			n.style.backgroundColor = '';
			n.className += ' gridCell ' + colorClasses[k];
			var text = n.textContent;
			for (var x = 0; x < filterStrings.length; x++) {
				if (text.indexOf(filterStrings[x]) > -1) {
					n.className += ' filtered';
					break;
				}
			}
		}
	}
	
	var s = document.createElement('style');
	s.appendChild(document.createTextNode(stylesheet));
	document.body.appendChild(s);
	s.setAttribute('type', 'text/css');
}

main();
//window.addEventListener('load', main, false);

})();