// ==UserScript==
// @name          KoL MCD Dropdown
// @description   Changes the "Mind Control" readout in the charpane into a dropdown.
// @namespace     http://www.mrphlip.com/
// @include       http://kingdomofloathing.com/charpane.php*
// @include       http://www*.kingdomofloathing.com/charpane.php*
// @include       http://dev.kingdomofloathing.com/charpane.php*
// ==/UserScript==

var MCD, select, loadingImg, charname;

findMCD();

function findMCD()
{
	MCD = document.evaluate("//a[contains(@href,'canadia.php?place=machine')]/following-sibling::b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	
	if (!MCD)
	{
		// try looking compact-mode-style
		MCD = document.evaluate("//a[contains(@href,'canadia.php?place=machine')]/../..//b", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	}
	
	if (!MCD)
	{
		// either we don't have access to the MCD, or we do and it's set to 0...
		// don't check every time the charpane loads... use a cache
		
		// get the character name
		charname = document.evaluate("//a[contains(@href,'charsheet.php')]//text()", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
		// what level is this character?
		var level = document.evaluate("//text()[contains(.,'Level') or contains(.,'Lvl.')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
		a = level.match(/(\d+)/);
		if (!a)
			return;
		level = parseInt(a[1]);
		// get the cached data
		var cacheLevel = GM_getValue(charname + ".level", 123456789);
		var hasMCD = GM_getValue(charname + ".mcd", 0);
		GM_setValue(charname + ".level", level);
		if (cacheLevel > level)
		{
			// we've probably ascended since the script last ran...
			// check whether we have access to Little Canadia
			GM_xmlhttpRequest({
				method: "GET",
				url: "http://" + location.hostname + "/canadia.php",
				onload: checkMystSign
			});
		}
		else if (hasMCD)
			addMCD();
	}
	else
		replaceMCD()
}

function checkMystSign(results)
{
	if (results.responseText.indexOf("place=machine") >= 0)
	{
		GM_setValue(charname + ".mcd", 1)
		addMCD();
	}
	else
		GM_setValue(charname + ".mcd", 0)
}

function addMCD()
{
	// compact mode: add it directly under the "Adv:" row
	var adv = document.evaluate("//td[contains(.,'Adv:')]/ancestor::tr", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	if (adv)
	{
		// we're in compact mode
		// put it below "Ronin" if it's there
		var row = document.evaluate("//td[contains(.,'Ronin:') or contains(.,'Hardcore')]/ancestor::tr", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
		if (!row)
			row = adv;
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		td.style.textAlign = "right";
		var link = document.createElement('a');
		link.href = "canadia.php?place=machine";
		link.target = "mainpane";
		link.appendChild(document.createTextNode("MC"));
		td.appendChild(link);
		td.appendChild(document.createTextNode(":"));
		tr.appendChild(td);
		td = document.createElement('td');
		td.style.textAlign = "left";
		MCD = document.createElement('b');
		MCD.appendChild(document.createTextNode('0'));
		td.appendChild(MCD);
		tr.appendChild(td);
		row.parentNode.insertBefore(tr, row.nextSibling);
		replaceMCD();
	}
	else
	{
		// we're in full mode
		// add it directly under the table that has the adventures counter
		adv = document.evaluate("//img[contains(@src,'hourglass.gif')]/ancestor::table", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
		if (!adv)
			return;
		ronin = document.evaluate("//font[contains(.,'Ronin:') or contains(.,'Hardcore')]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
		var font = document.createElement('font');
		font.size = 2;
		var link = document.createElement('a');
		link.href = "canadia.php?place=machine";
		link.target = "mainpane";
		link.appendChild(document.createTextNode("Mind Control"));
		font.appendChild(link);
		font.appendChild(document.createTextNode(": "));
		MCD = document.createElement('b');
		MCD.appendChild(document.createTextNode('0'));
		font.appendChild(MCD);
		if (ronin)
		{
			ronin.parentNode.insertBefore(font, ronin.nextSibling);
			ronin.parentNode.insertBefore(document.createElement('br'), ronin.nextSibling);
			ronin.parentNode.insertBefore(document.createElement('br'), ronin.nextSibling);
		}
		else
		{
			adv.parentNode.insertBefore(document.createElement('br'), adv.nextSibling);
			adv.parentNode.insertBefore(font, adv.nextSibling);
			adv.parentNode.insertBefore(document.createElement('br'), adv.nextSibling);
		}
		replaceMCD();
	}
}

function replaceMCD()
{
	var sel = parseInt(MCD.firstChild.nodeValue);
	var form = document.createElement('form');
	select = document.createElement('select');
	for (var i = 0; i <= 11; i++)
	{
		var option = document.createElement('option');
		option.value = i;
		if (i == sel)
			option.selected = true;
		option.appendChild(document.createTextNode(i));
		select.appendChild(option);
	}
	select.addEventListener("change", changeMCD, false);
	form.appendChild(select);
	form.style.display = "inline";
	loadingImg = document.createElement('img');
	loadingImg.src = "data:image/gif;base64,R0lGODlhEgASAJECAMDAwNvb2%2F%2F%2F%2FwAAACH%2FC05FVFNDQVBFMi4wAwEAAAAh%2BQQFCgACACwAAAAAEgASAAACMpSPqQmw39o7IYjo6qpacpt8iKhoITiiG0qWnNGepjCv7u3WMfxqO0%2FrqVa1CdCIRBQAACH5BAUKAAIALAcAAQAIAAYAAAIOVCKZd2osAFhISmcnngUAIfkEBQoAAgAsCwADAAYACAAAAg5UInmnm4ZeAuBROq%2BtBQAh%2BQQFCgACACwLAAcABgAIAAACD5QTJojH2gQAak5jKdaiAAAh%2BQQFCgACACwHAAsACAAGAAACDpQdcZgKIFp4Lzq6RF0FACH5BAUKAAIALAMACwAIAAYAAAIOFCCZd2osQlhISmcnngUAIfkEBQoAAgAsAQAHAAYACAAAAg4UIHmnm4ZeCuFROq%2BtBQAh%2BQQFCgACACwBAAMABgAIAAACD5QBJojH2kQIak5jKdaiAAA7";
	loadingImg.style.display = "none";
	MCD.parentNode.replaceChild(loadingImg, MCD);
	loadingImg.parentNode.insertBefore(form, loadingImg);
}

function changeMCD(e)
{
	loadingImg.style.display = "inline";
	GM_xmlhttpRequest({
		method: "POST",
		url: "http://" + location.hostname + "/canadia.php",
		headers: {"Content-type": "application/x-www-form-urlencoded"},
		data: "action=changedial&whichlevel=" + select.value,
		onload: function(){ location.reload(); }
	});
}
