// ==UserScript==
// @name           WoD - Gruppe - Gegenstandsauswahl
// @namespace      world-of-dungeons.de
// @include        http*://*world-of-dungeons.*/wod/spiel/hero/items.php*
// ==/UserScript==

// Sprache definieren
var lang = 'de';
if(location.host.search('world-of-dungeons.net') != -1) {
	lang = 'en';
}

// Bestimme Knoten zum Einfügen des Buttons und füge Button ein
var allInputs = document.getElementsByTagName('input');
for (var j = 0; j < allInputs.length; j++) {
	var thisButton = allInputs[j];
	if(thisButton.getAttribute('name')=='checknone'){
		var newButton = document.createElement('input');
		newButton.setAttribute('class', 'button');
		newButton.setAttribute('type', 'button');
		if (lang == 'de') {
			newButton.setAttribute('value', 'Aufsplitten (GL/SK)');
		} else if (lang == 'en' ) {
			newButton.setAttribute('value', 'Split (GS/TV)');
		}
		newButton.setAttribute('id', 'switchItems'+j);
		newButton.setAttribute('name', 'switchItems'+j);
    newButton.addEventListener('click', switchItem, true);

    var newP = document.createElement('p');
    newP.appendChild(newButton);

    thisButton.parentNode.insertBefore(newP, thisButton.nextSibling);
	}
}

function switchItem(ev) {

	var found = false;
	var allSelects = document.getElementsByTagName('select');
	for (var i = 0; i < allSelects.length; i++) {
		thisSelect = allSelects[i];
    if(thisSelect.name.indexOf('EquipItem')!=-1
    		&& thisSelect.getElementsByTagName('option').length > 3
    		&& (thisSelect.options[thisSelect.selectedIndex].text == "Lager"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "Keller"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "Schatzkammer"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "Gruppenlager"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "storage"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "group storage"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "treasure vault"
    		|| thisSelect.options[thisSelect.selectedIndex].text == "cellar")
    	)
		{
			var aNodes = thisSelect.parentNode.parentNode.getElementsByTagName('a');
			var aNode = null;
			if (aNodes != null) {
				aNode = aNodes[0];

  			var Ausdruck = /[\w\W]*\(([0-9]*)\/([0-9]*)\)/;
  			var ergebnis = Ausdruck.exec(aNode.parentNode.innerHTML);
  			if ( ergebnis != null ) {
  				// alert(ergebnis[1] + ", " + ergebnis[2]);

  				for( var j = 0; j < thisSelect.options.length; j++)
  				{
  					if ( thisSelect.options[j].text == "Gruppenlager"
  					     || thisSelect.options[j].text == "group storage")
				    {
				    	thisSelect.selectedIndex = j;
				    }
  				}
  			}
  			else
  			{

  				for( var j = 0; j < thisSelect.options.length; j++)
  				{
  					if ( thisSelect.options[j].text == "Schatzkammer"
  					     || thisSelect.options[j].text == "treasure vault")
				    {
				    	thisSelect.selectedIndex = j;
				    }
  				}
  			}
			}

		}
	}
	return false;
}




