// ==UserScript==
// @author		luckyman, modified by tomasz.frelik (at) enzo.pl
// @namespace	http://frelo.enzo.pl/userscript
// @name		Travian: Resources Needed To Build (based on Travian: Resource)
// @description	On build pages, if not enough resources show how much is needed and how much time it will take to produce this quantity. --- luckyman's script with my few quick fixes.
// @include		http://s*.travian*/build.php?id=*
// ==/UserScript==

function format(maxtime){
	var hrs = Math.floor(maxtime/3600);
	var min = Math.floor(maxtime/60) % 60;
	var sec = maxtime % 60;
	var t = hrs + ":";
	if(min < 10){t += "0";}
	t += min + ":";
	if(sec < 10){t += "0";}
	t += sec;
	return t;
}

if ( navigator.appName == 'Opera' ) {
	eventSource = document;
} else {
	eventSource = window;
}

eventSource.addEventListener( 'load', function( e ) {
			
	var results = document.evaluate('//td[@id]/text()',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
	//alert(results.snapshotLength);
	if (results.snapshotLength == 0) {return;}
	
	var wood = parseInt(results.snapshotItem(0).data.split('/'));
	var clay = parseInt(results.snapshotItem(1).data.split('/'));
	var iron = parseInt(results.snapshotItem(2).data.split('/'));
	var crop = parseInt(results.snapshotItem(3).data.split('/'));
	var woodp = parseInt(results.snapshotItem(0).parentNode.title);
	var clayp = parseInt(results.snapshotItem(1).parentNode.title);
	var ironp = parseInt(results.snapshotItem(2).parentNode.title);
	var cropp = parseInt(results.snapshotItem(3).parentNode.title);
	
	//var need = document.evaluate('/html/body/table/tbody/tr/td[3]/table[last()]/tbody/tr//td/text()',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
	var need = document.evaluate('//table[@class="f10"]',document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
	//alert(need.snapshotLength);
	for (var i=0;i<need.snapshotLength;i++){
		//wood | clay | iron | crop | upkeep | time
		//alert(need.snapshotItem(i).textContent);
		if (need.snapshotItem(i).textContent.indexOf("|")==-1||need.snapshotItem(i).textContent.charAt(0)=="\n") continue;
		var needs = need.snapshotItem(i).textContent.split(" | ");
		//for (var j=0;j<needs.length;j++) alert(needs[j]);
		
		var woodneed = parseInt(needs[0]);
		var clayneed = parseInt(needs[1]);
		var ironneed = parseInt(needs[2]);
		var cropneed = parseInt(needs[3]);
		if (wood>=woodneed&&clay>=clayneed&&iron>=ironneed&&crop>=cropneed) continue;
		//alert("Not enough resources");
		
		var woodsn=0,claysn=0,ironsn=0,cropsn=0;
		if (wood<woodneed) woodsn=woodneed-wood;
		if (clay<clayneed) claysn=clayneed-clay;
		if (iron<ironneed) ironsn=ironneed-iron;
		if (crop<cropneed) cropsn=cropneed-crop;
		
		var maxtime=0,time,t;
		time = woodsn/woodp*3600;
		if (maxtime<time) maxtime=time;
		time = claysn/clayp*3600;
		if (maxtime<time) maxtime=time;
		time = ironsn/ironp*3600;
		if (maxtime<time) maxtime=time;
		time = cropsn/cropp*3600;
		if (maxtime<time) maxtime=time;
		maxtime=parseInt(maxtime);
		t=format(maxtime);
	
		// create presentation
		// in Firefox the total is in a new line, even though the div is floated
		var resource_div = document.createElement( 'div' );
		resource_div.style.cssFloat = "left";
		//resource_div.style.paddingRight = 4;
		resource_div.innerHTML = "Resources needed: <img src=\"img/un/r/1.gif\" width=\"18\" height=\"12\">"+woodsn
		                                             +" | <img src=\"img/un/r/2.gif\" width=\"18\" height=\"12\">"+claysn
		                                             +" | <img src=\"img/un/r/3.gif\" width=\"18\" height=\"12\">"+ironsn
		                                             +" | <img src=\"img/un/r/4.gif\" width=\"18\" height=\"12\">"+cropsn
		                                             +"<br>Enough resources in: <span id=timer0>"+t+"</span>";
		need.snapshotItem(i).appendChild(resource_div);
	}
},false);

