// ==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*://mail.google.com/*
// ==/UserScript==

//
// Version 3.1.1:
//  - Dutch translation.
// Version 3.1:
//  - Button was missing in 'sent mail', now fixed.
//  - Configuration via user script commands, position and color.
// Version 3.0.1:
//  - Ukranian translation.
// Version 3.0:
//  - Completely rebuilt, based on the injection technique designed for
//    my related script 'Gmail Action Links'
//
// --------------------------------------------------------------------
// Originally written by Anthony Lieuallen of http://www.arantius.com/
// Licensed for unlimited modification and redistribution as long as
// this notice is kept intact.
// When distributing this original script or any script based on this
// code on the web, a link to the homepage (URL in namespace above) is
// required.
// --------------------------------------------------------------------
//

(function() {
function _gdb_watch_click(e) {
	var el=e.target;
	if (!el) return;
	if (!el.id) return;
	if ('_gdb_'!=el.id) return;
	e.preventBubble();
	
	var action='tr';
	var cont=el;
	while (null!=cont && 'TABLE'!=cont.tagName) cont=cont.parentNode;
	if (!cont) return;
	
	var box=cont.getElementsByTagName('select')[0];
	box.onfocus();
	var action_index=-1;
	for (var i=0; i<box.options.length; i++) {
		if (action==box.options[i].value && !box.options[i].disabled ) {
			action_index=i;
			break;
		}
	}
	if (-1==action_index) return;
	box.selectedIndex=action_index;
	box.onchange();
}

if ('undefined'==typeof GM_registerMenuCommand) {
	//we're an extension, no GM apis
	function GM_getValue(a,b) {return b;}
} else {
	//we're a user script
	function _gdb_option_pos() {
		GM_setValue('gdb_pos',
			prompt(
				'Please enter the position you want.  Valid choices: left, right\n'+
				'NOTE: You will need to re-load GMail for this to take effect',
				GM_getValue('gdb_pos', 'left')
			)
		);
	}
	GM_registerMenuCommand(
		'Gmail Delete Button Position...',
		_gdb_option_pos
	);

	function _gdb_option_color() {
		GM_setValue('gdb_color',
			prompt(
				'Please enter the color, all HTML colors are valid\n(white, black, #DD3322, etc)\n'+
				'NOTE: You will need to re-load GMail for this to take effect',
				GM_getValue('gdb_color', 'black')
			)
		);
	}
	GM_registerMenuCommand(
		'Gmail Delete Button Color...',
		_gdb_option_color
	);
}

if (window.document.location.href.match(/name=js/)) {
	//find language
	var lang='';
	for (var i in unsafeWindow) {
		var x=unsafeWindow[i];
		if ('string'!=typeof x) continue;

		if (x.indexOf('help/intl/')==0) {
			lang=x.substr(10);
			lang=lang.substr(0,lang.length-1);
			break;
		}
	}

	//make button HTML
	var buttonText='Delete';
	switch (lang) {
	case 'it':    buttonText='Elimina'; break;
	case 'es':    buttonText='Borrar'; break;
	case 'fr':    buttonText='Supprimer'; break;
	case 'pt-BR': buttonText='Apaga'; break;
	case 'de':    buttonText='L&#246;schen'; break;
	case 'bg':    buttonText='&#1048;&#1079;&#1090;&#1088;&#1080;&#1081;'; break;
	case 'ru':    buttonText='&#1059;&#1076;&#1072;&#1083;&#1080;&#1090;&#1100;'; break;
	case 'pl':    buttonText='Usu&#324;'; break;
	case 'ja':    buttonText='\u30b4\u30df\u7bb1\u3078\u79fb\u52d5'; break;
	case 'hu':    buttonText='T&#246;r&#246;l'; break;
	case 'uk':    buttonText='&#1042;&#1080;&#1076;&#1072;&#1083;&#1080;&#1090;&#1080;'; break;
	case 'nl':    buttonText='Verwijderen'; break;
	}
	var button="<button id='_gdb_' class='ab' style='color: "+
		GM_getValue('gdb_color', 'black')+
		";'><b>"+buttonText+"</b></button>";
	var space='&nbsp;&nbsp;';

	//tweak variables to put button in
	for (var i in unsafeWindow) {
		try {
			var x=unsafeWindow[i];
			if ('object'!=typeof x) continue;

			if (0==x[0].indexOf('<table cellspacing')
				&&
				x[2].indexOf('class=lz')>0
			) {
				//list views
				if (x[4].indexOf('&nbsp;')>0) {
					if ('left'==GM_getValue('gdb_pos', 'left')) {
						unsafeWindow[i][2]+=button+space;
					} else {
						unsafeWindow[i][6]+=button+space;
					}
				} else {
					if ('left'==GM_getValue('gdb_pos', 'left')) {
						unsafeWindow[i][4]+=button+space;
					} else {
						unsafeWindow[i][8]=space+button+unsafeWindow[i][8];
					}
				}
			} else if (0==x[0].indexOf('<table width')
				&&
				x[2].indexOf('class=lz')>0
			) {
				//message view
				if ('left'==GM_getValue('gdb_pos', 'left')) {
					unsafeWindow[i][4]+=button+space;
				} else {
					unsafeWindow[i][6]=space+button+unsafeWindow[i][6];
				}
			}
		} catch (e) {
			//noop, just catch errors
		}
	}
} else {
	window.addEventListener('click', _gdb_watch_click, true);
}

})();
