// Updated for era 46 (April 15th)

var showInfoCount = 0
var miniMapIsHidden = true;
var showMagicBord = false;
var spell = false;
var bygg = false;
var army = false;
var NN;
var days = 0;
var imgfCount = 0;
var imgCount = 0;
var mDays = 1;
var oldX = 0;
var oldY = 0;
var armyID;
var armyX;
var armyY;
var lastMoveMouse = 0;
var armypathCount = 0;
var bolCanImoveNow = true;
var lastMouseMove = new Date();
var intArmyLastX = 0;
var intArmyLastY = 0;
var byggX = 0;
var byggY = 0;
var bScroll = false;
var scrollStartTop = 0;
var scrollStartLeft = 0;
var posLocTemp = '';
var imgCountLastClick = 1;
var imgfCountLastClick = 1;
var daysR = 0;
var mDaysR = 1;
var oldXR = 0;
var oldYR = 0;
var haveMoved = false;
var nyX = 0;
var nyY = 0;
var armyStep = 0;
var hasDblClicked = false;
var Xcursor = 0;
var Ycursor = 0;
var oDrag = null;
var iDragStartX = null
var FadeObjects = new Object();
var FadeTimers = new Object();
var slots = new Array(1); // True = nerstängd
var slotszindex = 100;
var flashInterval = new Object();
var ownArmyId = -1;
//var newWindow = new Object();

var OPERA = (navigator.userAgent.indexOf('Opera') != -1);
var CHROME = (navigator.userAgent.indexOf('Chrome') != -1);
var SAFARI = (navigator.userAgent.indexOf('Safari') != -1);
var FIREFOX = (navigator.userAgent.indexOf('Firefox') != -1);
var IE = (navigator.userAgent.indexOf('MSIE') != -1);

//OPERA = false;

var bodyHolder = document.body;

bodyHolder = document.getElementById('operafix');


if(document.all) {
  IE=true;
}
else
{
  NN=true;
}


var splashHtml = '<center><img src="/images/loader.gif"></center>';


function StringToInt(s) {
	return parseInt(s.replace(/\,/g, ''));
}

function scrollIfRight(e) {
	
	
	var button = new String();

	if (e.which == null)
		   /* IE case */
		   button= (e.button < 2) ? "LEFT" :
			 ((e.button == 4) ? "MIDDLE" : "RIGHT");
		else
		   /* All others */
		   button= (e.which < 2) ? "LEFT" :
			 ((e.which == 2) ? "MIDDLE" : "RIGHT");


	if(button=='LEFT') bScroll=true
	//else alert(button);

	
	return false;
}


function centerMouse() {
	
	if(!hasDblClicked) {

		var mouseX = Xcursor - parseInt(bodyHolder.scrollLeft);
		var mouseY = Ycursor - parseInt(bodyHolder.scrollTop);

		//window.status = mouseX + ', ' + mouseY;

		var winW = (window.innerWidth) ? window.innerWidth : bodyHolder.clientWidth;
		var winH = (window.innerHeight) ? window.innerHeight : bodyHolder.clientHeight;
		
		//if(mouseX > winW*.8 || mouseX < winW*.2 || mouseY > winH*.8 || mouseY < winH*.2)
		bodyHolder.scrollLeft = parseInt(bodyHolder.scrollLeft) + mouseX - winW/2;
		bodyHolder.scrollTop = parseInt(bodyHolder.scrollTop) + mouseY - winH/2;

		
		// Smooth scroll
		
		//for(var x=parseInt(bodyHolder.scrollLeft);x<(parseInt(bodyHolder.scrollLeft) + mouseX - winW/2);x++) {
		
		//	setTimeout('self.scroll(' + x + ', ' + (parseInt(bodyHolder.scrollTop) + mouseY - winH/2) + ')', (x-mouseX) * 10);
		//}
	}
}


function race(intRace) {
	switch (intRace) {
		case '1': return 'Human';
		case '2': return 'Elf';
		case '3': return 'Orc';
		case '4': return 'Dwarf';
		case '5': return 'Troll';
		case '6': return 'Halfling';
	}
}


function Show(strTxt) {
	if(!army) {
		document.getElementById('infotext').innerHTML = strTxt;
		document.getElementById('ifo').style.visibility='visible';
		document.getElementById('ifo').style.zoom = 1 / currentZoom
		showInfoCount = 1
		Showa()
	}
}

function Showa() {
	showInfoCount++;
	if(showInfoCount < 50) {

		document.getElementById('ifo').style.top = bodyHolder.scrollTop - 50 - -showInfoCount + 'px'
		document.getElementById('ifo').style.left = bodyHolder.scrollLeft + bodyHolder.clientWidth/2 - 153 + 'px'
		showInfoCount = showInfoCount + showInfoCount;
		setTimeout("Showa()",50);
	}
}

function Hide() {
	document.getElementById('ifo').style.visibility='hidden'
	showInfoCount = 0;
}

function hidePath() {
	var tempObj;
	for(i=1;;i++) {
		tempObj = document.getElementById('armypath' + i)
		if(tempObj) tempObj.style.visibility='hidden';
		else break;
	}
}

function showPath() {
	var tempObj;
	for(i=1;;i++) {
		tempObj = document.getElementById('armypath' + i)
		if(tempObj) tempObj.style.visibility='visible';
		else break;
	}
}



walls = walls.replace(/\|/g, '<table border=0 background=images/wallX.gif width=')
walls = walls.replace(/\@/g, '<table border=0 background=images/wallY.gif width=')
walls = walls.replace(/\,/g, ' height=')
walls = walls.replace(/\#/g, ' style="position: absolute; top: ')
walls = walls.replace(/\%/g, 'px; left: ')
walls = walls.replace(/\&/g, 'px;"><tr><td></td></tr></table>')
document.write(walls)


var armies = strArmies.split('&');
for(i=0;i<armies.length;i++) {
  armies[i] = armies[i].split('#');
}

var cities = strCities.split('&');
for(i=0;i<cities.length;i++) {
  cities[i] = cities[i].split('#');
}

var dragons = strDragons.split('&');
for(i=0;i<dragons.length;i++) {
  dragons[i] = dragons[i].split('#');
}

if(strArmiesPath) {
	armyPath = strArmiesPath.split('&');
	for(i=0;i<armyPath.length;i++) {
	  armyPath[i] = armyPath[i].split('#');
	}
}

//City images
for(i=0;i<cities.length-1;i++) {
	document.write('<img width=150 height=150 src="images/' + cities[i][3] + cities[i][2] + '.gif" style="position: absolute; top:' + (cities[i][5] - -mapSize - 75) + '; left: ' + (cities[i][4] - -mapSize - 75) + '">');
}

if(showKDbanners) {
	for(i=0;i<cities.length-1;i++) {
		if((cities[i][13] != userID) || (intUserKingdomID != cities[i][9])) document.write('<img class="kdbanner" width=24 height=20 src="http://' + strHost + '/KDbanners/kingdom' + cities[i][9] + '.jpg" style="position: absolute; top:' + (cities[i][5] - -mapSize - 10) + '; left: ' + (cities[i][4] - -mapSize) + ';">');
	}
}



// Army Direction
for(i=0;i<armies.length-1;i++) {
	if(armies[i][8].length>0) document.write('<div style="width: 83px; height: 77px; position: absolute; top: ' + (armies[i][3] - -mapSize - 38) + '; left: ' + (armies[i][2] - -mapSize - 41) + '; background-image: url(\'images/arrow' + armies[i][8] + '.gif\');"></div>');
}

// Dragon Direction
for(i=0;i<dragons.length-1;i++) {
	document.write('<div style="width: 83px; height: 77px; position: absolute; top: ' + (dragons[i][3] - -mapSize - 38 - -(modYdirection(dragons[i][5]) * dragons[i][4] / 3) ) + '; left: ' + (dragons[i][2] - -mapSize - 41 - -(modXdirection(dragons[i][5]) * dragons[i][4] / 3)  ) + '; background-image: url(\'images/arrow' + dragons[i][5] + '.gif\');"></div>');
}
//id, name, X, Y, power, direction

// Dragons
for(i=0;i<dragons.length-1;i++) {
	document.write('<img title="' + dragons[i][1] + '" onclick="pop(\'dragon.asp?id=' + dragons[i][0] + '\')" style="position: absolute; top:' + (dragons[i][3] - -mapSize - dragons[i][4] / 2.31) + '; left: ' + (dragons[i][2] - -mapSize - dragons[i][4] / 2) + '; width: ' + dragons[i][4] + 'px; " class="dragons" src="images/dragon.gif">');
	//alert(
}
//id, name, X, Y, power, direction




// FOG OF WAR
if(showFoW==1) {
	try {
		document.write('<div id="fow"></div>');
		drawFoW();
	}
	catch(err) {
		//alert('script aborted:\n' + err);
	}

}

document.write(strWaypoints);

// City names
for(i=0;i<cities.length-1;i++) {
	if((cities[i][13] == userID) && (intUserKingdomID == cities[i][9] || ((intUserKingdomID == -1) && (cities[i][9] == 0)))) {
		document.write('<div title="Your city: ' + cities[i][14] + ' buildings." onclick="pop(\'build.asp?cityID=' + cities[i][0] + '\')" style="position: absolute; top:' + (cities[i][5] - -mapSize - 25) + '; left: ' + (cities[i][4] - -mapSize - 50) + ';" class="citynames">' + cities[i][1] + '</div>');
	}
	else document.write('<div title="' + race(cities[i][7]) + ' city owned by ' + cities[i][6] + '' + cities[i][11] + ': ' + cities[i][14] + ' buildings." onclick="pop(\'cityInfoE.asp?cityID=' + cities[i][0] + '\')" style="position: absolute; top:' + (cities[i][5] - -mapSize - 25) + '; left: ' + (cities[i][4] - -mapSize - 50) + ';" class="citynames">' + cities[i][1] + '</div>');
	if(cities[i][13] == userID) cityNR++
}
//citys.ID, name, lev, city_type, X, Y, CONCAT(accounts.title, ' ', accounts.owner), race, tag, kingdomID, gates, kingdomanme, los, owneriD





// Army icons with size
for(i=0;i<armies.length-1;i++) {
	if(armies[i][11] != userID) {
		if(armies[i][12] != '0' || armies[i][15] > 0) {
		
			
			document.write('<div onclick="pop(\'armyInfoE.asp?armyID=' + armies[i][0] + '\')" title="' + armies[i][1] + ': ' + race(armies[i][5]) + ' ' + armySize(armies[i][9]) + ' controlled by ' + armies[i][4] + '' + armies[i][10] + '" style="position: absolute; top: ' + (armies[i][3] - -mapSize - 15) + '; left:' + (armies[i][2] - -mapSize - 15) + ';" class="armyclick ')
			
			if(armies[i][15] > 0) document.write('frozen ');
			if(armies[i][12] != '0') document.write('attackingarmy ');
			
			document.write('">');
			
			//if(armies[i][15] > 0) document.write('*');

			document.write('<img width=23 height=5 src="images/armysize' + armies[i][9] + '.gif"><br>')
			if(showKDbanners) document.write('<img width=18 height=15 src="http://' + strHost + '/KDbanners/kingdom' + armies[i][7] + '.jpg" style="border: 1px solid #000000;">')
			else {
				if(armies[i][7] == intUserKingdomID) document.write('<center><div style="width: 18px; height: 15px; font-size: 0px; background-color: darkgreen; border: 1px solid black;"></div>')
				else document.write('<center><div style="width: 18px; height: 15px; font-size: 0px; background-color: darkred; border: 1px solid black;"></div>')
			}
			document.write('</div>')
		}
		else {
			if(showKDbanners) document.write('<img class="armyclick" onclick="pop(\'armyInfoE.asp?armyID=' + armies[i][0] + '\')" title="' + armies[i][1] + ': ' + race(armies[i][5]) + ' ' + armySize(armies[i][9]) + ' controlled by ' + armies[i][4] + '' + armies[i][10] + '" width=18 height=15 src="http://' + strHost + '/KDbanners/kingdom' + armies[i][7] + '.jpg" style="position: absolute; top: ' + (armies[i][3] - -mapSize - 8) + '; left:' + (armies[i][2] - -mapSize - 9) + '; border: 1px solid #000000;">')
			else {
				if(armies[i][7] == intUserKingdomID) document.write('<div class="armyclick" onclick="pop(\'armyInfoE.asp?armyID=' + armies[i][0] + '\')" title="' + armies[i][1] + ': ' + race(armies[i][5]) + ' ' + armySize(armies[i][9]) + ' controlled by ' + armies[i][4] + '' + armies[i][10] + '" style="background-color: darkgreen; width: 18px; height: 15px; position: absolute; top: ' + (armies[i][3] - -mapSize - 8) + '; left:' + (armies[i][2] - -mapSize - 9) + '; border: 1px solid #000000;"></div>')
				else document.write('<div class="armyclick" onclick="pop(\'armyInfoE.asp?armyID=' + armies[i][0] + '\')" title="' + armies[i][1] + ': ' + race(armies[i][5]) + ' ' + armySize(armies[i][9]) + ' controlled by ' + armies[i][4] + '' + armies[i][10] + '" style="background-color: darkred; width: 18px; height: 15px; position: absolute; top: ' + (armies[i][3] - -mapSize - 8) + '; left:' + (armies[i][2] - -mapSize - 9) + '; border: 1px solid black;"></div>')
			}
			
			document.write('<img width=23 height=5 src="images/armysize' + armies[i][9] + '.gif" style="position: absolute; top: ' + (armies[i][3] - -mapSize - 14) + '; left:' + (armies[i][2] - -mapSize - 10) + ';">')
			
		}
	}
}




//armyPath
if(strArmiesPath) {
	for(i=0;i<armyPath.length-1;i++) {
		tempArray = armyPath[i][1].split(':');
		for(j=0;j<tempArray.length;j++) {
		  tempArray[j] = tempArray[j].split(',');
		}
		for(j=0;j<tempArray.length-1;j++) {
			armypathCount++;
			document.write('<img id="armypath' + armypathCount + '" src="images/reddot.gif" style="position: absolute; top:' + (tempArray[j][1] - -mapSize - 15) + '; left: ' + (tempArray[j][0] - -mapSize - 15) + '; visibility: hidden;">');
		}
	}
}


// Circles for army movement
for(i=1;i<101;i++) {
	document.write('<img id="imf' + i + '" width=30 height=30 src="images/cirkel.gif" style="position: absolute; top: -50px; left: -50px">');
	document.write('<img id="im' + i + '" width=10 height=10 src="images/reddot.gif" style="position: absolute; top: 10px; left: -15px">');
}


// Own armies (movable)
document.write('<form name="armysize">');
for(i=0;i<armies.length-1;i++) {
	if(armies[i][11] == userID) {
	
		ownArmyId = i;
	
		document.write('<div id="army' + armies[i][0] + '" ondblclick="movethearmy()" onClick="move(' + i + '); hidePath();" title="' + armies[i][1] +': Your army, click to move it" onmouseover="if(!army) showPath();" onmouseout="hidePath();" style="position: absolute; top: ' + (armies[i][3] - -mapSize - 13) + '; left:' + (armies[i][2] - -mapSize - 13) + ';" class="ownarmy">')

		document.write('<img width=23 height=5 src="images/armysize' + armies[i][9] + '.gif"><br>')
		
		if(showKDbanners) document.write('<img width=18 height=15 src="http://' + strHost + '/KDbanners/kingdom' + armies[i][7] + '.jpg" style="border: 1px solid #000000;">')
		else document.write('<center><div style="width: 18px; height: 15px; background-color: black; border: 1px solid black;"></div>')

		document.write('</div>')
		
		document.write('<input type="hidden" name="army' + armies[i][0] + '" value="' + armies[i][13] + '">'); 
	}
}
document.write('</form>');

//armys.ID, name, X, Y, CONCAT(accounts.title, ' ', accounts.owner), race, tag, kingdomID, going, size, ownerID



function modXdirection(d) {

	switch(parseInt(d)) {
	
		case 0: return 1; // East
		case 1: return 1; // North east';
		case -1: return 1; // South East
		case 2: return 0; // North
		case -2: return 0; // South
		case 3: return -1; //north west
		case -3: return -1; // south west
		case 4: return -1; // west
		case -4: return -1; // west
		case 5: return 0; // not moving
	}

}	

function modYdirection(d) {

	switch(parseInt(d)) {
	
		case 0: return 0; // East
		case 1: return -1; // North east';
		case -1: return 1; // South East
		case 2: return -1; // North
		case -2: return 1; // South
		case 3: return -1; //north west
		case -3: return 1; // south west
		case 4: return 0; // west
		case -4: return 0; // west
		case 5: return 0; // not moving
	}
	
	return 0;
}	




function armySize(i) {
	switch(parseInt(i)) {
		case 1: return 'scout';
		case 2: return 'section';
		case 3: return 'platoon';
		case 4: return 'company';
		case 5: return 'battalion';
		case 6: return 'regiment';
		case 7: return 'brigade';
		case 8: return 'division';
		case 9: return 'corps';
		case 10: return 'army';
		case 11: return 'army group';
		case 12: return 'horde';
	}
	
	return '';

}



function pop(vad) {

	if(document.location.href.indexOf('translate.google.com') > 0) {

		var linktype = false;
		var linkId = parseInt(vad);

		if(vad.indexOf('=') > 0) {
			linkId = vad.substring(vad.lastIndexOf('=')+1, vad.length);
		}



		if(vad.indexOf('cityID') > 0) linktype='c'
		else if(vad.indexOf('armyID') > 0) linktype='a';
		else if(vad.indexOf('dragon') > 0) linktype='d';


		if(linktype) vad = document.getElementById(linktype + linkId).href;

		//alert('*' + linktype + linkId + '* = ' + vad);

		go(vad, window.frames['ifrslot0']);
		
		return true;

	}
	else {
	
		if(vad.indexOf('vuid') == -1) vad = vad + '&vuid=' + vuid;
		
		vad = 'http://' + strHost + '/' + vad;
	}


	popup(vad)
}


function castit() {
	pop("castSpell.asp?spellX=" + Xcursor + "&spellY=" + Ycursor + "&spell=" + spell + "&world=" + world)
	document.getElementById(spell).style.left = -50 + 'px'
	spell = false
	showMagicBord = false;
}


function buildit() {
	var tempBygg = bygg;
	bygg = false;

	var modX = 10;
	var modY = 10;
	var errorT = false;

	document.getElementById(tempBygg).style.left = -200 + 'px'

	if(totalCost<=StringToInt(parent.resources.document.getElementById('gold').innerHTML)) {
		if(tempBygg=='waypoint') {
			name = prompt("What do you want to call this waypoint?\n(Max 15 characters)","Waypoint");
			if(name=="" || name=="Waypoint")
			{
				alert("You must choose a name for your new waypoint!")
				buildit()
			}
			else if(name=='null') {
				// What the hell?
			}
			else if(name)
			{
				pop("newColony.asp?world=" + world + "&X=" + byggX + "&Y=" + byggY + "&vad=" + tempBygg + "&name=" + escape(name))
			}
		}
		else {

			if(checkT(Xcursor, Ycursor, modX, modY)==false) {
				alert("You cannot build on water, mountains or in forests!")
				errorT = true
			}

			if(!errorT) {
				name = prompt("What do you want to call your new city?\n(Max 15 characters)","City Name");
				if(name=="" || name=="City Name")
				{
					alert("You must choose a name for your new city!")
					buildit()
				}
				else if(name=='null') {
					// What the hell?
				}
				else if(name)
				{
					pop("newColony.asp?world=" + world + "&X=" + byggX + "&Y=" + byggY + "&vad=" + tempBygg + "&name=" + name + "&wall1Width=" + wall1Width + "&wall2Width=" + wall2Width + "&wall1Height=" + wall1Height + "&wall2Height=" + wall2Height)
				}
			}
		}
	}
	else {
		if(tempBygg=='wallY' || tempBygg=='wallX') alert("You need " + totalCost + " gold to build a wall!")
		else alert("You need " + totalCost + " gold to build a " + tempBygg + "!")
	}
	Hide();
}


function clearArmyPathImages(starta, stoppa) {

	for(var as=starta;as<=stoppa;as++) {
		document.getElementById('im' + as).style.left = -20 + 'px'
		document.getElementById('im' + as).style.top = -20 + 'px'
		document.getElementById('im' + as).width = 10
		document.getElementById('im' + as).height = 10
	}
}


function clearArmyPathImagesF(starta, stoppa) {

	for(var as=starta;as<=stoppa;as++) {
		document.getElementById('imf' + as).style.left = -50 + 'px'
		document.getElementById('imf' + as).style.top = -50 + 'px'
	}
}


function move(i)
{

	haveMovedArmy = true;

	var oldXa = (armies[i][2] - -mapSize);
	var oldYa = (armies[i][3] - -mapSize);

	if(army && moveArmyByClicking && !errorT) {
	
		oldXR = nyX;
		oldYR = nyY;
		
		if(nyX==0 && nyY==0) {
			nyX = oldXa;
			nyY = oldYa;
		}

		daysR = days;
		mDaysR = mDays;

		document.pos.loc.value = document.pos.loc.value + posLocTemp;

		posLocTemp = '';

		imgCountLastClick = imgCount + 1;
		imgfCountLastClick = imgfCount + 1;
		
		
		// Center on mouse position (wait for dbl click)
		hasDblClicked = false;
		setTimeout('centerMouse()', 500);


	}
	
	
	var obj = 'army' + armies[i][0];
	var armyIDa = armies[i][0];
	var pureHorse = armies[i][14];
	
	if(!army && bolCanImoveNow) {
		oldX = oldXa
		oldY = oldYa
		
		
		armyX = oldXa
		armyY = oldYa
		army = obj
		armyID = armyIDa
		
		clearArmyPathImages(1, 100);
		clearArmyPathImagesF(1, 100);
		
		errorT = false
		days = 1
		mDays = 1
		
		oldXR = oldXa;
		oldYR = oldYa;
		
		daysR = 1;
		mDaysR = 1;
		
		posLocTemp = '';
		
		if(armageddon) armyStep = 20
		else armyStep = Math.round(Math.max(20-Math.log(eval('document.armysize.army' + armyID).value),4) / 3.16);
		
		if(pureHorse==1) armyStep = Math.round(armyStep * 2);
		
		if(armyStep<2) armyStep = 2;
		
		if(armyStep>24) armyStep = 24;
		
		
		document.pos.loc.value = document.pos.loc.value=""
		document.pos.loc.value = document.pos.loc.value + (oldXa - mapSize) + "," + (oldYa - mapSize) + ":"
		
		bolCanImoveNow = false;
		
		Hide();
	}
	
}


function movethearmy() {

	hasDblClicked = true;

	var armytemp = army;
	army = false;
	
	if(!errorT) {
	
		//if(moveArmyByClicking) {
		//	days = daysR;
		//	mDays = mDaysR;
		//}
		
	
		document.pos.loc.value = document.pos.loc.value + (intArmyLastX - mapSize) + "," + (intArmyLastY - mapSize) + ":";
		days++;
		if(days/armyStep>=1) {
			imgCount++
			if(imgCount > 99) imgCount = 1
			document.getElementById('im' + imgCount).style.left = intArmyLastX -15 + 'px'
			document.getElementById('im' + imgCount).style.top = intArmyLastY -15 + 'px'
			document.getElementById('im' + imgCount).width = 30
			document.getElementById('im' + imgCount).height = 30
			mDays++
			days = 0
		}

		if(confirm("This will take " + mDays + " days. Are you sure you want to move your army?")) {	
			//pop("moveArmy2.asp?armyID=" + armyID + "&world=" + world + "&armyStep=" + armyStep + "&armySize=" + );
			
			document.pos.armyID.value = armyID;
			document.pos.armyStep.value = armyStep;
			document.pos.armySize.value = eval('document.armysize.army' + armyID).value;

			
			popup(document.getElementById('movearmylink').href)
			//else document.pos.submit();
			
		}
		else {
			for(as=1;as<=100;as++) {
				document.getElementById('im' + as).style.left = -10 + 'px'
				document.getElementById('im' + as).style.top = -10 + 'px'
				document.getElementById('im' + as).width = 10
				document.getElementById('im' + as).height = 10
				document.getElementById('imf' + as).style.left = -10 + 'px'
				document.getElementById('imf' + as).style.top = -10 + 'px'
			}
			document.getElementById(armytemp).style.left = armyX -armyModX + 'px'
			document.getElementById(armytemp).style.top = armyY -armyModY + 'px'
		}
	}
	else {
		alert("The red circle(s) means that you can not move the army over that area\nYou can not move the army over water, forest, mountains or cities with closed gates.")
		for(as=1;as<=100;as++) {
			document.getElementById('im' + as).style.left = -10 + 'px'
			document.getElementById('im' + as).width = 10
			document.getElementById('im' + as).height = 10
			document.getElementById('imf' + as).style.left = -50 + 'px'
		}
		document.getElementById(armytemp).style.left = armyX -armyModX + 'px'
		document.getElementById(armytemp).style.top = armyY -armyModY + 'px'
	}
	
	setTimeout("bolCanImoveNow = true",1000);
}


function handleMousemove(e) {
	var moveMouse = new Date();
	if(!lastMoveMouse) lastMoveMouse = moveMouse;

	if(army || spell || bygg || scroll) {
		
		if (!e) var e = window.event;
		
		if (e.pageX || e.pageY) {
			Xcursor = e.pageX;
			Ycursor = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			Xcursor = e.clientX;
			Ycursor = e.clientY;
		}



		//Xcursor = (document.layers) ? e.pageX : event.x;
		//Ycursor = (document.layers) ? e.pageY : event.y;

		Xcursor = Xcursor / currentZoom;
		Ycursor = Ycursor / currentZoom;

		Xcursor = Xcursor + bodyHolder.scrollLeft;
		Ycursor = Ycursor + bodyHolder.scrollTop;

		//window.status = 'Mouse position = (' + Xcursor + ', ' + Ycursor + ')';
	}
	
	if(army) {
		x = Xcursor
		y = Ycursor
		
		intArmyLastX = x;
		intArmyLastY = y;
		
		document.getElementById(army).style.left = (x - 13 * currentZoom) + 'px'
		document.getElementById(army).style.top = (y - 13 * currentZoom) + 'px'

		if(moveArmyByClicking) {
		
		
			// Clear old temp path
			clearArmyPathImages(imgCountLastClick, imgCount);
			clearArmyPathImagesF(imgfCountLastClick, imgfCount);

			errorT = false;
			days = daysR;
			mDays = mDaysR;
			
			oldX = oldXR;
			oldY = oldYR;
		
			imgCount = imgCountLastClick;
			imgfCount = imgfCountLastClick;

			posLocTemp = '';
		
			// Draw a temporary path to the mouse location

			//oldXD = oldX;
			//oldYD = oldY;

			avs = Math.sqrt((x-oldX)*(x-oldX) + (y-oldY)*(y-oldY))
			if(avs >= armyMoveLength) {
				extra = Math.floor(avs/armyMoveLength);
				
				for(var i=1;i<=extra;i++) {
					nyX = oldX+((x-oldX)*(armyMoveLength/avs))*i
					nyY = oldY+((y-oldY)*(armyMoveLength/avs))*i
							
					//window.status = Math.round(Math.sqrt((nyX-oldXD)*(nyX-oldXD) + (nyY-oldYD)*(nyY-oldYD))*100) / 100 + ' / ' + armyMoveLength;

					//if(Math.sqrt((nyX-oldXD)*(nyX-oldXD) + (nyY-oldYD)*(nyY-oldYD)) > (armyMoveLength+1)) alert('dist=' + Math.sqrt((nyX-oldXD)*(nyX-oldXD) + (nyY-oldYD)*(nyY-oldYD)) + ' aaaaarh!');
					
					//oldXD = nyX;
					//oldYD = nyY;
					
					days++
					posLocTemp += (nyX - mapSize) + "," + (nyY - mapSize) + ":"
					if(days/armyStep>=1) {
						imgCount++
						if(imgCount > 99) imgCount = 1
						document.getElementById('im' + imgCount).style.left = Math.round(nyX) -15 + 'px'
						document.getElementById('im' + imgCount).style.top = Math.round(nyY) -15 + 'px'
						document.getElementById('im' + imgCount).width = 30
						document.getElementById('im' + imgCount).height = 30
						mDays++
						days = 0
					}
					if(!checkT(nyX, nyY, 4, 4) || !checkC(nyX, nyY, 4, 4)) {
						imgfCount++
						if(imgfCount > 99) {
							imgfCount = 1;
							clearArmyPathImagesF(imgfCountLastClick, 99);
							imgfCountLastClick = 1;
						}
						document.getElementById('imf' + imgfCount).style.left = nyX -15 + 'px'
						document.getElementById('imf' + imgfCount).style.top = nyY -15 + 'px'
						errorT = true
					}
					else {
						imgCount++
						if(imgCount > 99) {
							imgCount = 1
							clearArmyPathImages(imgCountLastClick, 99);
							imgCountLastClick = 1;
						}
						document.getElementById('im' + imgCount).style.left = nyX -5 + 'px'
						document.getElementById('im' + imgCount).style.top = nyY -5 + 'px'
						document.getElementById('im' + imgCount).width = 10
						document.getElementById('im' + imgCount).height = 10
					}

				}

			}







		
		}
		
		
		else {
			// Follow the mouse
			avs = Math.sqrt((x-oldX)*(x-oldX) + (y-oldY)*(y-oldY))
			if(avs > armyMoveLength) {
				extra = Math.floor(avs/armyMoveLength)
				for(i=1;i<=extra;i++) {
					nyX = oldX+((x-oldX)*(armyMoveLength/avs))*i
					nyY = oldY+((y-oldY)*(armyMoveLength/avs))*i
					days++
					document.pos.loc.value = document.pos.loc.value + (nyX - mapSize) + "," + (nyY - mapSize) + ":"
					if(days/armyStep>=1) {
						imgCount++
						if(imgCount > 99) imgCount = 1
						document.getElementById('im' + imgCount).style.left = nyX -15 + 'px'
						document.getElementById('im' + imgCount).style.top = nyY -15 + 'px'
						document.getElementById('im' + imgCount).width = 30
						document.getElementById('im' + imgCount).height = 30
						mDays++
						days = 0
					}
					if(!checkT(nyX, nyY, 4, 4) || !checkC(nyX, nyY, 4, 4)) {
						imgfCount++
						if(imgfCount > 99) imgfCount = 1
						document.getElementById('imf' + imgfCount).style.left = Math.round(nyX) -15 + 'px'
						document.getElementById('imf' + imgfCount).style.top = Math.round(nyY) -15 + 'px'
						errorT = true
					}
					else {
						imgCount++
						if(imgCount > 99) imgCount = 1
						document.getElementById('im' + imgCount).style.left = nyX -5 + 'px'
						document.getElementById('im' + imgCount).style.top = nyY -5 + 'px'
						document.getElementById('im' + imgCount).width = 10
						document.getElementById('im' + imgCount).height = 10
					}

				}


				oldX = nyX
				oldY = nyY

			}
		}
	}


	if(spell) {
		try {
			document.getElementById(spell).style.left = (Xcursor - document.getElementById(spell).width/2) + 'px'
			document.getElementById(spell).style.top = (Ycursor - document.getElementById(spell).height/2) + 'px'
		}
		catch(e) {
			;
		}
	}

	if(bygg) {

		document.getElementById(bygg).style.left = Xcursor - 10 + 'px'
		document.getElementById(bygg).style.top = Ycursor - 10 + 'px'

		if(bygg=='waypoint') {
			//document.getElementById('waypoint').style.left = Xcursor - 30 + 'px'
			//document.getElementById('waypoint').style.top = Ycursor - 30 + 'px'
			totalCost = 1000
			//setTimeout("ifNotMovedShow('<br>Building a waypoint will cost " + totalCost + " gold!')", 500)
			Hide();
		}

	
		byggX = Xcursor;
		byggY = Ycursor;
	
	}
	
	if(bScroll) {
	
		if((Xcursor!=scrollStartLeft) || (Ycursor!=scrollStartTop)) {

		
			//bodyHolder.scrollLeft = bodyHolder.scrollLeft + (scrollStartLeft-Xcursor) * 1;
			//bodyHolder.scrollTop = bodyHolder.scrollTop + (scrollStartTop-Ycursor) * 1;
			
			
			bodyHolder.scrollLeft = bodyHolder.scrollLeft + (scrollStartLeft-Xcursor);
			bodyHolder.scrollTop = bodyHolder.scrollTop + (scrollStartTop-Ycursor);
				
			
			deSelect()
				
		}
		
	}
	else {
		scrollStartLeft = Xcursor;
		scrollStartTop = Ycursor;
		
	}
	

	if(oDrag) {
	
		oDrag.style.left = iDragStartX + Xcursor - iDragStartMouseX;
		oDrag.style.top = iDragStartY + Ycursor - iDragStartMouseY;
		
		window.status = oDrag.style.left + ', ' + oDrag.style.top;
		
		deSelect();

	}


	lastMoveMouse = moveMouse;
}


function ifNotMovedShow(txt) {
	var tempTid = new Date();
	if(tempTid.getTime() - lastMoveMouse.getTime() > 500) Show(txt);
}

function castspell() {
	pop("castSpell.asp?spellX=" + Xcursor + "&spellY=" + Ycursor + "&spell=" + spell + "&world=" + world)
	document.getElementById(spell).style.left = -50 + 'px'
	spell = false
}


function checkC(tX, tY, modX, modY) {

	var cityModX = 0;
	var cityModY = 0;

	tX = tX - mapSize;
	tY = tY - mapSize;

	for(var cht=0;cht<cities.length-1;cht++) {
		
		if(cities[cht][10] == 1) {
			// The gates are closed
			if(!(cities[cht][13] == userID || intUserKingdomID == cities[cht][9])) {
				// Not in same kingdom
				cityModX = (cities[cht][2] < 8) ? cities[cht][2] * 10 : 75;
				cityModY = cityModX;

				if(((cities[cht][4]-cityModX)<(tX+modX)) && ((cities[cht][4]- -cityModX)>(tX-modX)) && ((cities[cht][5]-cityModY)<(tY+modY)) && ((cities[cht][5]- -cityModY)>(tY-modY))) {
					return false;
				}
			}
		}
	}
	
	return true;

}


function mainLoop() {

	if(IE && !popups) {
		// IE doesn't support fixed positions
		
		
		
		//Slots (dragable)
		for(var i=0;i<slots.length;i++) {
			document.getElementById('slot' + i).style.top = document.getElementById('slot' + i).fixedY + bodyHolder.scrollTop;
			document.getElementById('slot' + i).style.left = document.getElementById('slot' + i).fixedX + bodyHolder.scrollLeft;	
		}

	
	
	}

	if(showMiniMap) miniMap()

	if(showMagicBord) {
		document.getElementById('magicBord').style.display='block';
	
		if(IE) {
			
			document.getElementById('magicBord').style.left=bodyHolder.scrollLeft - -document.getElementById('magicBord').fixedX;
			document.getElementById('magicBord').style.top=bodyHolder.scrollTop - -document.getElementById('magicBord').fixedY;
		}
	}
	else {
		document.getElementById('magicBord').style.display='none';
	}
	
}

document.onmouseup = function() {
	bScroll = false;
	
	if(oDrag) {
		if(IE) {
			// Save position for stupid IE
			oDrag.fixedX =  parseInt(oDrag.style.left) - parseInt(bodyHolder.scrollLeft);
			oDrag.fixedY =  parseInt(oDrag.style.top) - parseInt(bodyHolder.scrollTop);
		}
		oDrag = null;
	}
	
	// De-select everything
	deSelect()

}



function enableDrag(obj) {
	oDrag = obj;
	iDragStartX = parseInt(obj.style.left);
	iDragStartY = parseInt(obj.style.top);
	iDragStartMouseX = Xcursor;
	iDragStartMouseY = Ycursor;
}


function deSelect() {
	try {
		if (document.selection) document.selection.empty()
		else window.getSelection().removeAllRanges();
	}
	catch(e) {};
}



function minimizeFrame(slot) {
	document.getElementById('slot' + slot).style.display='none';
	

}


function backFrame(slot) {
	window.frames['ifrslot' + slot].history.back();
}


function closeFrame(slot) {

	//document.all['ifrslot' + slot].contentDocument.body.innerHTML = splashHtml;

	document.getElementById('slot' + slot).style.display='none';
	
	if(OPERA) {
		bodyHolder.scrollTop = bodyHolder.scrollTop + 1;
		bodyHolder.scrollTop = bodyHolder.scrollTop - 1;
	}
		
	mainLoop();

}



function bodyLoad() {

	placeMenus();
	

	// IE doesn't support fixed position, so change to absolute and and add a "fixed" attribute
	if(IE && !popups) {
	
		for(var i=0;i<slots.length;i++) {
			document.getElementById('slot' + i).style.position='absolute';
			document.getElementById('slot' + i).fixedY = parseInt(document.getElementById('slot' + i).style.top)
			document.getElementById('slot' + i).fixedX = parseInt(document.getElementById('slot' + i).style.left)
		}
		
	}
	
	
	if(IE) {
		// Add support for fixed position
		document.getElementById('magicBord').style.position='absolute';
		document.getElementById('magicBord').fixedX = parseInt(document.getElementById('magicBord').style.left)
		document.getElementById('magicBord').fixedY = parseInt(document.getElementById('magicBord').style.top)
	}

	// Fill the menu select boxes
	try {
		parent.menu.bodyLoad(loggedIn, popups, showMiniMap);
	}
	catch(err) {
	}
	
	//alert('main showMiniMap=' + showMiniMap);

	// Open up a page ?
	if(showNews || openpage.length>0 ) {
	 
		if(loggedIn && openpage=='') openpage = 'news2.asp';
		else if(openpage.length>0) openpage = openpage;
		else if(!loggedIn) openpage = 'welcome.asp';

		if(openpage.indexOf('vuid') == -1) {
			if(openpage.indexOf('?') == -1)	openpage+= '?vuid=' + vuid
			else openpage+= '&vuid=' + vuid;
		}
		
		if(openpage.indexOf(strHost) == -1) openpage = 'http://' + strHost + '/' + openpage;
		
		
		popup(openpage);
	}
	
	scrollit();
	
	//alert('body loaded\npopups=' + popups + '\nopenpage=' + openpage);
}


function placeMenus() {
	
	//var clientHeight = parseInt(bodyHolder.clientHeight);
	//var clientWidth = parseInt(bodyHolder.clientWidth);

	var clientHeight = parseInt(document.body.clientHeight);
	var clientWidth = parseInt(document.body.clientWidth);

	var iFrameHeight = 600;
	
	if(document.getElementById('id_ifrslot0') != null) {
		try {
			iFrameHeight = document.getElementById('id_ifrslot0').contentDocument.getElementById('main').clientHeight - -30;
		}
		catch(err) {};
	}

	if(!popups) {

		// Frame slots in the center of the screen
		for(var i=0;i<slots.length;i++) {

			var slotHeight = parseInt(document.getElementById('slot' + i).style.height);
			var slotWidth = parseInt(document.getElementById('slot' + i).style.width);

			// Resize if too high
			if( slotHeight > ( clientHeight - 50 ) ) {
				document.getElementById('slot' + i).style.height = clientHeight - 50;
				document.getElementById('id_ifrslot' + i).style.height = parseInt(clientHeight) - 86;
			}
			else if( slotHeight < 636) {
				document.getElementById('slot' + i).style.height = Math.min(clientHeight - 50, iFrameHeight - -36);
				document.getElementById('id_ifrslot' + i).style.height = Math.min(parseInt(clientHeight) - 86, iFrameHeight);
			}

			// Resize if too wide
			if( slotWidth >( clientWidth - 20 ) ) {
				document.getElementById('slot' + i).style.width = clientWidth - 20;
				document.getElementById('id_ifrslot' + i).style.width = parseInt(clientWidth) - 44;
				
				
				
			}
			else if( slotWidth < 628) {
				document.getElementById('slot' + i).style.width = Math.min(clientWidth - 20, 628);
				document.getElementById('id_ifrslot' + i).style.width = Math.min(parseInt(clientWidth) - 44, 604);
			}



			document.getElementById('slot' + i).style.top = clientHeight / 2 - parseInt(document.getElementById('slot' + i).style.height) / 2 - 8;
			document.getElementById('slot' + i).style.left = clientWidth / 2 - parseInt(document.getElementById('slot' + i).style.width) / 2 - 8;	

		}

		// Hide the menu if the screen width is too small
		if(clientWidth<636) {
			if(parent.menu) {
				var waitToggleMenu = setTimeout("parent.menu.toggleMenu(true)", 1000);
			 }
		}
	}
	
	// Place the magic board in the bottom center
	document.getElementById('magicBord').style.left = clientWidth / 2 - parseInt(document.getElementById('magicBord').style.width) / 2;
	
}



function centerMenu() {

}



function resize() {

	setTimeout('placeMenus();', 0);
	setTimeout('mainLoop();', 0);

}

function popup(href, slot) {

	if(!slot) slot = 0;

	//alert('slot=' + slot + '\nhref=' + href);

	if(popups) {
		var height = parent.document.body.clientHeight - 300;
		if(!height) height = 600;
		if(height<600) height = 600;

		//width=624

		var newWindow = window.open(href,'ifrslot' + slot,'top='+(screen.height/2 - height/2)+',left='+(screen.width/2 - 362)+',width=605,height=' + height + ',resizable=no,scrollbars=yes');
		if(newWindow) newWindow.window.focus();
		else {
			alert('It seems like your browser does not accept popups. Go to game account settings to disable popups!');
		}
		
		return false;
	}
	else {
	
		window.frames['ifrslot' + slot].location  = href;
		document.getElementById('slot' + slot).style.display = 'block'; // Show it
		
		return true;
	}
}

function scroll(x, y) {
	bodyHolder.scrollLeft =x;
	bodyHolder.scrollTop =y;
}


if(!haveMovedArmy && ownArmyId > -1) {
	flashInterval = setInterval('flashArmy()', 500);
}

function flashArmy() {
	

	var color = document.getElementById('army' + armies[ownArmyId][0]).style.borderColor;
	
	//alert(color);
	
	if(color=='#000000') color = '#ffffff'
	else color = '#000000';
	
	document.getElementById('army' + armies[ownArmyId][0]).style.borderColor = color;
	
	if(haveMovedArmy) {
		document.getElementById('army' + armies[ownArmyId][0]).style.borderColor = "#000000";
		clearInterval (flashInterval);
	}
}



bodyHolder.onscroll = mainLoop;
document.onmousemove = handleMousemove;
window.onresize = resize;

