// Tard's Moon Helper (Adapted by CDMoyer)
// Original Copyright (c) 2006, Byung Kim
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// ==UserScript==
// @name           Tard's Moon Helper (CDM)
// @namespace      http://www.noblesse-oblige.org/cdmoyer/
// @include        *kingdomofloathing.com/topmenu.php*
// @include        *kingdomofloathing.com/compactmenu.php*
// @include        *127.0.0.1:*/topmenu.php*
// @include        *127.0.0.1:*/compactmenu.php*
// @exclude	       *forums.kingdomofloathing.com*
// @description    Version 1.0
// ==/UserScript==


if (window.location.pathname == "/compactmenu.php" || window.location.pathname == "/topmenu.php") {

	setConstants = function() {
		this.wW = window.innerWidth;
		this.wH = window.innerHeight;
		this.strB = '1px solid #aaa;';
		this.strB2 = '1px dashed #666;';
		this.bkg = 'background:#ececec;';
	}
	
	var c = new setConstants();

	var strCSS = '' +
		'body {margin:0px;padding:0px;font-family:Arial,Verdana,Helvetica;}' +
		'table{border:0px;margin:0px;padding:0px;font-size:10px;font-weight:normal;}' +
		'td,th {padding:2px;}' +
		'th {font-size:11px;font-weight:bold;text-align:left;}' +
		'table{border-collapse:collapse;}' +
		'.layerTable {padding:0px;border:1px solid #000;width:350px;'+ c.bkg +'font-size:10px;}' +
		'.layerTable table {width:350px;}' +
		'.layerTable table tr.tr0 {background:#fcfcfc;}' +
		'#moonInfo,#moonInfo table {width:300px;}' +
	'';

	strJS = "";

	
	// Moon Calendar
	strJS += 'setMoonCalendar = function() {' +
		// The following array indicates the brightness generated by either moon
		// based on the moon's image number.  So the first element is for moon0
		// (which does not exist-- it's a placeholder) and the next for moon1, etc.
		// For each element, there are 3 sub-elements.  The first is for the normal
		// moon graphic, say moon1.gif.  The second is for the a-version
		// (moon1a.gif) and the third for the b-version (moon1b.gif).  Take the
		// brightness for each moon and then add in the brightness for the
		// minimoon if it is not currently overlapping a major moon.
		'const moonBrightness = [[0,0,0],[0,1,1],[1,0,2],[2,1,3],[3,2,4],[4,3,3],[3,4,2],[2,3,1],[1,2,0]];' +
			
		// If the mini-moon graphic is not present, use index 0.  If it is, use the
		// appropriate element (minimoon1.gif = 1, minimoon2.gif =2)
		'const freestandingMiniMoonBrightness = [[0,0,0,0,0,0,0],[1,1,1,1,1,1,2],[0,0,0,0,0,0,0]];' +

		// Now for the grimacite units.  These are more complicated, but work
		// roughly the same way.  The key difference is that Grimace and
		// Ronald impact things differently.  Mostly you have one unit per
		// dark segment of grimace, with the minimoon counting as part of
		// grimace unless it is bright and obscuring a dark section of grimace.
		//                    Moon      0(fake) 1       2       3       4       5       6       7       8
		'const grimaciteUnitsRonald =  [[0,0,0],[0,0,0],[0,1,2],[0,1,0],[0,1,0],[0,1,1],[0,0,1],[0,0,1],[0,0,1]];' +
		'const grimaciteUnitsGrimace = [[0,0,0],[4,3,3],[3,4,2],[2,3,1],[1,2,0],[0,1,1],[1,0,2],[2,1,3],[3,2,4]];' +
		'const freestandingMiniMoonGrimaciteUnits = [[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[1,1,1,1,1,1,2]];' +

		// Certain moon configurations indicate stat days.  This is an easy decoder.
		// Decoder: [[Moon Combination], Musc_Day, Mys_Day, Mox_Day], ...
		'const statDays = [["1,1","Mox"],["5,3","Mys"],["1,5","Mus"],["2,5","Mus"],["5,7","Mys"],["8,8","Mox"]];' +

		//5-8 on the left and 2-5 on the right gives a bonus brightness
		//This array allows the pinpointing of the current moon formation, therefore allowing the following day's moon formation to be found.
		'const moonPositionFinder = {"1,1,2,1":"1","2b,1,0,0":"2","3,2a,0,0":"3","4,2,2,5":"4","5,3,1,4":"5","6,3,0,0":"6","7a,4,0,0":"7","8,4,1,6":"8","1,5b,0,0":"9","2,5,0,0":"10","3,6,2,2":"11","4,6,1,1":"12","5b,7,0,0":"13","6,7a,0,0":"14","7,8,1,5":"15","8,8,2,4":"16","1,1,0,0":"17","2a,1,0,0":"18","3,2,1,3":"19","4,2b,0,0":"20","5,3,0,0":"21","6,3,1,2":"22","7,4,2,1":"23","8b,4,0,0":"24","1,5a,0,0":"25","2,5,1,5":"26","3,6,2,4":"27","4,6,0,0":"28","5a,7,0,0":"29","6,7,1,3":"30","7,8b,0,0":"31","8,8,0,0":"32","1,1,2,2":"33","2,1,1,1":"34","3b,2,0,0":"35","4,2a,0,0":"36","5,3,2,5":"37","6,3,1,4":"38","7,4,0,0":"39","8a,4,0,0":"40","1,5,1,3":"41","2,5b,0,0":"42","3,6,0,0":"43","4,6,2,2":"44","5,7,1,1":"45","6b,7,0,0":"46","7,8a,0,0":"47","8,8,1,5":"48","1,1,2,4":"49","2,1,0,0":"50","3a,2,0,0":"51","4,2,1,3":"52","5,3b,0,0":"53","6,3,0,0":"54","7,4,1,2":"55","8,4,2,1":"56","1b,5,0,0":"57","2,5a,0,0":"58","3,6,1,5":"59","4,6,2,4":"60","5,7,0,0":"61","6a,7,0,0":"62","7,8,1,3":"63","8,8b,0,0":"64","1,1,0,0":"65","2,1,2,2":"66","3,2,1,1":"67","4b,2,0,0":"68","5,3a,0,0":"69","6,3,2,5":"70","7,4,1,4":"71","8,4,0,0":"72","1a,5,0,0":"73","2,5,1,3":"74","3,6b,0,0":"75","4,6,0,0":"76","5,7,1,2":"77","6,7,2,1":"78","7b,8,0,0":"79","8,8a,0,0":"80","1,1,2,5":"81","2,1,2,4":"82","3,2,0,0":"83","4a,2,0,0":"84","5,3,1,6":"85","6,3b,0,0":"86","7,4,0,0":"87","8,4,1,2":"88","1,5,2,1":"89","2b,5,0,0":"90","3,6a,0,0":"91","4,6,1,5":"92","5,7,2,4":"93","6,7,0,0":"94","7a,8,0,0":"95","8,8,1,3":"96","1,1b,0,0":"97","2,1,0,0":"98","3,2,2,2":"99","4,2,1,1":"100","5b,3,0,0":"101","6,3a,0,0":"102","7,4,2,5":"103","8,4,1,4":"104","1,5,0,0":"105","2a,5,0,0":"106","3,6,2,3":"107","4,6b,0,0":"108","5,7,0,0":"109","6,7,1,2":"110","7,8,2,1":"111","8b,8,0,0":"112","1,1a,0,0":"113","2,1,2,5":"114","3,2,1,4":"115","4,2,0,0":"116","5a,3,0,0":"117","6,3,1,6":"118","7,4b,0,0":"119","8,4,0,0":"120","1,5,2,2":"121","2,5,1,1":"122","3b,6,0,0":"123","4,6a,0,0":"124","5,7,1,5":"125","6,7,2,4":"126","7,8,0,0":"127","8a,8,0,0":"128","1,1,2,3":"129","2,1b,0,0":"130","3,2,0,0":"131","4,2,2,2":"132","5,3,1,1":"133","6b,3,0,0":"134","7,4a,0,0":"135","8,4,2,5":"136","1,5,1,4":"137","2,5,0,0":"138","3a,6,0,0":"139","4,6,2,3":"140","5,7b,0,0":"141","6,7,0,0":"142","7,8,1,2":"143","8,8,2,1":"144","1b,1,0,0":"145","2,1a,0,0":"146","3,2,2,5":"147","4,2,1,4":"148","5,3,0,0":"149","6a,3,0,0":"150","7,4,1,6":"151","8,4b,0,0":"152","1,5,0,0":"153","2,5,2,2":"154","3,6,1,1":"155","4b,6,0,0":"156","5,7a,0,0":"157","6,7,1,5":"158","7,8,2,4":"159","8,8,0,0":"160","1a,1,0,0":"161","2,1,2,3":"162","3,2b,0,0":"163","4,2,0,0":"164","5,3,1,2":"165","6,3,2,1":"166","7b,4,0,0":"167","8,4a,0,0":"168","1,5,1,5":"169","2,5,1,4":"170","3,6,0,0":"171","4a,6,0,0":"172","5,7,1,3":"173","6,7b,0,0":"174","7,8,0,0":"175","8,8,1,2":"176"};' +
		'const moonFutureDayPredictions = [["1,1,2,1"],["2b,1,0,0"],["3,2a,0,0"],["4,2,2,5"],["5,3,1,4"],["6,3,0,0"],["7a,4,0,0"],["8,4,1,6"],["1,5b,0,0"],["2,5,0,0"],["3,6,2,2"],["4,6,1,1"],["5b,7,0,0"],["6,7a,0,0"],["7,8,1,5"],["8,8,2,4"],["1,1,0,0"],["2a,1,0,0"],["3,2,1,3"],["4,2b,0,0"],["5,3,0,0"],["6,3,1,2"],["7,4,2,1"],["8b,4,0,0"],["1,5a,0,0"],["2,5,1,5"],["3,6,2,4"],["4,6,0,0"],["5a,7,0,0"],["6,7,1,3"],["7,8b,0,0"],["8,8,0,0"],["1,1,2,2"],["2,1,1,1"],["3b,2,0,0"],["4,2a,0,0"],["5,3,2,5"],["6,3,1,4"],["7,4,0,0"],["8a,4,0,0"],["1,5,1,3"],["2,5b,0,0"],["3,6,0,0"],["4,6,2,2"],["5,7,1,1"],["6b,7,0,0"],["7,8a,0,0"],["8,8,1,5"],["1,1,2,4"],["2,1,0,0"],["3a,2,0,0"],["4,2,1,3"],["5,3b,0,0"],["6,3,0,0"],["7,4,1,2"],["8,4,2,1"],["1b,5,0,0"],["2,5a,0,0"],["3,6,1,5"],["4,6,2,4"],["5,7,0,0"],["6a,7,0,0"],["7,8,1,3"],["8,8b,0,0"],["1,1,0,0"],["2,1,2,2"],["3,2,1,1"],["4b,2,0,0"],["5,3a,0,0"],["6,3,2,5"],["7,4,1,4"],["8,4,0,0"],["1a,5,0,0"],["2,5,1,3"],["3,6b,0,0"],["4,6,0,0"],["5,7,1,2"],["6,7,2,1"],["7b,8,0,0"],["8,8a,0,0"],["1,1,2,5"],["2,1,2,4"],["3,2,0,0"],["4a,2,0,0"],["5,3,1,6"],["6,3b,0,0"],["7,4,0,0"],["8,4,1,2"],["1,5,2,1"],["2b,5,0,0"],["3,6a,0,0"],["4,6,1,5"],["5,7,2,4"],["6,7,0,0"],["7a,8,0,0"],["8,8,1,3"],["1,1b,0,0"],["2,1,0,0"],["3,2,2,2"],["4,2,1,1"],["5b,3,0,0"],["6,3a,0,0"],["7,4,2,5"],["8,4,1,4"],["1,5,0,0"],["2a,5,0,0"],["3,6,2,3"],["4,6b,0,0"],["5,7,0,0"],["6,7,1,2"],["7,8,2,1"],["8b,8,0,0"],["1,1a,0,0"],["2,1,2,5"],["3,2,1,4"],["4,2,0,0"],["5a,3,0,0"],["6,3,1,6"],["7,4b,0,0"],["8,4,0,0"],["1,5,2,2"],["2,5,1,1"],["3b,6,0,0"],["4,6a,0,0"],["5,7,1,5"],["6,7,2,4"],["7,8,0,0"],["8a,8,0,0"],["1,1,2,3"],["2,1b,0,0"],["3,2,0,0"],["4,2,2,2"],["5,3,1,1"],["6b,3,0,0"],["7,4a,0,0"],["8,4,2,5"],["1,5,1,4"],["2,5,0,0"],["3a,6,0,0"],["4,6,2,3"],["5,7b,0,0"],["6,7,0,0"],["7,8,1,2"],["8,8,2,1"],["1b,1,0,0"],["2,1a,0,0"],["3,2,2,5"],["4,2,1,4"],["5,3,0,0"],["6a,3,0,0"],["7,4,1,6"],["8,4b,0,0"],["1,5,0,0"],["2,5,2,2"],["3,6,1,1"],["4b,6,0,0"],["5,7a,0,0"],["6,7,1,5"],["7,8,2,4"],["8,8,0,0"],["1a,1,0,0"],["2,1,2,3"],["3,2b,0,0"],["4,2,0,0"],["5,3,1,2"],["6,3,2,1"],["7b,4,0,0"],["8,4a,0,0"],["1,5,1,5"],["2,5,1,4"],["3,6,0,0"],["4a,6,0,0"],["5,7,1,3"],["6,7b,0,0"],["7,8,0,0"],["8,8,1,2"]];' +

		'var tmp = [];' +
		'tmp.push("<table><thead><tr><th>Day</th><th>Stat</th><th>Grue</th><th>Wereseal</th><th>Baio</th><th>Jekyllin</th><th>Grimacite</th></tr></thead><tbody>");' +
		'var m1 = 0;' + // Moon 1 Brightness (Ronald)
		'var m2 = 0;' + // Moon 2 Brightness (Grimace)
		'var m3 = 0;' + // Moon 3 Brightness (Hamburgler color)
		'var m4 = 0;' + // Hamburgler exact Position (1-5) (6 = moon is in the middle and worth double)
		'var moons = document.getElementsByTagName("body")[0].innerHTML.match(/\\/s?moon\\d\\w*/gi);' +
		'var miniMoon = document.getElementsByTagName("body")[0].innerHTML.match(/s?minimoon\\d*/gi);' +
		'var allMoons = document.getElementsByTagName("body")[0].innerHTML.match(/m?i?n?i?moon\\d*\\w*/gi);' +
		'var m1 = moons[0].match(/\\d\\w?/);' +
		'var m2 = moons[1].match(/\\d\\w?/);' +
		'if (miniMoon && (miniMoon.length > 0)) {' +
		  'if (miniMoon[0].indexOf("2") > -1) { m3 = "2"; }' +
		  'else { m3 = "1";}' +
		'}' +
		'var n1 = moons[0].match(/\\d/);' +
		'var n2 = moons[1].match(/\\d/);' +

		// This checks to see whether or not we are in compact mode (Compact mode has an extra word "Moons" which we need to get rid of by offsetting everything else.)
		'var m = allMoons[0].indexOf("M") == -1 ? 1 : 0;' + 
		
		// Determine the exact position of the minimoon
		//'alert(menupanedocument.innerHTML.match(/width=14 align=\\d/));' +
		'if (allMoons[1-m].indexOf("mini") != -1) { m4 = 1; }' +
		'var td = document.getElementsByTagName("td")[6].align;' +
		'if (allMoons[2-m].indexOf("mini") != -1 && td == "left" ) { m4 = 2;}' +
		'if (allMoons[2-m].indexOf("mini") != -1 && td == "center") { m4 = 3;}' +
		'if (allMoons[2-m].indexOf("mini") != -1 && td == "right") { m4 = 4;}' +
		'if (allMoons[3-m] && allMoons[2].length > 0) {' +
			'if (allMoons[3-m].indexOf("mini") != -1) { m4 = 5; }' +
		'}' +
		'if (m4 == 3 && n1 >= 5 && n1 <= 8 && n2 >= 2 && n2 <= 5) { m4 = 6;}' +
		
		'var now = new Date();' +
		'var then = new Date(2006,5,1);' + //for some stupid reason, month is 0-11, but day is 1-31 - so this is June 1st, 2006
		'var days = (Math.round((now - then) / 86400000) - 3) > 174 ? days = days/2 : days = (Math.round((now - then) / 86400000) - 3);' + // 86,400,000 milliseconds in a day
		'if(days == 174) {' +
			'days = -2;' +
		'}' +
		
		// Find which part of the moon cycle we are currently on (if we are far in the moon phase this can take awhile)
		//'alert(m1 +"," + m2 + "," + m3 + "," + m4);' +
		'if(moonPositionFinder[m1 +"," + m2 + "," + m3 + "," + m4] != undefined) {' +
			'var moonPosition = moonPositionFinder[m1 +"," + m2 + "," + m3 + "," + m4];' +
		'}' +
		
		//run a for loop to generate the next 14 days of moon data'
		'for (var n=0;n<=14;n++) {' +
			'var thismoon = String(moonFutureDayPredictions[moonPosition-1]);' +
			//'alert(thismoon + ":" + moonPosition);' +
			'thismoon = thismoon.split(",");' +
			'var o1 = thismoon[0].length > 1 ? thismoon[0].match(/\\d/) : thismoon[0];' +
			'var o1ModifierIndex = 0;' +
			'if (thismoon[0].indexOf("a") != -1) { o1ModifierIndex = 1; }' +
			'if (thismoon[0].indexOf("b") != -1) { o1ModifierIndex = 2; }' +
			
			'var o2 = thismoon[1].length > 1 ? thismoon[1].match(/\\d/) : thismoon[1];' +
			'var o2ModifierIndex = 0;' +
			'if (thismoon[1].indexOf("a") != -1) { o2ModifierIndex = 1; }' +
			'if (thismoon[1].indexOf("b") != -1) { o2ModifierIndex = 2; }' +
			
			'var o3 = thismoon[2];' +
			'var o4 = thismoon[3];' +
		
			'var curMoonBrightness = moonBrightness[o1] [o1ModifierIndex] + moonBrightness[o2] [o2ModifierIndex] + freestandingMiniMoonBrightness[o3] [o4];' +
			'var curGrimaciteUnits = grimaciteUnitsRonald[o1] [o1ModifierIndex] + grimaciteUnitsGrimace[o2] [o2ModifierIndex] + freestandingMiniMoonGrimaciteUnits[o3] [o4];' +
			
			'var today = new Date().valueOf();' +
			'var t = new Date(today+n*86400000);' +
			'var m = t.getMonth()+1;' +
			'var d = t.getDate();' +
			
			// Check whether it is currently a stat day and generate the result in a variable "curStatDay"
			'var curStatDay = "";' +
			'for (i in statDays) {' +
			  'if (statDays[i] [0] == o1+","+o2) {' +
				'curStatDay = statDays[i] [1] ;' +
				'break;' +
			  '}' +
			'}' +
			'tmp.push("<tr class=tr" + (n % 2) + "><td>" + (n == 0 ? "Today" : m + "/" + d));' +
			'tmp.push("</td><td>" + (curStatDay));' +
			'tmp.push("</td><td>" + (curMoonBrightness <= 5 ? "Yes" : "No"));' +
			'tmp.push("</td><td>" + (curMoonBrightness >= 4 ? "+" : "") + (curMoonBrightness - 4)*25 + "%");' +
			'tmp.push("</td><td>" + (curMoonBrightness*10) + "%");' +
			'tmp.push("</td><td>" + (9-curMoonBrightness) + " / " + (15 + 5*curMoonBrightness) + "%");' +
			'tmp.push("</td><td>" + "+" + (10*curGrimaciteUnits) + "%");' +
			'tmp.push("</td></tr>");' +
			'moonPosition = moonPosition < 176 ? Number(moonPosition) + 1 : 1;' +
		'}' +
		'tmp.push("</tbody></table>");' +
		'var strMoon = tmp.join("");' +
		'var moonImgs = document.getElementsByTagName("img");' +
		'for (var i in moonImgs) {' +
			'var moonImg = moonImgs[i];' +
			'if (moonImg.src && moonImg.src.indexOf("moon") != -1) {' +
				'moonImg.style.cursor = "pointer";' +
				//'moonImg.onclick = function() {top.document.getElementById("moonInfo").style.display="block";};' +
				'moonImg.onclick = function() {n=window.open(null, "moons", "height=300,width=350,toolbar=no,menubar=no,resizable=no,scrollbars=no,titlebar=no,status=no", true); n.document.write("<html><head><title>KoL Moons</title><script language=\\"javascript\\">document.onkeydown= function(ev){ window.close();}</script><style type=\\"text/css\\">'+strCSS+'</style></head><body><span style=\\"color: black; font-weight: bold;\\">Press Any Key to Close</span><div class=\\"layerTable\\">"+strMoon+"</div></body></html>"); n.document.close()};'+
				//'moonImg.onmouseout = function() {top.document.getElementById("moonInfo").style.display="none";};' +
			'}' +
		'}' +
	'};';
	

	// Frame onload functions
	strJS += '' +
		'setMoonCalendar();'+
	'';
	
	setTimeout(strJS,1000);
} 
