// ==UserScript==
// @name           Tykinruoka's Missing Discoveries Script
// @namespace      http://gm.taistelumarsu.org/
// @description    Shows missing recipes
// @include        http://www*.kingdomofloathing.com/craft.php?mode=discoveries*
// @include        http://kingdomofloathing.com/craft.php?mode=discoveries*
// ==/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; findMehItems(false);
	}
}
//end of jQuery

function get(url, cb, mode) {
	GM_xmlhttpRequest({
		method: "GET",
		url: url,
		onload: function(xhr) { cb(mode, xhr.responseText, true); }
	});
}

//define recipes
var recipes = [];
var totalModeRecipes = 0;

function getRecipes(mode) {
	update = false;
	if (GM_getValue("kol_nextupdate_" + mode) == undefined) {
		update = true;
	} else {
		var now = new Date();
		var nextupdate = new Date();
		nextupdate = Date.parse(GM_getValue("kol_nextupdate_" + mode));
		if (now > nextupdate) {
		       update = true;
		}
	}
 	if (GM_getValue("kol_" + mode) == undefined) {
		update = true;
	}
	if (update) {
		harvestData(mode);
	} else {
		setData(mode, GM_getValue("kol_" + mode), false);
	}
}

function setData(mode, data, saveGM) {
	arr = data.split(';');
	recipes[mode] = arr;
	if (saveGM) {
		GM_setValue("kol_" + mode,data);
		var upDate=new Date();
		upDate.setDate(upDate.getDate()+5);
		GM_setValue("kol_nextupdate_" + mode,upDate.toString());
	}
}

function harvestData(mode) {
	get("http://gm.taistelumarsu.org/get" + mode + ".php", setData, mode);
}

function found(mode,item) {
	newarr = [];
	for (i = 0; i < recipes[mode].length; i++) {
		if (recipes[mode][i].toLowerCase() != item.toLowerCase()) {
			newarr[newarr.length] = recipes[mode][i];
		}
	}
	recipes[mode] = newarr;
}

getRecipes("meatpaste");
getRecipes("food");
getRecipes("smith");
getRecipes("booze");
getRecipes("jewelry");
getRecipes("misc");

function findMehItems() {
	mode = false;
	$("table").each( function() {
		if ($(this).attr("id")) {
			//only tables with ID's are interesting
			if (!mode) {
				tableid = $(this).attr("id");
				if (tableid.indexOf('table_combine') != -1) {
					mode = "meatpaste";
				}
				if (tableid.indexOf('table_cook') != -1) {
					mode = "food";
				}
				if (tableid.indexOf('table_smith') != -1) {
					mode = "smith";
				}
				if (tableid.indexOf('table_cocktail') != -1) {
					mode = "booze";
				}
				if (tableid.indexOf('table_jewelry') != -1) {
					mode = "jewelry";
				}
				if (tableid.indexOf('table_multi') != -1) {
					mode = "misc";
				}
			}
			if (typeof(recipes[mode]) == "undefined") {
				setTimeout(findMehItems,100);
				return false;
			}
			if (totalModeRecipes == 0) {
				totalModeRecipes = recipes[mode].length;
			}
			$(this).find("b").each( function() {
				item = $(this).html();
				found(mode,item);
			});
			last_table = this;
		}
	});
	if (typeof(recipes[mode]) == "undefined") {
		return false;
	}
	$(last_table).after('<br><div id="missing_recipes"></div>');
	$("#missing_recipes").html('<center><u><b>Missing recipes</b></u>&nbsp;(<a href="#" id="tykinruokahelp">?</a>)</center><table style="width: 100%;" id="missing_recipestable"></table>');
	$("#tykinruokahelp").click( function() {
		alert("Recipe information is updated to your computer once a week. If these recipes are out of date and you want to update without waiting, just type 'about:config' as URL and filter by 'kol_'. Click the correct recipe category with right mouse button and select 'Reset to default' from submenu. Sorry about this complex method, please someone fix it for me :|\n\n-Tykinruoka (#875019)");
		return false;
	});
	if (recipes[mode].length > 0) {
		$("#missing_recipestable").append('<tr><td colspan="2">You have ' + recipes[mode].length + '/' + totalModeRecipes + ' missing recipes.</td></tr>');
		dmode = "left";
		for (i = 0; i < recipes[mode].length; i++) {
			if (dmode == "left") {
				$("#missing_recipestable").append('<tr><td><a target="_BLANK" href="http://kol.coldfront.net/thekolwiki/index.php/' + recipes[mode][i].replace(' ','_') + '">' + recipes[mode][i] + '</a></td></tr>');
				dmode = "right";
			} else {
				$("#missing_recipestable tr:last").append('<td><a target="_BLANK" href="http://kol.coldfront.net/thekolwiki/index.php/' + recipes[mode][i].replace(' ','_') + '">' + recipes[mode][i] + '</a></td>');
				dmode = "left";
			}
		}
		if (dmode == "right") {
			$("#missing_recipestable tr:last").append('<td></td>');
		}
	} else {
		$("#missing_recipestable").append('<tr><td>You have no missing recipes.</td></tr>');
	}
}

GM_wait();
