// ==UserScript==
// @name           dA Plonk
// @namespace      http://maat.fivebyfive.be/
// @include        http://*.deviantart.com/*
// ==/UserScript==


/*
	daPlonk 0.2
	Copyright (C) 2008 Kalle Raisanen.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


function addClassName(obj, className) {
	if(obj.className.indexOf(className) == -1)
		obj.className += (obj.className.length ? ' ' : '') + className;
}

function removeClassName(obj, className) {
	if(obj.className.indexOf(className) != -1) {
		var re = new RegExp(className);
		obj.className = obj.className.replace(re, '');
	}
}


String.prototype.toRegExp = function() {
	if(this.match(/^\/.*?\/[igm]*/)) {
		var p = this.split(/\//);
		return new RegExp(p[1], p[2]);
	} else {
		return new RegExp('^' + this + '$', 'i');
	}
}

Array.prototype.indexOfRegExp = function(haystack, startIndex) {
	if(!this.length) return -1;

	var start = (typeof startIndex != 'undefined' ? startIndex : 0);

	for(var i = start; i < this.length; i++) {
		var re = this[i] instanceof RegExp ? this[i] : this[i].toRegExp();

		if(haystack.match(re))
			return i;
	}

	return -1;
};


var daPlonk = {
	VERSION:    '0.2'

	,CLASSNAME: 'dp-hidden'
	,STYLE:     '.dp-hidden * { display: none; } .dp-hidden { height: 0 !important;}'

	,plonklist: ''
	,comments:  []


	,_init: function() {
		var dAdiv = document.getElementById('deviantART-v6');

		if(dAdiv) {
			GM_addStyle(daPlonk.STYLE);

			daPlonk.plonklist = String(GM_getValue('plonklist', ''));

			GM_registerMenuCommand('Set daPlonk list', daPlonk.set_plonklist);

			var commentrange = document.evaluate("//div[@class='thought block']", dAdiv, null, XPathResult.ANY_TYPE, null);

			if(commentrange) {
				var current = commentrange.iterateNext();
				while(current) {
					daPlonk.comments.push(current.id);
					current = commentrange.iterateNext();
				}

				if(daPlonk.comments.length) {
					daPlonk.do_plonks();
				}
			}
		}
	}


	,do_plonks: function() {
		var plonk_arr = daPlonk.plonklist.length ? daPlonk.plonklist.split(/\s*,\s*/) : [];

		for(var i = 0; i < daPlonk.comments.length; i++) {
			var div  = document.getElementById(daPlonk.comments[i]);
			removeClassName(div, 'dp-hidden');

			if(plonk_arr.length > 0) {
				var auth = daPlonk.get_author(daPlonk.comments[i]);

				if(auth) {
					if(plonk_arr.indexOfRegExp(auth) != -1)
						addClassName(div, 'dp-hidden');
				}
			}
		}
	}

	,get_author: function(par) {
		var pdiv = document.getElementById(par);
		if(pdiv) {
			var cspans = pdiv.getElementsByTagName('span');

			for(var i = 0; i < cspans.length; i++) {
				if(cspans[i].className.match(/author/)) {
					return cspans[i].innerHTML.replace(/<.*?>|[^\w\-]/g, '');
				}
			}
		}
		return '';
	}

	,set_plonklist: function() {
		var new_list = prompt('Plonk list (comma separated):', daPlonk.plonklist);

		if(typeof new_list != 'undefined' && new_list != null) {
			daPlonk.plonklist = new_list;
			GM_setValue('plonklist', daPlonk.plonklist);

			daPlonk.do_plonks();
		}
	}
};


daPlonk._init();

