// ==UserScript==
// @name DS Einlagerungen verfeinern
// @description Ersetzt den Link "Maximalmenge wählen" durch ein Menü zur genaueren Auswahl der Einlagerungen
// @author Michael Richter
// @namespace http://osor.de/
// @include http://de*.die-staemme.de/game.php?*screen=snob*
// ==/UserScript==


if(/screen=snob/.test(location.href) && /mode=(reserve|coin)/.test(location.href) && /(|&group=\d+)/.test(location.href)) {
	
	isCoin = (document.getElementById('coin_overview_table') ? true : false);

	function selectReserve() {
		if(!isCoin) {
			var chooser2 = document.getElementById('chooser' + (this.firstChild.id == 'chooser1' ? '2' : '1'));
			chooser2.removeEventListener('change', selectReserve, false);
			chooser2.selectedIndex = this.firstChild.selectedIndex;
			chooser2.addEventListener('change', selectReserve, false);
		}
		var value = this.firstChild.value;
		var selects = document.getElementsByName('villages')[0].getElementsByTagName('select');
		var sum = 0;
		var offset = isCoin ? 1 : 0;
		for(var i = 0; i < selects.length; i++) {
			if(selects[i].id == 'chooser1' || selects[i].id == 'chooser2') {
				continue;
			}
			if(value == 'none') {
			// Nichts
				if(isCoin)
					selects[i].options[0].selected = true;
				else
					selects[i].options[selects[i].options.length - 1].selected = true;
			} else if(value == 'all') {
			// Alles
				selects[i].options[selects[i].options.length - 2 + offset].selected = true;
			} else if(value.substr(value.length - 1, 1) == 'x') {
			// Relativ zum Speicher-Inhalt
			  if(26 - (selects[i].options.length - 1) < parseInt(value, 10)) {
			    selects[i].options[parseInt(value, 10) - (26 - (selects[i].options.length - 1)) - 1 + offset].selected = true;
				} else {
					if(isCoin)
						selects[i].options[0].selected = true;
					else
				  	selects[i].options[selects[i].options.length - 1].selected = true;
				}
			} else if(value > 0) {
			// Absolute Menge
				if(selects[i].options.length - 1 < value) {
					selects[i].options[selects[i].options.length - 2 + offset].selected = true;
				} else {
					selects[i].options[value - 1 + offset].selected = true;
				}
			} else if(value < 0) {
			// Absolut, abhängig vom Speicher
				if(selects[i].options.length - 2 < -value) {
					if(isCoin)
						selects[i].options[0].selected = true;
					else
						selects[i].options[selects[i].options.length - 1].selected = true;
				} else {
					selects[i].options[selects[i].options.length - 2 + offset + parseInt(value, 10)].selected = true;
				}
			}
			sum += parseInt(selects[i].value, 10);
		}
		document.getElementById('selectedBunches_top').innerHTML = sum;
		if(!isCoin) {
			document.getElementById('selectedBunches_bottom').innerHTML = sum;
		}
	}
	
	var chooser = document.evaluate('//form[@name="villages"]/table/tbody/tr/td[2]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	if(!isCoin) {
		var chooser2 = document.evaluate('//form[@name="villages"]/table/tbody/tr[last()]/td[2]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	}

	if(chooser && chooser.snapshotLength > 0) {
		chooser = chooser.snapshotItem(0);
		if(!isCoin) {
			chooser2 = chooser2.snapshotItem(0);
		}
		//chooser.removeChild(chooser.firstChild.nextSibling);
		chooser.innerHTML = '';
		if(!isCoin) {
			//chooser2.removeChild(chooser2.firstChild.nextSibling);
			chooser2.innerHTML = '';
		}
		var div = document.createElement('div');
		var html = '<select id="chooser1">' +
										'<option value="none">- nichts -</option>' +
										'<option value="all">- alles -</option>' +
										'<optgroup label="Absolute Menge">';
		for(var i = 1; i <= 26; i++) {
			html += '<option value="' + i + '">' + i + 'x</option>'
		}
		html += '</optgroup><optgroup label="Absolut, abhängig vom Speicher">';
		for(var i = 1; i <= 26; i++) {
			html += '<option value="' + i + 'x">' + i + 'x</option>'
		}
		html += '</optgroup><optgroup label="Relativ zum Speicher-Inhalt">';
		for(var i = 26; i >= 1; i--) {
			html += '<option value="-' + i + '">Max -' + i + 'x</option>'
		}
		html += '</optgroup>' +
										'</select>';
		div.innerHTML = html;
		chooser.appendChild(div);
		if(!isCoin) {
			var div2 = div.cloneNode(true);
			div2.firstChild.id = 'chooser2';
			chooser2.appendChild(div2);
			div2.addEventListener('change', selectReserve, false);
		}
		div.addEventListener('change', selectReserve, false);
	}
}

