// ==UserScript==
// @name           Facebook Purity - Removes annoying quiz and application messages from your facebook homepage
// @namespace      http://steeev.freehostia.com
// @description    Removes messages posted by applications to your facebook homepage
// @include        http://www.facebook.com/*
// @include        http://www.new.facebook.com/*
// @version        1.54b - 24th June 2009
// ==/UserScript==

// UPDATES
// 1.51  30th March 2009 Bug fixed: if there were no pending requests, the script didnt work
// 1.52   4th April 2009 removed GM_addStyle command, for better compatibility with other browsers (chrome + opera)
// 1.53  26th April 2009 changed insertpoint so its not dependent on suggestions box
// 1.54  27th April 2009 script is now compatible with Google Chrome + Opera (and possibly safari, not tested yet)
// 1.54a  5th May   2009 fixed a minor bug
// 1.54b 24th June  2009 fixed for facebook code change
// 14.10.2009: daddyspambox@gmail.com: Fixed for code change, using xpath to get the desired elements (more flexible)

//
// (C) stephen fernandez 2009        http://steeev.freehostia.com/wp/
//

// If you like this script please donate big or small donations, anything is welcome
// http://steeev.freehostia.com/donate/

//
//

xQuery = "//div[contains(@data-ft,'app_id') and not(contains(@class,'fbpblocked'))]";

// Evaluate xPath against the given element or document if no element is given.
function xPath(query, doc) {
    var xResult, arrResult = [];
	if (!doc) {
		doc = document;
	}
	xResult = document.evaluate(query, doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);	
	for (i = 0; item = xResult.snapshotItem(i); i++) {
		arrResult.push(item);
	}
	return arrResult;
} 

window.addEventListener('load', 
 function () {
  GM_log('on load');
  var crappyappmsgcounter=0;
  var fbpstyle=document.createElement('style');
  fbpstyle.textContent='.fbpblocked {border-style: dashed; border-width:1px; border-color: lightpink}';
  if (document.getElementsByTagName('head'))
      document.getElementsByTagName('head')[0].appendChild(fbpstyle);

  document.addEventListener("DOMNodeInserted", fpInsertedNodeDomHandler, false);

  function fpInsertedNodeDomHandler(event) {
    if(location.href.match('\/home\.php') && event.target.getElementsByClassName && event.target.getElementsByClassName('UIIntentionalStory_AttachmentInfo'))
      cleartheshizzle(event.target);          
  }

  var cleartheshizzle=function(thenode) {
    GM_log('clearing...');
    if(!document.getElementById('fbpblockcount')) {
    
      fbpshowblocked=function () {

        var showhidelink=document.getElementById('fbpshowblockedlink');
        var showorhidetext=document.getElementById('fbpshowblockedlink').textContent;
        var blockedmsgs=document.getElementsByClassName('fbpblocked');
      
        if (showorhidetext=='Show') {
          document.getElementById('fbpshowblockedlink').innerHTML='Hide';
          displaymode='block';
        }
        else  {
          document.getElementById('fbpshowblockedlink').innerHTML='Show';
          displaymode='none';
        }
      
        for(i in blockedmsgs) {
          try {
            blockedmsgs[i].style.display=displaymode;
          }
          catch (e) { 
            ;
          }
        }
      
        blockedmsgs=null;
      } // END fbshowblocked function

      crappyappmsgcounter=0;
      var insertpoint = document.getElementById('pagelet_reqbox');
      if(insertpoint && insertpoint.firstChild) {
        var fbpurityinfo=document.createElement('div');
        fbpurityinfo.setAttribute('class','UIOneOff_Container');
        fbpurityinfo.style.marginBottom='12px';
        fbpurityinfo.innerHTML='<a href="http://bit.ly/fbpure">FB Purity</a> blocked: <span id="fbpblockcount">0</span> app msgs [ <a id="fbpshowblockedlink" href="javascript:;">Show</a> ]';
        insertpoint.insertBefore(fbpurityinfo, insertpoint.firstChild);
        document.getElementById('fbpshowblockedlink').addEventListener("click", fbpshowblocked, false);
        fpbblockcountspan=document.getElementById('fbpblockcount');
      }
    }
    var footernodes=xPath(xQuery, thenode);
    GM_log(thenode.innerHTML);
    GM_log("hits: " + footernodes.length);
    for(i=0;i<footernodes.length;i++) {
        par = footernodes[i];
        par.style.display='none';
        par.setAttribute('class',par.getAttribute('class')+' fbpblocked');
        if(fpbblockcountspan)
          fpbblockcountspan.innerHTML=++crappyappmsgcounter;      
    }
  footernodes=null;
  }
  
  if(location.href.match('\/home\.php'))
    cleartheshizzle(document);
    
}
, true);
