// ==UserScript==
// @author		tomasz.frelik (at) enzo.pl
// @namespace	http://frelo.enzo.pl/userscript
// @name		Travian: Map Scrolling
// @description	Adds the link to scroll the map diagonally 3 fields NW, NE, SE or SW. Good for looking over the neighbourhood (after first scroll your village is in the corner).
// @include		http://s*.travian*/karte.php
// ==/UserScript==

var mapPattern = "//div[@class='map_show_xy']/table[1]/tbody[1]";

map_start();

function map_start() {
			
	// make links for scrolling
	
	mapResults = document.evaluate( mapPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	//alert( mapResults.snapshotLength ); 	
	if ( mapResults.snapshotLength < 1 ) {
		return;
	}
	
	var tbody = mapResults.snapshotItem( 0 );
	//alert( tbody );
	
	newTd = document.createElement( 'td' );
	newTr = document.createElement( 'tr' );	
	
	var directions = new Array( 'NW', 'NE', 'SW', 'SE' );
	for ( var i = 0; i < directions.length; i++ ) {
	
		linkElement = document.createElement( 'a' );
		linkElement.appendChild( document.createTextNode( directions[i] ));
		linkElement.href = "#";
		linkElement.addEventListener( 'click', eval( "move" + directions[i] ), false );	
		newTd.appendChild( linkElement );

		if ( i == 0 || i == 2 ) {
			newTd.appendChild( document.createTextNode( '|' ));
		}

		if ( i == 1 ) {
			newTd.appendChild( document.createElement( 'br' ));
		}
		
	}
	
	//linkElement.href = 'karte.php';
	//tbody.appendChild( linkElement, res );	

	newTd.appendChild( linkElement )
	newTr.appendChild( newTd );
	tbody.appendChild( newTr );
	
	return;

}

function moveNW() {
	newPosition( -3, +3 );
}

function moveNE() {
	newPosition( +3, +3 );
}

function moveSE() {
	newPosition( +3, -3 );
}

function moveSW() {
	newPosition( -3, -3 );
}

function newPosition( moveX, moveY ) {
	var inputX = document.forms[0].elements[0];
	var inputY = document.forms[0].elements[1];
	inputX.value = Number( inputX.value ) + moveX;
	inputY.value = Number( inputY.value ) + moveY;
	document.forms[0].submit();
}
