/**
 * twinpedia-hordes.user.js
 * Copyright (c) 2008-2009 Aurélien Maille
 * Released under the GPL license 
 * 
 * @version 0.3
 * @author  Aurélien Maille <bobe+hordes@webnaute.net>
 * @link    http://dev.webnaute.net/Applications/Twinpedia-hordes/
 * @license http://www.gnu.org/copyleft/gpl.html  GNU General Public License
 * @charset UTF-8
 */

// --------------------------------------------------------------------
// 
// This is a Greasemonkey user script.
// 
// To install, you need Greasemonkey: http://www.greasespot.net/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
// 
// To uninstall, go to Tools/Manage User Scripts,
// select "Twinpedia-hordes", and click Uninstall.
// 
// --------------------------------------------------------------------
// ==UserScript==
// @name           TwinPedia Hordes
// @namespace      http://dev.webnaute.net/Applications/Twinpedia-hordes
// @description    Changements graphiques sur Twinpedia/hordes
// @include        http://twinpedia.com/hordes/chantiers*
// @include        http://www.twinpedia.com/hordes/chantiers*
// ==/UserScript==
//


const TPH_VERSION = '0.3';
const TPH_APPNAME = 'Twinpedia-hordes';
const BG_COLORVAL = '#cec';

var getdata_url = 'http://dev.webnaute.net/hordes/xml?key=[apikey]&pseudo=[login]';

GM_registerMenuCommand('Configurer ' + TPH_APPNAME, function() {
	var login, apikey;
	if( (login = prompt("Saisissez votre pseudo de joueur à hordes :", GM_getValue('login', ''))) != null ) {
		GM_setValue('login', login);
	}
	
	if( (apikey = prompt("Saisissez votre clef API de joueur à hordes :", GM_getValue('apikey', ''))) != null ) {
		GM_setValue('apikey', apikey);
	}
	
	applyStyles();
});

document.styleSheets[document.styleSheets.length-1].insertRule(
	'tr.building_built td { background-color: ' + BG_COLORVAL + '; }',
	document.styleSheets[document.styleSheets.length-1].cssRules.length
);

function applyStyles()
{
	var login  = GM_getValue('login',  '');
	var apikey = GM_getValue('apikey',  '');
	if( login == '' || apikey == '' ) {
		return false;
	}
	
	var getdata_url2 = getdata_url.replace('\[login\]', login).replace('\[apikey\]', apikey);
	
	GM_xmlhttpRequest({
		method: 'GET',
		url: getdata_url2,
		headers: {
			'X-Handler'  : TPH_APPNAME,
			'User-Agent' : TPH_APPNAME + '/' + TPH_VERSION,
		},
		onload: function(responseDetails) {
			var listing = [];
			var items   = null;
			
			//
			// Retrait des classes building_built ajoutées précédemment s'il y a lieu
			//
			var items = document.evaluate('//table[@class="inline"]/tbody/tr[@class="building_built"]',
				document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
			for( var i = 0, m = items.snapshotLength; i < m; i++ ) {
				items.snapshotItem(i).removeAttribute('class');
			}
			
			// problème serveur
			if( responseDetails.status != 200 ) {
				return false;
			}
			
			try {
				var parser  = new DOMParser();
				var doc = parser.parseFromString(responseDetails.responseText, 'application/xml');
			}
			catch(e) {
				return false;
			}
			
			// Erreur XML
			if( doc.documentElement.nodeName == 'parsererror' || doc.getElementsByTagName('error').length > 0 ) {
				return false;
			}
			
			items = doc.getElementsByTagName('building');
			for( i = 0, m = items.length; i < m; i++ ) {
				listing[items[i].getAttribute('name').toLowerCase()] = items[i].getAttribute('id');
			}
			
			//
			// Ajout de la classe building_built sur les bâtiments concernés
			//
			items = document.evaluate('//table[@class="inline"]/tbody/tr/td[position()=1]/strong',
				document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
			
			for( i = 0, m = items.snapshotLength; i < m; i++ ) {
				item = items.snapshotItem(i);
				if( typeof(listing[item.firstChild.textContent.toLowerCase()]) != 'undefined' ) {
					item.parentNode.parentNode.setAttribute('class', 'building_built');
				}
			}
		}
	});
}

applyStyles();



