// RetroFrog Skill Hiding Script
// 
// ==UserScript==
// @name           Skill Hiding Script
// @namespace      http://retrofrog.hardcoreoxygenation.com
// @include        *kingdomofloathing.com/*
// @include        *127.0.0.1:*/*
// @include        *kingdomofloathing.com/skills.php*
// @include        *kingdomofloathing.com/fight.php*
// @include        *127.0.0.1:600*/skills.php*
// @include        *127.0.0.1:600*/fight.php*
// @description    Version 0.4
// 
// ==/UserScript==


/********************************** Recent Changes **********************************************
Recent Updates:
	0.4	Added spectral snapper and chronic indigestion (thanks Ricket)
	0.3     Added the *butt skills (oops)
	0.2	First version!  Removes the skills you specify from your skill casting list, 
		in combat and on the skills page (optionally).
		A configuration menu 
			Tools->Greasemonkey->User Script Commands->RetroFrog's Skill Hiding Config
		lets you configure which skills should be hidden in combat and on the skills page.
		To un-hide skills in a list, just ctrl-click on the list (only affects that one
		list; go to the config menu if you want to make it permanent).
   
********************************************************************************************/
GM_setValue("scriptVer","0.4");
GM_setValue("scriptName","SkillHiding");
GM_setValue("scriptURL","http://retrofrog.hardcoreoxygenation.com/skill_hiding.user.js");

var HIDE_SKILLS_COMBAT_KEY = "RetroFrog_Skill_Hiding_Combat";
var HIDE_SKILLS_SKILLS_PAGE_KEY = "RetroFrog_Skill_Hiding_Skills_Page";
var HIDE_SKILLS_COMBAT_LIST_KEY = "RetroFrog_Skill_Hiding_Combat_List";
var HIDE_SKILLS_NONCOMBAT_LIST_KEY = "RetroFrog_Skill_Hiding_NonCombat_List";

function showSkills(evt) {
	if (!evt.ctrlKey) return;
	this.title = "All skills shown";
	reshowSkillsList(this);
	evt.preventDefault();
	return false;
}
function modifyAllSkillsLists(hideList, addChk) {
	if (document.body){ 
		var lists = document.getElementsByTagName('select');
		for (var i = lists.length - 1; i >= 0; i--) {	
			if (lists[i].name == 'whichskill') {	
				modifySkillsList(lists[i], hideList, addChk);
			}
		}
	}
}
function modifySkillsList(list, hideList, addChk) {
	var omitSkillsTxt = " " + GM_getValue(hideList, "") + " ";

	var hiddenCount = 0;		
	var options = list.options;
	for (j = options.length - 1; omitSkillsTxt.length > 2 && j >= 0; j--) {
		if (omitSkillsTxt.indexOf(" " + options[j].value + " ") >= 0) {
			options[j].style.display = "none";
			hiddenCount++;
		}
	}
	if (hiddenCount>0 && addChk) {
		list.title = "Ctrl-click to show hidden skills";
		list.addEventListener("mousedown", showSkills, false);
	}	
}
function reshowAllSkillsLists() {
	if (document.body){ 
		var lists = document.getElementsByTagName('select');
		for (var i = lists.length - 1; i >= 0; i--) {	
			if (lists[i].name == 'whichskill') {	
				reshowSkillsList(lists[i]);
			}
		}
	}
}
function reshowSkillsList(list) {		
	var options = list.options;
	for (j = options.length - 1; j >= 0; j--) {
		if (options[j].style.display == "none") {
			options[j].style.display = "";
		}
	}
}
function getSkillList() {
	var spells = new Array();

	//format:
	//  [spell number, "spell name", incombat, outofcombat]

	spells.push(["XXXX"]);

	spells.push(["2", "Chronic Indigestion", true, false]);
	spells.push(["3", "The Smile of Mr. A.", false, true]);
	spells.push(["15", "CLEESH", true, false]);
	spells.push(["16", "Summon Snowcone", false, true]);
	spells.push(["17", "Summon Hilarious Objects", false, true]);
	spells.push(["XXXX"]);

	spells.push(["1000", "Seal Clubbing Frenzy", false, true]);
	spells.push(["1007", "Tongue of the Otter", false, true]);
	spells.push(["1010", "Tongue of the Walrus", false, true]);
	spells.push(["1015", "Rage of the Reindeer", false, true]);
	spells.push(["1019", "Musk of the Moose", false, true]);
	spells.push(["1020", "Snarl of the Timberwolf", false, true]);
	spells.push(["XXXX"]);

	spells.push(["2000", "Patience of the Tortoise", false, true]);
	spells.push(["2003", "Headbutt", true, false]);
	spells.push(["2005", "Shieldbutt", true, false]);
	spells.push(["2007", "Ghostly Shell", false, true]);
	spells.push(["2008", "Reptilian Fortitude", false, true]);
	spells.push(["2009", "Empathy", false, true]);
	spells.push(["2010", "Tenacity of the Snapper", false, true]);
	spells.push(["2012", "Astral Shell", false, true]);
	spells.push(["2015", "Kneebutt", true, false]);
	spells.push(["2022", "Spectral Snapper", true, false]);
	spells.push(["2103", "Head + Knee Combo", true, false]);
	spells.push(["2105", "Head + Shield Combo", true, false]);
	spells.push(["2106", "Knee + Shield Combo", true, false]);
	spells.push(["2107", "Head + Knee + Shield Combo", true, false]);
	spells.push(["XXXX"]);

	spells.push(["3000", "Pasta Oneness", false, true]);
	spells.push(["3003", "Minor Ray of Something", true, false]);
	spells.push(["3004", "Entangling Noodles", true, false]);
	spells.push(["3005", "eXtreme Ray of Something", true, false]);
	spells.push(["3006", "Pastamancery", false, true]);
	spells.push(["3007", "Cone of Whatever", true, false]);
	spells.push(["3008", "Weapon of the Pastalord", true, false]);
	spells.push(["3009", "Lasagna Bandages", true, true]);
	spells.push(["3010", "Leash of Linguini", false, true]);
	spells.push(["3012", "Cannelloni Cocoon", false, true]);
	spells.push(["3015", "Springy Fusilli", false, true]);
	spells.push(["3019", "Fearful Fettucini", true, false]);
	spells.push(["3101", "Spirit of Cayenne", false, true]);
	spells.push(["3102", "Spirit of Peppermint", false, true]);
	spells.push(["3103", "Spirit of Garlic", false, true]);
	spells.push(["3104", "Spirit of Wormwood", false, true]);
	spells.push(["3105", "Spirit of Bacon Grease", false, true]);
	spells.push(["XXXX"]);

	spells.push(["4000", "Saucemastery", false, true]);
	spells.push(["4003", "Stream of Sauce", true, false]);
	spells.push(["4005", "Saucestorm", true, false]);
	spells.push(["4006", "Saucecrafting", false, true]);
	spells.push(["4007", "Elemental Saucesphere", false, true]);
	spells.push(["4008", "Jalapeņo Saucesphere", false, true]);
	spells.push(["4009", "Wave of Sauce", true, false]);
	spells.push(["4011", "Jabaņero Saucesphere", false, true]);
	spells.push(["4012", "Saucegeyser", true, false]);
	spells.push(["4014", "Saucy Salve", true, false]);
	spells.push(["4019", "Scarysauce", false, true]);
	spells.push(["XXXX"]);

	spells.push(["5000", "Disco State of Mind", false, true]);
	spells.push(["5003", "Disco Eye Poke", true, false]);
	spells.push(["5005", "Disco Dance of Doom", true, false]);
	spells.push(["5007", "Disco Nap", false, true]);
	spells.push(["5008", "Disco Dance II: Electric Boogaloo", true, false]);
	spells.push(["5011", "Disco Power Nap", false, true]);
	spells.push(["5012", "Disco Face Stab", true, false]);
	spells.push(["5014", "Advanced Cocktailcrafting", false, true]);
	spells.push(["5017", "Smooth Movements", false, true]);
	spells.push(["5019", "Tango of Terror", true, false]);
	spells.push(["XXXX"]);

	spells.push(["6000", "Mariachi Mood", false, true]);
	spells.push(["6003", "Aloysius' Antiphon of Aptitude", false, true]);
	spells.push(["6004", "Moxious Madrigal", false, true]);
	spells.push(["6005", "Cletus's Canticle of Celerity", false, true]);
	spells.push(["6006", "Polka of Plenty", false, true]);
	spells.push(["6007", "Magical Mojomuscular Melody", false, true]);
	spells.push(["6008", "Power Ballad of the Arrowsmith", false, true]);
	spells.push(["6009", "Brawnee's Anthem of Absorption", false, true]);
	spells.push(["6010", "Fat Leon's Phat Loot Lyric", false, true]);
	spells.push(["6011", "Psalm of Pointiness", false, true]);
	spells.push(["6012", "Jackasses' Symphony of Destruction", false, true]);
	spells.push(["6013", "Stevedave's Shanty of Superiority", false, true]);
	spells.push(["6014", "Ode to Booze", false, true]);
	spells.push(["6015", "The Sonata of Sneakiness", false, true]);
	spells.push(["6016", "Carlweather's Cantata of Confrontation", false, true]);
	spells.push(["6017", "Ur-Kel's Aria of Annoyance", false, true]);
	spells.push(["6018", "Dirge of Dreadfulness", false, true]);
	spells.push(["XXXX"]);

	spells.push(["7002", "Shake Hands", true, false]);
	spells.push(["7003", "Hot Breath", true, false]);
	spells.push(["7004", "Cold Breath", true, false]);
	spells.push(["7005", "Spooky Breath", true, false]);
	spells.push(["7006", "Stinky Breath", true, false]);
	spells.push(["7007", "Sleazy Breath", true, false]);
	spells.push(["7008", "Moxious Maneuver", true, false]);
	spells.push(["7009", "Magic Missile", true, false]);
	

	return spells;
}
function closeConfigPanel() {
	if (configPanel) {
		configPanel.close();
		configPanel = null;
		return true;
	} else {
		return false;
	}
}
var configPanel = null;
function storeSettings() {
	if (this.ownerDocument.body){ 
		var inputs = this.ownerDocument.getElementsByTagName('input');
		var noncombats = "";
		var combats = "";
		for (var i = inputs.length - 1; i >= 0; i--) {	
			if (inputs[i].type == 'checkbox' && inputs[i].checked) {
				if (inputs[i].id == 'combat') {	
					combats += inputs[i].name + " ";
				} else if (inputs[i].id == 'noncombat') {
					noncombats += inputs[i].name + " ";
				} 
			} 
		}		
		//alert("Combats: " + combats);
		//alert("Noncombats: " + noncombats);
		GM_setValue(HIDE_SKILLS_COMBAT_LIST_KEY, combats);
		GM_setValue(HIDE_SKILLS_NONCOMBAT_LIST_KEY, noncombats);
		alert("Settings saved and will take effect on next load of the skills/combat page");
		//this.ownerDocument.parentWindow.close();
		closeConfigPanel();
	} else {
		alert("Error saving settings.  (wth?)");
	}
}
function uncheck(doc, id) {
	var inputs = doc.getElementsByTagName('input');
	for (var i = inputs.length - 1; i >= 0; i--) {	
		if (inputs[i].type == 'checkbox' && inputs[i].id == id) {
			inputs[i].checked = false;
		}
	}
}
function uncheckCombat() {
	uncheck(this.ownerDocument, 'combat');
}
function uncheckNonCombat() {
	uncheck(this.ownerDocument, 'noncombat');
}
function displayConfigMenu(e) {
        configPanel = window.open("", "rfConfigPanel","width=750,height=550,scrollbars=yes,resizable=yes");

	var omitCombatTxt = " " + GM_getValue(HIDE_SKILLS_COMBAT_LIST_KEY, "") + " ";
	var omitNonCombatTxt = " " + GM_getValue(HIDE_SKILLS_NONCOMBAT_LIST_KEY, "") + " ";
	
        var html = '<h1>Skill Hiding Config</h1>';
	html += 'version ' + GM_getValue("scriptVer","?") + '<br/><br/>';

	var skills = getSkillList();

	html += "<table width='100%'>";
	for (var i = 0; i < skills.length; i++) {
		if (skills[i][0] == "XXXX") {
			html +=   "<tr><td colspan='5' height='30'></td></tr>";
			html +=   "<tr>";
			html +=     "<th align='right'>Skill&nbsp;&nbsp;</th>";
			html +=     "<th width='10'></th>";
			html +=     "<th>Hide in combat?</th>";
			html +=     "<th width='10'></th>";
			html +=     "<th>Hide on skills page?</th>";
			html +=   "</tr>";
		} else {
			html += "<tr>";
			html +=   "<td align='right'>&nbsp;&nbsp;" + skills[i][1] + "&nbsp;&nbsp;</td>";
			html +=   "<td/>";
			if (skills[i][2]) {
				html +=   "<td align='center'><input id='combat' name=" + skills[i][0] + " type='checkbox'";
				if (omitCombatTxt.indexOf(" " + skills[i][0] + " ") >= 0) {
					html += " checked";
				}
				html +=   "></td>";
			} else {
				html +=   "<td/>";
			}
			html +=   "<td/>";
			if (skills[i][3]) {
				html +=   "<td align='center'><input id='noncombat' name=" + skills[i][0] + " type='checkbox'";
				if (omitNonCombatTxt.indexOf(" " + skills[i][0] + " ") >= 0) {
					html += " checked";
				}
				html +=   "></td>";
			} else {
				html +=   "<td/>";
			}
			html += "</tr>";
		}
	}
	html +=   "<tr>"
	html +=     "<td colspan='5' align='center' valign='bottom' height='30'>";
	html +=       "<input type='button' id='combatClear' value='Uncheck all combat skills'>&nbsp;";
	html +=       "<input type='button' id='noncombatClear' value='Uncheck all noncombat skills'>&nbsp;";
	html +=       "<input type='button' id='save' value='Apply Changes and Close'>";
	html +=     "</td>";
	html +=   "</tr>";
	html += "</table>";


	html += "<br/><br/>";

	//configPanel.document.open("about:blank");
	configPanel.document.write(html);
	configPanel.document.close();

	configPanel.document.getElementById('save').addEventListener("click", storeSettings, false);
	configPanel.document.getElementById('combatClear').addEventListener("click", uncheckCombat, false);
	configPanel.document.getElementById('noncombatClear').addEventListener("click", uncheckNonCombat, false);
	configPanel.document.title = "RetroFrog's Skill Hiding Configuration";
}

GM_registerMenuCommand("RetroFrog's Skill Hiding Config", displayConfigMenu); 

if (window.location.pathname == "/fight.php") { 
	modifyAllSkillsLists(HIDE_SKILLS_COMBAT_LIST_KEY, true);
} else if (window.location.pathname == "/skills.php") { 
	//displayConfigMenu();
	modifyAllSkillsLists(HIDE_SKILLS_NONCOMBAT_LIST_KEY, true);
}

