// Presidential Civilizer
// version 1.0 Super-forever-beta
// 09-11-09
// By Andrew Venell
// http://andrewvenell.com
// based on Jmaxxz vulgar word blocker
// --------------------------------------------------------------------
//
// 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.
//
// This script uses code cobbled together from other scripts.  
// Please feel free to modify this code as you see fit in order 
// to make it work with other image sharing sites.
//
// --------------------------------------------------------------------
// ==UserScript==
// @name            Presidential Civilizer
// @namespace       http://andrewvenell.com/civilizer/
// @description     Replace all instances of Barry Hussein Obama
// @include       *
// @include           http://*.facebook.com/*
// ==/UserScript==

(function() { 
var bad = [], good = [], modifiers = [];

populate({

 "Barry Hussein Obama": "the President, who I admire,",
 "\"Barry\" Hussein Obama": "the President, who I admire,",
 "Barry \"Hussein\" Obama": "the President, who I admire,",
 "“Barry” Hussein Obama": "the President, who I admire,",
 "Barry “Hussein” Obama": "the President, who I admire,",
 "(Barry) Hussein Obama": "the President, who I admire,",
 "Barry (Hussein) Obama": "the President, who I admire,",
 }, "gi");  

function populate(replacements, flags) { 
  var word, modPos, mod; 
  for(var key in replacements) { 
    if((modPos = key.indexOf("/")) > -1) { 
      mod = key.substring(modPos + 1); 
      word = key.substring(0, modPos); 
    } else { 
      mod = ""; 
      word = key; 
    } 
    modifiers.push(mod); 
    bad.push(new RegExp(word, flags)); 
    good.push(replacements[key]); 
  } 
} 



// this function does the replacements 
function sanitize(s, noContext, notredirect) { 
	
  for (var j = 0; j < bad.length; j++) { 
    if(noContext && modifiers[j].indexOf("c") != -1 || notredirect && modifiers[j].indexOf("r") !=-1 ) {  
     continue;
    } 
    s = s.replace(bad[j], good[j]);
  } 
  return s;  
} 

// replace in title 
if(document.title) 
{
	var temp = sanitize(" "+document.title+" ", false , true);
	document.title = temp.substring(1,temp.length -1);
	
}

// replace in body text 
var textnodes = document.evaluate( "//body//text()", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
  for (var i = 0; i < textnodes.snapshotLength; i++) { 
  node = textnodes.snapshotItem(i);
  node.data = sanitize(" "+node.data+" ", false, true);
  node.data = node.data.substring(1,node.data.length -1);
}

})();
