// ==UserScript==
// @name           Tykinruoka's Display Case Script
// @namespace      http://trashf.taistelumarsu.org
// @description    Compares between your inventory and your display case
// @include        http://*.kingdomofloathing.com/managecollection.php
// ==/UserScript==
/*
Version history:
v0.01 Alpha version
- slow, looped through the display case every time
- browser stopped responding

v0.01 Beta version
- works pretty nice I think :)
- some ideas not yet implemented

v0.01 Initial release
- no longer pops up the question, just prints all kind of stuff without asking any questions
- added checkbox for hiding the information

v0.02
- fixed a bug occuring when case was empty
- fixed a calculations bug
- changed visibility -> display, which is much wiser when the list is long
- automatically inserts next 10 items to boxes

v0.03
- fixed to work with Firefox 4

TODO: 
- Check for script updates.

Feel free to develop further.

Questions? Feedback? Want to develop this PoS further? Please, contact Tykinruoka (#875019) a.k.a. TrashF @ QuakeNET, IRCnet & netgamers.

*/

//get all selects with name "whichitem1"
var selects = document.getElementsByName("whichitem1");

//first one of them is user inventory
var invselect = selects[0];

//and the second one is user's display case (if there is no 2nd select, case is empty)
var dispselect = false;
var dispitems = [];
if (selects.length > 1) {
	dispselect = selects[1];
}

if (dispselect != false) {
	//collect all display case item's item-id's here
	for (i = 0; i < dispselect.options.length; i++) {
		dispitems[dispitems.length] = dispselect.options[i].value;
	}

	//and sort them
	dispitems.sort(function(a,b){return a - b});
}

//then write something to output
var msg = "";
msg += "Unique items in inventory: " + parseInt(invselect.options.length-1) + "<br>";
if (dispselect != false) {
	msg += "Unique items in display case: " + dispselect.options.length + "<br><br>";
} else {
	msg += "Unique items in display case: 0<br><br>";
}
var notfound = 0;

function handleMissing(num) {
	addboxes = 1;
	if (num > 10) { 
		num = 10;
	}
	while (addboxes <= num) {
		if (addboxes > 1) {
			addDiv = document.getElementById('inv_item' + addboxes);
			if (addDiv.wrappedJSObject)
				addDiv = addDiv.wrappedJSObject;
			addDiv.innerHTML = '<b>Put: <input type="text" name="howmany' + addboxes + '" size="2"></b><select name="whichitem' + addboxes + '"></select>';
			addedSelect = document.getElementsByName('whichitem' + addboxes)[0];
			if (addedSelect.wrappedJSObject)
				addedSelect = addedSelect.wrappedJSObject;
			for (i = 0; i < invselect.options.length; i++) {
				newOption = document.createElement('option');
				newOption.text = invselect.options[i].text;
				newOption.value = invselect.options[i].value;
				try {
					addedSelect.add(newOption, null);
				} catch (ex) {
					addedSelect.add(newOption);
				}
			}
		}
		addboxes++;
	}
	notFoundLi = document.getElementsByClassName('tykinruoka-notfound');
	for (i = 0; i < notFoundLi.length; i++) {
		if (i > 10) break;
		howmany = 'howmany' + (parseInt(i)+1);
		document.getElementsByName(howmany)[0].value = '1';
		whichitem = 'whichitem' + (parseInt(i)+1);
		document.getElementsByName(whichitem)[0].value = notFoundLi[i].title;
	}
}

for (i = 0; i < invselect.options.length; i++) {
	if (invselect.options[i].value != "") {
		//looping through user inventory
		var found_in_disp = false;
		if (invselect.options[i].value > 0 && invselect.options[i].value != "") {
			var compare = parseInt(invselect.options[i].value);
			for (h = 0; h < dispitems.length; h++) {
				if (compare == dispitems[h]) {
					//this inventory item is already in display case
					found_in_disp = true;
					break;
				}
				if (compare < dispitems[h]) {
					//search has gone too far
					break;
				}
			}
			if (!found_in_disp) {
				if (notfound == 0) {
					msg += "Items in inventory that aren't in display case (TOTALNUM):<br><ul>";
				}
				notfound++;
				msg += "<li class='tykinruoka-notfound' id='notfound" + notfound + "' title='" + invselect.options[i].value + "'>" + invselect.options[i].text.substring(0,invselect.options[i].text.length-4) + "</li>";
			}
		}
	}
}
if (notfound == 0) {
	msg += "No matches.";
} else {
	msg += "</ul>";
	itemword = " items";
	if (notfound == 1) {
		itemword = " item";
	}
	msg = msg.replace("TOTALNUM", notfound+itemword);
}

//create new div, search a nice spot to add it to and append the created message to the newly made div
var div11 = document.getElementById("inv_item11");
var newdiv = document.createElement("div");
newdiv.style.marginTop = "10px";
newdiv.style.marginBottom = "10px";
newdiv.innerHTML = msg;
newdiv.id = "dispdiff";
checkboxdiv = document.createElement("div");
checkboxdiv.marginTop = "10px";
checkboxdiv.innerHTML = "<input type='checkbox' onchange='if (document.getElementById(\"dispdiff\").style.display == \"none\") { document.getElementById(\"dispdiff\").style.display=\"block\"; } else { document.getElementById(\"dispdiff\").style.display=\"none\";}'>Hide comparison between inventory and display case";
if (div11.wrappedJSObject)
	div11 = div11.wrappedJSObject;
if (checkboxdiv.wrappedJSObject)
	checkboxdiv = checkboxdiv.wrappedJSObject;
div11.appendChild(checkboxdiv);
checkboxdiv.appendChild(newdiv);

if (notfound > 0) {
	handleMissing(notfound);
}
