// ==UserScript==
// @name           Tykinruoka's Auto-Fighter
// @namespace      http://gm.taistelumarsu.org
// @description    If you're not hurt, this script hits again
// @include        *.kingdomofloathing.com/*
// @include        kingdomofloathing.com/*
// ==/UserScript==


//jQuery-related part
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

function GM_wait() {
	if(typeof unsafeWindow.jQuery == 'undefined') {
		window.setTimeout(GM_wait,100);
	} else {
		$ = unsafeWindow.jQuery;
		if (hitMeBaby) hitMeBaby(0);
	}
}

//end of jQuery

function getSelectElem(name) {
	selects = document.getElementsByTagName('select');
	for (i = 0; i < selects.length; i++) {
		if (selects[i].name == name) {
			return selects[i];
		}
	}
	return null;
}

function getSubmitButton(value) {
	inputs = document.getElementsByTagName('input');
	for (i = 0; i < inputs.length; i++) {
		if (inputs[i].type == 'submit' && inputs[i].value == value) {
			return inputs[i];
		}
	}
	return null;
}

function hitMeBaby(tries) {
	if (tries > 10) {
		return false;
	}
	tries++;

	if (/^http:\/\/(www\d*\.)?kingdomofloathing\.com\/topmenu\.php/.test(document.location)) {
		//sorry for stealing :) why'd I reinvent the wheel...
		var tykinruoka_autofighter = document.createElement("span");
		with (tykinruoka_autofighter.style) {
			background = "white";
			position = "fixed";
			top = "3px";
			left = "5px";
			padding = "3px 5px 3px 5px";
			MozUserSelect = "none";
		}
		tykinruoka_autofighter.innerHTML = 'AutoFight<a href="javascript:void(0);" id="tykinruoka_aa_disable">&trade;</a>';

		selectElem = document.createElement("select");
		selectElem.id = 'tykinruoka_aa_select';
		noneOption = document.createElement("option");
		noneOption.value = 'none';
		noneOption.text = 'Disabled';
		selectElem.add(noneOption, null);
		weaponOption = document.createElement("option");
		weaponOption.value = 'weapon';
		weaponOption.text = "Weapon";
		selectElem.add(weaponOption, null);
		itemOption = document.createElement("option");
		itemOption.value = 'item';
		itemOption.text = "Item";
		selectElem.add(itemOption, null);
		skillOption = document.createElement("option");
		skillOption.value = 'skill';
		skillOption.text = "Skill";
		selectElem.add(skillOption, null);
		macroOption = document.createElement("option");
		macroOption.value = 'macro';
		macroOption.text = "Macro";
		selectElem.add(macroOption, null);
		tykinruoka_autofighter.innerHTML += '<br>';
		tykinruoka_autofighter.appendChild(selectElem);
		document.body.appendChild(tykinruoka_autofighter);
		
		$('#tykinruoka_aa_disable').css('text-decoration', 'none');
		$('#tykinruoka_aa_disable').click( function() {
			$('#tykinruoka_aa_select').val('none');
			return false;
		});
	} else {
		realWindow = window.wrappedJSObject;
		
		loc = (document.location).toString();
		if (realWindow.top.frames[0].document.getElementById('tykinruoka_aa_select').value == 'none') {
			return false;
		}
		if (loc.indexOf('fight.php') != -1 || loc.indexOf('adventure.php') != -1 || loc.indexOf('choice.php') != -1 || loc.indexOf('sewer.php') != -1) {
			if ($("html").html().indexOf('You lose') == -1 && $("html").html().indexOf("macro aborted") == -1 && $("html").html().indexOf('You run away') == -1 && $("html").html().indexOf('id="tack"') != -1) {
				switch (realWindow.top.frames[0].document.getElementById('tykinruoka_aa_select').value) {
					default:
					case 'none':
						return false;
					case 'weapon':
						$("#tack").click();
						break;
					case 'item':
						itemSelect = getSelectElem('whichitem');
						itemSelect2 = getSelectElem('whichitem2');
						if ((itemSelect != null && itemSelect.value != "0") ||
							(itemSelect2 != null && itemSelect2.value != "0")) {
							submitButton = getSubmitButton('Use Item');
							if (submitButton != null) {
								submitButton.click();
							} else {
								submitButton = getSubmitButton('Use Item(s)');
								if (submitButton != null) {
									submitButton.click();
								}
							}
						}
						break;
					case 'skill':
						skillSelect = getSelectElem('whichskill');
						if (skillSelect != null && skillSelect.value != "none") {
							submitButton = getSubmitButton('Use Skill');
							if (submitButton != null) {
								submitButton.click();
							}
						}
						break;
					case 'macro':
						macroSelect = getSelectElem('whichmacro');
						if (macroSelect != null && macroSelect.value != "0") {
							submitButton = getSubmitButton('Execute Macro');
							if (submitButton != null) {
								submitButton.click();
							}
						}
						break;
				}
				return false;
			}
			$("a").each( function() {
				if ($(this).html().indexOf("Adventure Again") != -1 ||
				$(this).html().indexOf("Eat another black pudding") != -1) {
					start = location.href;
					start = start.substring(0, start.lastIndexOf('/')+1);
					location.replace(start + $(this).attr("href"));
					return false;
				}
			});
			setTimeout("hitMeBaby(" + tries + ")",100);
			return false;
		}
	}
}

GM_wait();
