// ==UserScript==
// @name		Sellaband mailbox tweaker
// @namespace	http://www.radiusrock.com/greasemonkey/
// @description	Script to tweak the mailbox on Sellaband.com
// @include		http://www.sellaband.com/member/mailbox.html*
// ==/UserScript==


// change this to false if you don't want the confirmation popup
var confirmationRequired = true;
//

var box = null;
function checkMailBoxes() {
	if (confirmationRequired && confirm("Are you sure?")) {
		var count = 0;
		for (i = 20; i >= 0; i--) {
			var el = document.getElementById("check_" + i);
			if (el != null && el.getAttribute("checked") != null) {
				document.getElementById("deleteTarget"+i).src=el.getAttribute("value");
				var row = box.getElementsByTagName("tr")[i+1].parentNode;
				row.removeChild(box.getElementsByTagName("tr")[i+1]);
				row.removeChild(box.getElementsByTagName("tr")[i]);
				count++;
			}
		}
		if (count > 0) {
			window.setTimeout(function(){document.location.reload();},1000 * count);
			with (document.getElementById("loading_mailbox_progressbar").style) {
				display='block';
				visibility='visible';
			}
		}
	}
}
function clickMe(event) {
	if (event.target.getAttribute("checked") != null)
		event.target.removeAttribute("checked");
	else
		event.target.setAttribute("checked", "checked");
}
function getBox() {
	for (i=0;i<10;i++) {
		try {
			if(document.getElementById('main').getElementsByTagName('table')[i].className == 'mail_box') 
				box = document.getElementById('main').getElementsByTagName('div')[i];
			}
		catch(e) {}
	}
}
window.setTimeout(function() {
	getBox();
	if (box != null) {
		var rows = box.getElementsByTagName("tr");
		for (i = 0; i < rows.length; i++) {
			if (rows[i].parentNode.parentNode.className == 'mail_box') {
				if (rows[i].firstChild.className == "accordion_cell")
					rows[i].firstChild.setAttribute("colspan", "6");
				else {
					var td = document.createElement("td");
					td.setAttribute("style", "width: 20px");
					var input = document.createElement("input");
					input.setAttribute("type","checkbox");
					input.setAttribute("id", "check_" + i);
					input.setAttribute("value", rows[i].lastChild.firstChild.getAttribute("href"));
					td.appendChild(input);
					rows[i].insertBefore(td, rows[i].firstChild);
				}
			}
		}
		var button = document.createElement("input");
		button.setAttribute("value", "Delete checked");
		button.setAttribute("type", "reset");
		button.setAttribute("id", "deletebutton");
		document.getElementsByTagName("form")[0].parentNode.insertBefore(button, document.getElementsByTagName("form")[0].previousSibling);
		var img = null;
		for (i=0; i < 20; i = i + 2) {
			img = document.createElement("img");
			img.setAttribute("style", "width:0px;height:0px;display:none;visibility:hidden");
			img.setAttribute("id", "deleteTarget" + i);
			document.getElementsByTagName("form")[0].appendChild(img);
		}
		document.getElementById('deletebutton').addEventListener('click', checkMailBoxes, false);
		for (i = 0; i < 20; i++) {
			if (document.getElementById("check_" + i) != null)
				document.getElementById("check_" + i).addEventListener('click', clickMe, false);
		}
	}
}, 150);
