/* Pirate filter for web pages.
 * Copyright (C) 2006 by Andrew Arensburger.
 *
 * Inspired by Dougal Campbell's Text Filter Suite:
 * http://dougal.gunters.org/
 *
 * This code is distributable under the terms of the GNU Public License
 * (GPL).
 * http://www.fsf.org/licensing/licenses/gpl.html
 */

{	// Turn this code on automatically on Talk Like A Pirate Day.
	var today = new Date();

//	if ((today.getMonth() == 8) && (today.getDate() == 19))
		window.onload = init;
}

/* pi_pats
 * An array of pattern->replacement pairs for the pirate words we'll be
 * replacing.
 */
pi_pats = [
	[ /\b(\S+)ings\b/ig,	"$1in's" ],
	[ /\b(\S+)ing\b/ig,	"$1in'" ],
	[ /\b(d)on\'t know\b/ig,	"$1inna" ],
	[ /\bco-?workers\b/ig,	'shipmates' ],
	[ /\bco-?worker\b/ig,	'shipmate' ],
	[ /\bmanager\b/ig,	'admiral' ],
	[ /\bcaptain\b/ig,	"Cap'n" ],
	[ /\bearlier\b/ig,	'afore' ],
	[ /\bfriend(s)?\b/ig,	'matey$1' ],
	[ /\bpeople\b/ig,	'scallywags' ],
	[ /\b(m)yself\b/ig,	'$1eself' ],
	[ /\bbefore\b/ig,	'afore' ],
	[ /\bboss\b/ig,		'admiral' ],
	[ /\b(d)on\'t\b/ig,	"$1ern't" ],
	[ /\b(y)our\b/ig,	'$1er' ],
	[ /\b(y)ou\b/ig,	'$1e' ],
	[ /\bold\b/ig,		'auld' ],
	[ /\b(t)he\b/ig,	"$1h'" ],
	[ /\b(t)o\b/ig,		"$1'" ],
	[ /\b(m)y\b/ig,		'$1e' ],
	[ /\b(o)f\b/ig,		"$1'" ],
	[ /\b(d)o not\b/ig,	"$1ern't" ],
	[ /\b(n)ever\b/ig,	"$1e'er" ],
	[ /\b(e)ver\b/ig,	"$1'er" ],
	[ /\b(o)ver\b/ig,	"$1'er" ],
	[ /\bYes\b/g,		'Aye' ],
	[ /\bNo\b/g,		'Nay' ],
	[ /\bYeah\b/g,		'Aye' ],
	[ /\byeah\b/g,		'aye' ],
	[ /\b(d)on\'t know\b/ig,	"$1inna" ],
	[ /\b(d)idn\'t know\b/ig,	"$1id nay know" ],
	[ /\b(h)adn\'t\b/ig,	"$1a'nae" ],
	[ /\b(d)idn\'t\b/ig,	"$1i'nae" ],
	[ /\b(w)asn\'t\b/ig,	"$1eren't" ],
	[ /\b(h)aven\'t\b/ig,	"$1a'nae" ],
	[ /\b(f)or\b/ig,	'$1er' ],
	[ /\b(b)etween\b/ig,	'$1etwixt' ],
	[ /\b(a)round\b/ig,	"$1roun'" ],
	[ /\bit\'s\b/ig,	"'tis" ],
	[ /\bwoman\b/ig,	'wench' ],
	[ /\bwomen\b/ig,	'wenches' ],
	[ /\blady\b/ig,		'wench' ],
	[ /\bwife\b/ig,		'lady' ],
	[ /\bgirl\b/ig,		'lass' ],
	[ /\bgirls\b/ig,	'lassies' ],
	[ /\bguy(s)?\b/ig,	'lubber$1' ],
	[ /\bman\b/ig,		'lubber' ],
	[ /\bfellow(s)?\b/ig,	'lubber$1' ],
	[ /\bdude(s)?\b/ig,	'lubber$1' ],
	[ /\bboy\b/ig,		'lad' ],
	[ /\bboys\b/ig,		'laddies' ],
	[ /\bchildren\b/ig,	'little sandcrabs' ],
	[ /\bkids\b/ig,		'minnows' ],
	[ /\bhim\b/ig,		'that scurvy dog' ],
	[ /\bher\b/ig,		'that comely wench' ],
	[ /\bhim\.\b/ig,	'that drunken sailor' ],
	[ /\bhe\b/ig,		'the ornery cuss' ],
	[ /\bshe\b/ig,		'the winsome lass' ],
	[ /\b(h)e\'s\b/ig,	'$1e be' ],
	[ /\b(s)he\'s\b/ig,	'$1he be' ],
	[ /\b(w)as\b/ig,	"$1ere bein'" ],
	[ /\bhey\b/ig,		'avast' ],
	[ /\bher\.\b/ig,	'that lovely lass' ],
	[ /\bfood\b/ig,		'chow' ],
	[ /\bmoney\b/ig,	'doubloons' ],
	[ /\bdollars\b/ig,	'pieces of eight' ],
	[ /\bcents\b/ig,	'shillings' ],
	[ /\broad(s)?\b/ig,	'sea$1' ],
	[ /\bstreet(s)?\b/ig,	'river$1' ],
	[ /\bhighway(s)?\b/ig,	'ocean$1' ],
	[ /\bprobably\b/ig,	'likely' ],
	[ /\bidea\b/ig,		'notion' ],
	[ /\bcar(s)?\b/ig,	'boat' ],
	[ /\btruck(s)?\b/ig,	'schooner$1' ],
	[ /\bmachine\b/ig,	'contraption' ],
	[ /\bdriving\b/ig,	'sailing' ],
	[ /\bunderstand\b/ig,	'reckon' ],
	[ /\bdrive\b/ig,	'sail' ],
	[ /\bdied\b/ig,		'snuffed it' ],
	// Added by arensb
	[ /\bMr\.\b/g,		"Dread Cap'n" ],
	[ /\bandrew arensburger\b/ig,	"Captain Blood" ],
	[ /\bis\b/ig,		'be' ],
	[ /\b(a)nd\b/ig,	"$1n'" ],
	[ /\bI think\b/g,	"methinks" ],
	[ /\beyes\b/ig,		'deadlights' ],
	[ /\b\$([0-9])(\s)\b/ig,	'$1 pieces of eight$2' ],
	[ /\binternet\b/ig,	'Spanish Main' ],
	[ /\bGod\b/g,		'LeChuck' ],
	[ /\bblog(s)?\b/ig,	'log$1' ],
	[ /\bweblog(s)?\b/ig,	"ship's log$1" ],
	[ /\baren\'t any\b/ig,	'be no' ],
	[ /\bare\b/ig,		'be' ],
];

/* pi_pat_funcs
 * These are substitutions that use functions to compute the replacement
 * text. Safari 1 doesn't support these, so they're in a separate array.
 * We'll join them to pi_pats later, if supported.
 */
pi_pat_funcs = [
	[ /ar/ig,
		function(match) {
			if (Math.random() < 0.2)
				return "ARRR";
			return "ar";
		} ],
	[ /(\.\s)/g,
		function(match) {
			var n = Math.floor(Math.random()*shouts.length*3);
			if (n < shouts.length)
			{
				var shout = shouts[n];
				return shout.replace(/@@/, match);
			}
			return match;
		} ],
	[ /([!\?]\s)/g,
		function(match) {
			var n = Math.floor(Math.random()*shouts.length*2);
			if (n < shouts.length)
			{
				var shout = shouts[n];
				return shout.replace(/@@/, match);
			}
			return match;
		} ],
];

/* shouts
 * Random phrases to add at the end of sentences. "@@" is replaced by the
 * original end of the sentence, usually ". ", "! ", or "? ".
 */
var shouts = [
	", avast@@",
	"@@Ahoy! ",
	", and a bottle of rum! ",
	", by Blackbeard's sword@@",
	", by Davy Jones' locker@@",
	"@@ Walk the plank! ",
	"@@ Aarrr! ",
	"@@ Yaaarrrrr! ",
	", pass the grog! ",
	", and dinna spare the whip! ",
	", with a chest full of booty@@",
	", and a bucket o' chum@@",
	", we'll keel-haul ye! ",
	"@@ Shiver me timbers! ",
	"@@ And hoist the mainsail! ",
	"@@ And swab the deck! ",
	", ye scurvey dog@@",
	"@@ Fire the cannons! ",
	", to be sure@@",
	", I'll warrant ye@@",
	", on a dead man's chest! ",
	"@@ Load the cannons! ",
	"@@ Prepare to be boarded! ",
	", I'll warrant ye@@",
	"@@ Ye'll be sleepin' with the fishes! ",
	"@@ The sharks will eat well tonight! ",
	"@@ Oho! ",
	"@@ Fetch me spyglass! ",
];

{
	/* See whether this implementation allows using a function as
	 * the second argument to String.replace().
	 */
	var test = "x";
	test = test.replace(/x/, function() { return "y" });
	if (test == "y")
		// We can use a function. Prepend pi_pat_funcs to pi_pats
		pi_pats = pi_pat_funcs.concat(pi_pats);
}

function log(msg)
{
	if (debug == undefined)
		return;
	debug.innerHTML += msg + "\n";
}

/* init
 * Get this show on the road. Piratify the text.
 */
function init()
{
	debug = document.getElementById("jsdebug");

	// Get the "piratified" version of the page contents, and
	// replace the original contents.
	newstr = piratify(document.body.innerHTML);
	document.body.innerHTML = newstr;
}

function piratify(str)
{
	// Ignore strings consisting entirely of whitespace
	if (/^\s*$/.exec(str))
		return str;

	// Replace each pirate word in turn.
	for (var i = 0; i < pi_pats.length; i++)
		str = str.replace(pi_pats[i][0], pi_pats[i][1]);
	return str;
}

