// ==UserScript==
// @name          Gmail Delete Button
// @namespace     http://www.arantius.com/article/arantius/gmail+delete+button/
// @description	  Add a "Delete" button next to Gmail's "Archive" button.
// @include       http*://gmail.google.com/gmail*
// @version       2.2
// ==/UserScript==

//
// Version 2.2:
//  Patched to work with GreaseMonkey 0.3.3
//
// Originally written by Anthony Lieuallen of http://www.arantius.com/
// Licensed for unlimited modification and redistribution as long as
// this notice is kept intact.
//
// If possible, please contact me regarding new features, bugfixes
// or changes that I could integrate into the existing code instead of
// creating a different script.  Thank you
//

window._gd_gmail_delete=function(delete_button){
	//find the command box
	var command_box=delete_button.parentNode.getElementsByTagName('select')[0];
	command_box.onfocus();

	//find the command index for 'move to trash
	var delete_index=-1;
	for (var i=0; i<command_box.options.length; i++) {
		if ('tr'==command_box.options[i].value && !command_box.options[i].disabled ) {
			delete_index=i;
			break;
		}
	}
	//don't try to continue if we can't move to trash now
	if (-1==delete_index) return;

	//set the command index and fire the change event
	command_box.selectedIndex=delete_index;
	command_box.onchange();
}

window._gd_element=function(id) {
	var el=window.document.getElementById(id);
	if (el) return el;
	return false;
}

window._gd_make_dom_button=function(id) {
	var delete_button=window.document.createElement('button');
	delete_button.setAttribute('class', 'ab');
	delete_button.setAttribute('id', '_gd_delete_button'+id);
	delete_button.setAttribute('onclick', '_gd_gmail_delete(this);');
	delete_button.innerHTML='<b>Delete</b>';
	return delete_button;
}

window._gd_insert_button=function(insert_container, id) {
	if (!insert_container) return false;
	if (window._gd_element('_gd_delete_button'+id)) {
		return false;
	}
	//alert('try '+insert_container);

	//get the elements
	var spacer, delete_button;
	delete_button=window._gd_make_dom_button(id);
	spacer=insert_container.firstChild.nextSibling.cloneNode(false);

	//pick the right place to put them
	var insert_point=insert_container.firstChild;  //this is default
	if (2==id || 3==id) {
		// 2 and 3 are inside the message and go at a different place
		insert_point=insert_point.nextSibling.nextSibling;
	}
	if (document.location.search.match(/search=query/)) {
		//inside the search page we go yet different places with different spacers
		if (0==id) {
			spacer=insert_container.firstChild.nextSibling.nextSibling.cloneNode(false);
			insert_point=insert_container.firstChild.nextSibling.nextSibling.nextSibling;
		}
		if (1==id) spacer=document.createElement('span'); //no space really needed here
	}

	//put them in
	insert_container.insertBefore(spacer, insert_point);
	insert_container.insertBefore(delete_button, spacer);
}

window._gd_place_delete_buttons=function() {
	var top_menu=window._gd_element('tamu');  if (top_menu) window._gd_insert_button(top_menu.parentNode, 0);
	var bot_menu=window._gd_element('bamu');  if (bot_menu) window._gd_insert_button(bot_menu.parentNode, 1);
	var mtp_menu=window._gd_element('ctamu'); if (mtp_menu) window._gd_insert_button(mtp_menu.parentNode, 2);
	var mbt_menu=window._gd_element('cbamu'); if (mbt_menu) window._gd_insert_button(mbt_menu.parentNode, 3);
}

if (document.location.search) {
	if (document.location.search.match(/search=inbox/)
		||
		document.location.search.match(/search=query/)
		||
		document.location.search.match(/view=cv/) && !document.location.search.match(/search=trash/)
	) {
		setInterval('try{window._gd_place_delete_buttons();}catch(e){}', 25);
	}
}
