// ==UserScript==
// @name          Gmail Delete Button
// @namespace     http://www.arantius.com/article/arantius/gmail+delete+button/
// @description	  Add a "Delete" button to Gmail's interface.
// @include       http*://gmail.google.com/gmail?*
// @version       2.3
// ==/UserScript==

//
// Version 2.3:
//  Add/change code to track down/eliminate error conditions.
//  Display error when there are no selected messages to delete.
//  Include delete button in all labels and 'All Mail' section.
//
// 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
//

(function(){

window._gd_element=function(id) {
	try {
		var el=document.getElementById(id);
	} catch (e) {
		alert(
			"GMail Delete Button:\nThere was an error!\n\n"+
			"Line: "+e.lineNumber+"\n"+
			e.name+": "+e.message+"\n"
		);
		return false;
	}
	if (el) return el;
	return false;
}


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) {
		var box=_gd_element('nt1'); if (box) {
			//if we find the box put an error message in it
			box.firstChild.style.visibility='visible';
			box.getElementsByTagName('td')[1].innerHTML='Could not delete.  Make sure at least one conversation is selected.';
		}
		return;
	}

	//set the command index and fire the change event
	command_box.selectedIndex=delete_index;
	command_box.onchange();
}

function _gd_make_dom_button(id) {
	var delete_button= 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.style.color='#EE3311';
	delete_button.innerHTML='<b>Delete</b>';
	return delete_button;
}

function _gd_insert_button(insert_container, id) {
	if (!insert_container) return false;
	if (_gd_element('_gd_delete_button'+id)) {
		return false;
	}

	//get the elements
	var spacer, delete_button;
	delete_button=_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() {
	if (!window || ! document || ! document.body) return;
	var top_menu=_gd_element('tamu');  if (top_menu) _gd_insert_button(top_menu.parentNode, 0);
	var bot_menu=_gd_element('bamu');  if (bot_menu) _gd_insert_button(bot_menu.parentNode, 1);
	var mtp_menu=_gd_element('ctamu'); if (mtp_menu) _gd_insert_button(mtp_menu.parentNode, 2);
	var mbt_menu=_gd_element('cbamu'); if (mbt_menu) _gd_insert_button(mbt_menu.parentNode, 3);
}

if (document.location.search) {
	var s=document.location.search;
	dump('Load gmail page: '+s+'\n');
    if (s.match(/\bsearch=(inbox|query|cat|all)\b/) ||
        s.match(/view=cv/) && !s.match(/search=trash/)
    ) {
		dump('=== Apply Gmail Delete Button to: ===\n'+s+'\n');
		try{_gd_place_delete_buttons();}catch(e){alert(e.message);}
		setInterval('try{_gd_place_delete_buttons();}catch(e){alert(e.message);}', 750);
	}
}

})();
