// ==UserScript==
// @name           CAOD Cleaner
// @namespace      http://myeve.eve-online.com
// @description    Removes unwanted drivel
// @include        http://myeve.eve-online.com/ingameboard.asp?a=topic*
// ==/UserScript==

/**************************************************
	CAOD Cleaner v2.2
	Created by Randell
	Hacked by Jonny Damordred

	Greasemonkey Script that provides filtering
	and hiding functions for CAOD.  Could
	probably work for the whole of the forums,
	but I don't know if thats needed...

	Version 2.1 --> Version 2.2
	---------------------------------------------
	  *  Added the feature to automaticly hide
	     no-corp and no-alliance posts
	  *  Removed the version number from the
	     script name, alowing the users to update
	     without blowing away their settings
	  *  Added a default to the filter text

	Version 2.0 --> Version 2.1
	---------------------------------------------
	  *  Fixed bug where all posts were
	     displayed hidden, always.

***************************************************/

// Namespace object, just to keep things tidy
var CAODCleaner = new Object;

// array containing the text strings to filter by
CAODCleaner.filterArray = ["HERRO KITTY"];

// string containing the url to the current page,
// used to refresh the page afterword
CAODCleaner.urlString = "";

// get the filter text, and fill in
CAODCleaner.getFilterText = function() {
	var myText = GM_getValue("filterText", "HERRO KITTY|Moon Kitten|G00NFLEET|Rail Duke");
	return myText.split("|");
}

// add text to the filter array
CAODCleaner.addFilterText = function(myText) {
	// add the new item to the filtertext, then save it
	CAODCleaner.filterArray.push(myText);
	var filterText = CAODCleaner.filterArray.join("|");
	GM_setValue("filterText", filterText);

	// reload the page
	document.location.href = CAODCleaner.urlString;
}

// remove text from the filter array
CAODCleaner.removeFilterText = function(myText) {
	var newArray = [];

	var i = 0;
	while (i < CAODCleaner.filterArray.length) {
		if (CAODCleaner.filterArray[i] == myText) {
			CAODCleaner.filterArray.splice(i, 1);
		} else {
			i++;
		}
	}

	var filterText = CAODCleaner.filterArray.join("|");
	GM_setValue("filterText", filterText);

	// reload the page
	document.location.href = CAODCleaner.urlString;
}

// gets all the elements in the document with the supplied class names
CAODCleaner.getElementsByClassArray = function(element, classNames) {
	var myElements = document.getElementsByTagName(element);
	var returnElements = [];

	for(var i = 0; i < myElements.length; i++) {
		for(var j = 0; j < classNames.length; j++) {
			if(myElements[i].className == classNames[j]) {
				returnElements.push(myElements[i]);
			}
		}
	}

	return returnElements;
}

/**************************************************
	Post Object
	Handle the filtering of posts, and handle any
	events for that post.
***************************************************/

CAODCleaner.Post = function(myElement, isFiltered, filterText) {
	// divs holding the char information, post, and toggle button
	// they are filled out later
	this.infoDiv = "";
	this.postDiv = "";
	this.toggleDisplay = "";

	// status information
	this.isFiltered = isFiltered;
	this.isShown = !isFiltered;
	this.filterText = filterText;

	var name = "";
	var corp = "";
	var alliance = "";

	name = myElement.getElementsByTagName("span")[0].innerHTML;
	if (myElement.getElementsByTagName("i").length == 1) corp = myElement.getElementsByTagName("i")[0].innerHTML;
	if (myElement.getElementsByTagName("b").length == 2) alliance = myElement.getElementsByTagName("b")[1].innerHTML;

	//
	if(corp == "" && alliance == "") this.isShown = false;

	/* Setup the left side of the post */
	this.infoDiv = document.createElement("div");
	this.infoDiv.innerHTML = myElement.innerHTML;
	myElement.innerHTML = "";

	myElement.appendChild(this.infoDiv);

	/* Setup the right side of the post */
	// get the text row
	var postRow = myElement.parentNode;
	var textTD = postRow.getElementsByTagName("td")[1];

	// make the gui DIV
	var guiDiv = document.createElement("div");
	guiDiv.style.paddingBottom = "3px";
	guiDiv.style.paddingTop = "3px";

	// toggle button
	this.toggleDisplay = document.createElement("span");
	this.toggleDisplay.style.color = "#FFA500";
	this.toggleDisplay.addEventListener('click', this.toggleShowClosure(), true);

	// Name, Corp and Alliance buttons
	var nameSpan = document.createElement("span");
	nameSpan.style.color = "#FFA500";
	nameSpan.innerHTML = name;
	nameSpan.addEventListener('click', this.filterTextClosure(name), true);

	var corpSpan = document.createElement("span");
	corpSpan.style.color = "#FFA500";
	corpSpan.innerHTML = corp;
	corpSpan.addEventListener('click', this.filterTextClosure(corp), true);

	var allianceSpan = document.createElement("span");
	allianceSpan.style.color = "#FFA500";
	allianceSpan.innerHTML = alliance;
	allianceSpan.addEventListener('click', this.filterTextClosure(alliance), true);

	// unfilter button
	var unfilterSpan = document.createElement("span");
	unfilterSpan.style.color = "#FFA500";
	unfilterSpan.innerHTML = "[unfilter]";
	unfilterSpan.addEventListener('click', this.unfilterClosure(), this);

	// put it all together
	guiDiv.appendChild(this.toggleDisplay);
	guiDiv.appendChild(document.createTextNode(" -- "));
	guiDiv.appendChild(nameSpan);
	guiDiv.appendChild(document.createTextNode(" / "));
	guiDiv.appendChild(corpSpan);
	guiDiv.appendChild(document.createTextNode(" / "));
	guiDiv.appendChild(allianceSpan);

	// make the post div
	this.postDiv = document.createElement("div");
	this.postDiv.innerHTML = textTD.innerHTML;
	this.postDiv.style.marginTop = "5px";
	this.postDiv.style.paddingTop = "5px";
	this.postDiv.style.borderTop = "1px solid black";

	// put it back together
	textTD.innerHTML = "";
	textTD.appendChild(guiDiv);
	textTD.appendChild(this.postDiv);

	// if this post needs to be hidden, do so
	if(!this.isShown) {
		this.postDiv.style.display = "none";
		this.infoDiv.style.display = "none";
		this.toggleDisplay.textContent = "[show]";

		if(this.isFiltered) {
			guiDiv.appendChild(document.createTextNode(" -- "));
			guiDiv.appendChild(unfilterSpan);
		}
	} else {
		this.toggleDisplay.textContent = "[hide]";
	}
}

CAODCleaner.Post.prototype = {
	filterTextClosure: function(newText) {
		var parent = this;
		var myText = newText;

		function filterText(event){
			event.preventDefault();
			CAODCleaner.addFilterText(myText);
		}

		return filterText;
	},

	// remove text this post was filtered by from the list
	unfilterClosure: function() {
		var parent = this;

		function unfilter(event){
			event.preventDefault();
			CAODCleaner.removeFilterText(parent.filterText);
		}

		return unfilter;
	},

	// toggle if the div should be shown or not
	toggleShowClosure: function() {
		var parent = this;

		function toggleShow(event){
			event.preventDefault();

			if(parent.isShown) {
				parent.postDiv.style.display = "none";
				parent.infoDiv.style.display = "none";
				parent.toggleDisplay.textContent = "[show]";
				parent.isShown = false;
			} else {
				parent.postDiv.style.display = "block";
				parent.infoDiv.style.display = "block";
				parent.toggleDisplay.textContent = "[hide]";
				parent.isShown = true;
			}
		}

		return toggleShow;
	}
}

/**************************************************
	Now with the preliminaries done, lets get on
	with the main show
***************************************************/

// I have no idea how this works, but it does.  Smile, nod, and move on...
var CAOD, thisCAOD;
CAOD = document.evaluate("//a[@href='?a=channel&channelID=3521']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
thisCAOD = CAOD.snapshotItem(0);

if(thisCAOD.innerHTML=="Corporation, Alliance and Organization Discussions") {
	// save the url of this page for later
	CAODCleaner.urlString = document.location.href;

	// get the saved filtertext from the saved Greasemonkey value
	CAODCleaner.filterArray = CAODCleaner.getFilterText();

	// each post in the thread is split into two tds which can have one of
	// three classes: mbForumFirst, mbForum, mbForumAlt. We'll need to collect them all
	var postArray = CAODCleaner.getElementsByClassArray("td", ["mbForumFirst", "mbForum", "mbForumAlt"]);

	// loop over the array of elements
	for(var i = 0; i < postArray.length; i++) {
		var isFiltered = false;
		var filterText = "";

		if(!postArray[i].innerHTML.match("img.eve.is")) continue;

		// if we aren't filtering anything, why bother checking?
		if(CAODCleaner.filterArray.length != 0) {
			// loop over all the filtertext elements, setting this post to be filtered
			// if needed, and saving the filtertext that triggered it
			for(var j = 0; j < CAODCleaner.filterArray.length; j++) {
				if(CAODCleaner.filterArray[j] == "") continue;

				if(postArray[i].innerHTML.match(CAODCleaner.filterArray[j])) {
			 		isFiltered = true;
					filterText = CAODCleaner.filterArray[j];
					break;
				}
			}
		}

		new CAODCleaner.Post(postArray[i], isFiltered, filterText);
	}
}