// ==UserScript==
// @name          Boingboing boingboing
// @namespace     http://jpmullan.com
// @description   transform Boingboing comments into reasonable and smart ones
// @include       http://*boingboing*/*
// ==/UserScript==

// Based on feyntube, Written 2008 by Julien Oster <feyntube@julien-oster.de>, find
// newest version at http://www.julien-oster.de/projects/feyntube

function boingnode(node) {
    var text = "";
    // if the node has children, loop through them
    if (node.hasChildNodes()) {
	var children = node.childNodes;
	for (var i=0; i < children.length; i++) {
	    boingnode(children[i]);
	}
    } else if (node.nodeName == "#text") {
	if (!node.nodeValue.match(/^\w+\s+said$/)
	    && 'Recent Comments' != node.nodeValue) {
	    node.oldText = node.nodeValue;
	    node.newText = boingtext(node.nodeValue);
	    node.nodeValue = node.newText;
	    /*
	    node.parentNode.addEventListener('mouseover', function (event) {
		node.nodeValue = node.oldText;
	    }, false)
	    node.parentNode.addEventListener('mouseout', function (event) {
		node.nodeValue = node.newText;
	    }, false);
	    */
	}
    }
    return text;
}
window.random_cthulhus = new Array(" Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn ",
				   " Cthulhu R'lyeh ",
				   " Cthulhu fhtagn ");
			    
function boingtext(input_string) {
    if (0.975 < Math.random()) {
	return window.random_cthulhus[Math.floor(Math.random() * window.random_cthulhus.length)];
    }
    var input_parts = input_string.split(/\b/);
    var output_string = '';
    var part;
    for (var j = 0; j < input_parts.length; j++) {
	part = input_parts[j];
	if (part.match(/^[A-Z]+$/)) {
	    output_string += 'BOING';
	} else if (part.match(/^[A-Z]\w+$/)) {
	    output_string += 'Boing';
	} else if (part.match(/^[a-z][a-z]+$/)) {
	    output_string += 'boing';
	} else if ('I' == part) {
	    output_string += 'Boing';
	} else if (part.match(/^[0-9]+th$/)) {
	    output_string += 'boingth';
	} else if (part.match(/^[0-9]+rd$/)) {
	    output_string += 'boingrd';
	} else if (part.match(/^[0-9]+st$/)) {
	    output_string += 'boingst';
	} else {
	    output_string += part;
	}
    }
    return output_string;
}
var allDivs = document.getElementsByTagName('div');

for (var i = 0; i < allDivs.length; i++) {
    div = allDivs[i];
    if (div.getAttribute('id') == 'block-recentcomments'
	|| (div.hasAttribute('class')
	    && div.getAttribute('class').match(/comment-content/))) {
	boingnode(div);
    }
}

