// ==UserScript==
// @name          Shartak - Repeatable Search
// @namespace     http://www.shartak.com
// @description	  Extension for repeating searches
// @include       http://www.shartak.com/game.cgi
// ==/UserScript==
// Version 0.2

var searchButton;
var timesToSearch = 10;

if (!GM_setValue) {
    alert('Please upgrade to the latest version of Greasemonkey.');
    return;
}

searchButton = document.evaluate(
    "//input[@value='search']",
    document,
    null,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    null);

var thisButton;
for (var i = 0; i < searchButton.snapshotLength; i++) {
	thisButton = searchButton.snapshotItem(i);
}

if (thisButton) {
	newform = document.createElement('form');
	newform.innerHTML = thisButton.parentNode.innerHTML;
    newform.setAttribute("method", "POST");
    newform.setAttribute("class", "formblock");
    newform.setAttribute("action", "/game.cgi");
    newform.setAttribute("id", "gm_search_10");
    newform.innerHTML = newform.innerHTML.replace("Search Area", "x " + timesToSearch);
    thisButton.parentNode.parentNode.insertBefore(newform,thisButton.parentNode.nextSibling);

	if (GM_getValue("shartak_repeat_search") > 0) {
		GM_setValue("shartak_repeat_search", GM_getValue("shartak_repeat_search") - 1);
//		alert(GM_getValue("shartak_repeat_search") + " searches left");

		var infoBox = findInfoNode();
		if (GM_getValue("shartak_repeat_result")) {
			GM_setValue("shartak_repeat_result", GM_getValue("shartak_repeat_result") + "\n" + infoBox.innerHTML);
		} else {
			GM_setValue("shartak_repeat_result", infoBox.innerHTML);
		}

		if (GM_getValue("shartak_repeat_search") > 0) {
			document.getElementById("gm_search_10").submit();
		} else {
			if (GM_getValue("shartak_repeat_result")) {
				outputResults();
			}
			// If you click the multi-search button, you get to repeat the search.
			document.getElementById("gm_search_10").addEventListener('click', function(event) {
				GM_setValue("shartak_repeat_search", timesToSearch);
			}, true);
		}
	} else {
		if (GM_getValue("shartak_repeat_result")) {
			outputResults();
		}
		// If you click the multi-search button, you get to repeat the search.
		document.getElementById("gm_search_10").addEventListener('click', function(event) {
			GM_setValue("shartak_repeat_search", timesToSearch);
   		}, true);
	}
} else {
// Search button not found so reset counter to 0.
	if (GM_getValue("shartak_repeat_result")) {
		var infoBox = findInfoNode();
		if (GM_getValue("shartak_repeat_result")) {
			GM_setValue("shartak_repeat_result", GM_getValue("shartak_repeat_result") + "\n" + infoBox.innerHTML);
		} else {
			GM_setValue("shartak_repeat_result", infoBox.innerHTML);
		}
		outputResults();
	}
	GM_setValue("shartak_repeat_search", 0);
}

function outputResults() {
	var lastResults = GM_getValue("shartak_repeat_result");
	GM_setValue("shartak_repeat_result","");
	var infoBox = findInfoNode();
	var invMarker = findInventoryMarker();
	var txtNode = document.createElement("p");
	txtNode.setAttribute("class", "info");
	var lastResultsHtml = lastResults.replace(/\n/g, "<br>");
	txtNode.innerHTML = lastResultsHtml;
	if (infoBox) {
		infoBox.parentNode.replaceChild(txtNode,infoBox);
	} else if (invMarker) {
		invMarker.parentNode.insertBefore(txtNode,invMarker)
	} else {
		alert(lastResults);
	}
}

function findInventoryMarker() {
	var h3Markers = document.evaluate(
		"//h3",
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null);
	for (var i = 0; i < h3Markers.snapshotLength; i++) {
		var html = h3Markers.snapshotItem(i).innerHTML;
		if (/Inventory/.test(html)) {
			return h3Markers.snapshotItem(i);
		}
	}
}

function findInfoNode() {
	var infoBoxes = document.evaluate(
		"//p[@class='info']",
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null);
	if (infoBoxes.snapshotLength == 1) {
		return infoBoxes.snapshotItem(0);
	}
}

