// ==UserScript==
// @name           Ogame2WR
// @namespace      ogame.fr
// @description    Adds links to war-riders in statistics (http://ogame.spiderou.net)
// @include        http://uni*.ogame.fr/game/index.php?page=statistics&*
// ==/UserScript==

var url = document.location+'' ; // TransTyping
var cut = url.split('\/') ;
var host = cut[2] ;
var cut = host.split('\.') ;
var uni = cut[0].substr(3) ;
var lang = cut[2] ;

var mytables = xpath('//TABLE') ;
var mytable = mytables.snapshotItem(mytables.snapshotLength-1) ;
var nbcols = mytable.rows[0].cells.length ;

// First line : title
if ( nbcols == 5 ) { // Players' statistics
	var myrow = mytable.rows[0] ;
	var mycell = myrow.cells[2].cloneNode(true)
	myrow.insertBefore(mycell,myrow.cells[2]) ;
	var mycell = myrow.cells[2].cloneNode(true)
	myrow.insertBefore(mycell,myrow.cells[5]) ;
}

// Other lines
for (var i = 1 ; i < mytable.rows.length ; i++ ) {
	var myrow = mytable.rows[i] ;
	if ( nbcols == 5 ) { // Players' statistics
		var myplayernick = strip(myrow.cells[1].childNodes[1].firstChild.nodeValue) ;
		var myplayerally = strip(myrow.cells[3].childNodes[1].firstChild.nodeValue) ;
		myrow.insertBefore(wrlinkth(uni,'player',myplayernick,lang),myrow.cells[2]) ; // Player
		myrow.insertBefore(wrlinkth(uni,'ally',myplayerally,lang),myrow.cells[5]) ; // Ally
	} else { // Allys' statistics
		var myplayerally = strip(myrow.cells[1].childNodes[1].childNodes[0].nodeValue) ;
		myrow.replaceChild(wrlink(uni,'ally',myplayerally,lang),myrow.cells[2]) ;
	}
}

function wrlinkth(uni,type,name,lang) { // Returns a table cell containing an image with a link to a player or ally
	var myth = document.createElement('TH') ;
	if ( name != '' ) {
		var mylink = document.createElement('A') ;
		mylink.href = wrlink(uni,type,name,lang) ;
		mylink.target = '_blank' ;
		var myimage = document.createElement('IMG') ;
		myimage.src = 'http://www.war-riders.de/img/favicon.ico' ;
		myimage.height = 14 ;
		mylink.insertBefore(myimage,mylink.firstChild) ;
		myth.insertBefore(mylink,myth.firstChild) ;
	}
	return myth ;
}
function wrlink(uni,type,name,lang) { // Returns the URL of a WR page
	return 'http://www.war-riders.de/'+lang+'/'+uni+'/details/'+type+'/'+name ; ;
}
/* Fonctions generalistes */
function xpath(query) {
	return document.evaluate( query , document , null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE , null );
}
function strip(str) { // Removes all type of spaces from the string (space, tab, carriage return, newline ...)
	return str.replace(/\f/g,'').replace(/\n/g,'').replace(/\r/g,'').replace(/\t/g,'').replace(/\v/g,'').replace(/^\s*/g,'').replace(/\s*$/g,'') ;
}
function debug_xml_element_new(node,prefix) {
// Fonction de debug : retourne une chaine indiquant la structure de l'objet XML passe en parametre
// La fonction est recursive et doit etre appelee avec un prefixe pour l'affichage, de preference, mettre une chaine vide
	var result ='';
	if (node)
		switch (node.nodeType) {
			case 1:
			case 9:
				var childs = node.childNodes ;
				result += prefix + '<' + node.nodeName + '>' ;
				if ( node.nodeValue != null )
					result += '(' + node.nodeValue + ')' ;
				result += '\n' ;
				if ( childs.length > 0 ) {
					for( var i = 0 ; i < childs.length ; i++) {
						child = childs.item(i) ;
						result += debug_xml_element_new(child,prefix+'\t('+i+')') ;
					}
				}
				break;
			case 3:
				var text = strip(node.nodeValue) ;
				//if ( text != '' ) 
					result += prefix + '"' + text + '"\n' ;
				break;
			default:
				result += prefix + 'unknown node type ' + node.nodeType + '\n'
		}
	else
		alert("L'objet n'existe pas") ;
	return result ;
}
function debug(node) {
	alert(debug_xml_element_new(node,'')) ;
}
