// ==UserScript==
// @name           Facebook Fresh(TM)
// @include        http://*.facebook.com/profile.php?id=*
// ==/UserScript==

var goodApps = new Array(
	'2327158227',
	'2352557895',
	'4904702269',
	'2305272732',
	'2719290516',
	'2341989679',
	'2327158227',
	'2356318349',
	'2407511955',
	'2386512837',
	'2297529396',
	'2503140832'
);

function appSucks(appID) {
	for (var i=0; i<goodApps.length; i++)
		if (appID == goodApps[i]) return false;
	return true;
}

function xpath(query,base) {
    return document.evaluate(query, base, null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

function doAll(set, func) {
	for (var i=0; i<set.snapshotLength; i++)
		func(set.snapshotItem(i));
}

function hideIfAppSucks(element,idPrefix) {
	if (appSucks(element.id.substr(idPrefix.length))) {
		element.style.display = 'none';
		hidden++;
	}
}

function unhideAll() {
	doAll(links,function(e) { e.style.display = 'block'; });
	doAll(icons,function(e) { e.style.display = 'block'; });
	doAll(apps,function(e) { e.style.display = 'block'; });
	doAll(stories,function(e) {
		e.parentNode.parentNode.style.display = 'block';
	});
	wallAttachments.style.display = 'block';
	hidden=0;
}

function hideAll() {
	doAll(links,function(e) { hideIfAppSucks(e,'action_app_'); });
	doAll(icons,function(e) { hideIfAppSucks(e,'icon_app'); });
	doAll(apps,function(e) { hideIfAppSucks(e,'box_app_'); });
	doAll(stories,function(e) { 
		if (appSucks(e.href.substr(e.href.indexOf('app_id=')+7)))
			e.parentNode.parentNode.style.display = 'none';
	});
	wallAttachments.style.display = 'none';
}

function toggleHiddenApps() {
	if (hidden) {
		showTxt.innerHTML = 'Hide annoying applications';
		unhideAll();
	} else {
		showTxt.innerHTML = 'Show hidden applications';
		hideAll();
	}
}

var hidden = 0;
var profile = document.getElementById('userprofile');
var profileActions = document.getElementById('profileActions');
var links = xpath("//a[contains(@id,'action_app_')]", profileActions);
var icons = xpath("//div[contains(@id,'icon_app')]", profile);
var apps = xpath("//div[contains(@id,'box_app_')]", profile);
var stories = xpath("//a[contains(@href,'app_id=')]", profile);
var wallAttachments = document.getElementById('attachment_buttons_list');
hideAll();

if (hidden > 0) {
	var showBtn = document.createElement('a');
	var showTxt = document.createElement('div');
	showTxt.innerHTML = 'Show hidden applications';
	showTxt.className = 'holder';
	showBtn.appendChild(showTxt);
	showBtn.addEventListener('click',toggleHiddenApps,false);
	profileActions.appendChild(showBtn);
}

