// ==UserScript==
// @name            SpeedSim Online  - Espionage reports
// @namespace       http://websim.speedsim.net
// @description     Sends espionage reports to SpeedSim Online
// @author          Nicolas Höft
// @include         http://*ogame*
// ==/UserScript==

/*
Copyright (c) 2008 Nicolas Höft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

reports = new Array();

function str_erase(str, pos0, npos)
{
	// take out old string
	var newstr = str.substr(0, pos0);
	// put together again
	newstr += str.substr(pos0 + npos);
	return newstr;
}

// remove all tags
function cleanup_report(r)
{
	var p = 0, p2 = 0;
	while(p != -1)
	{
		if((p = r.indexOf("<")) != -1)
		{
			p2 = r.indexOf(">", p);
			if(p2 != -1)
				r = str_erase(r, p, p2 - p + 1);
			else
				r = str_erase(r, p, 1);
		}
		else if((p = r.indexOf("\n")) != -1)
		{
			r = str_erase(r, p, 1);
		}
	}
	return r;
}

// watch click events and get the button that was clicked
document.addEventListener('click', function(event) {
	var i = 0;
	for(i = 0; i < 20; i++)
	{
		if(event.target == document.getElementById("rep_btn" + i))
		{
			document.getElementById("input_sso").value = encodeURI(reports[i]);
			document.getElementById("speedsim_form").submit();
		}
	}
}, true);

if (document.URL.indexOf("/index.php?page=messages&") != -1)
{
	var i;
	// get all tables
	var spans = document.getElementsByTagName("span");
	var j = 0;
	// look for message table
	for(i = 0; i < spans.length; i++)
	{
		if(spans[i].className == "espionagereport")
		{
			var rep_table = spans[i].parentNode.parentNode.nextSibling;
			// opera needs extra handling
			if(!window.opera)
				rep_table = rep_table.nextSibling;
			var report = cleanup_report(rep_table.innerHTML);
			reports.push(report);
			// button to send the report
			var btn = document.createElement("input");
			btn.type = "button";
			btn.id = "rep_btn" + j;
			//btn.style.color = "orange";
			btn.value = "==> SpeedSim Online <==";
			//btn.style.fontWeight = "bold";
			// div around the button
			var div = document.createElement("div");
			div.align = "center";
			div.appendChild(btn);
			// put it beneath the report
			rep_table.getElementsByTagName("center")[0].parentNode.appendChild(div);
			j++;
		}
	}
	
	// create form to send this to speedsim online
	var el = document.createElement("form");
	el.innerHTML = '<input type="hidden" id="input_sso" name="report" />';
	el.method = "post";
	el.target = "_speedsim";
	el.action = "http://websim.speedsim.net/";
	el.id = el.name = "speedsim_form";
	document.getElementsByTagName("body")[0].appendChild(el);
}
