selectedUnitID = 0;
var attackWaitTime = 10;
var movementWaitTime = 5;
var globalDestX = 0;
var globalDestY = 0;

function performTraining(unitID, trainingID)
{
	url="listTraining.php?action=performTraining&trainingID=" + trainingID + "&unitID=" + unitID;
	requestData(url, refreshUnitInfo);
}



function spawn()
{
	url = "actions.php?action=spawn&mapID=" + getMapID();
	sendData(url);

	moveUnit(selectedUnitID, globalDestX , globalDestY);
	saveMove(selectedUnitID, globalDestX , globalDestY);
	clearMovement(selectedUnitID);
	setWaitTime(selectedUnitID, movementWaitTime);
	
}

function overTile(x, y)
{
	tile = getMouseOverTile(x, y);
	tile.style.visibility = "visible";
	showDisplay(getUnitIDAt(x, y));
	autoLogout = getGameVariable("autoLogout");;
}

function offTile(x, y)
{
	tile = getMouseOverTile(x, y);
	tile.style.visibility = "hidden";
	hideDisplay();
}

function closePopup()
{
	document.getElementById('popupPanel').style.visibility='hidden';
}

function clickTile(x, y)
{
	//
	// Did they click a unit?
	//
	objUnit = getUnitAt(x, y);
	if(objUnit != 0)
	{
		unitClicked(objUnit, x, y);
		return;
	}
	
	//
	// Is there a unit selected?
	//
	if(selectedUnitID > 0)
	{
		//can the unit move to the tile clicked?
		tile = document.getElementById("tile_"+ x + "_" + y + "_movement");
		var selectUnit = document.getElementById("unit_" + selectedUnitID);
		if(tile.getAttribute("canMoveTo") == "Y" && isWalkable(x, y) == 'Y' && selectUnit.getAttribute("waitTime") <= 0)
		{
			
			//excute any special functions
			tmpObj = getObjectAt(x,y);
			if(tmpObj != 0)
			{
				if(tmpObj.getAttribute("onContact").length > 1)
				{
					//alert("'" + tmpObj.getAttribute("onContact").length + "'");
					globalDestX = x;
					globalDestY = y;
					eval(tmpObj.getAttribute("onContact"));
					return;
				}
			}
			
			
			if(isWalkable(x,y) == 'Y')
			{
				moveUnit(selectedUnitID, x , y);
				saveMove(selectedUnitID, x , y);
				clearMovement(selectedUnitID);
				setWaitTime(selectedUnitID, movementWaitTime);
			}

		}
	}

}

function warp(mapID, x, y)
{
	if(selectedUnitID > 0)
	{
		url="actions.php?action=moveToMap";
		url+= "&mapID=" + mapID;
		url+= "&x= " + x;
		url+= "&y= " + y;
		url+= "&unitID= " + selectedUnitID;
		sendData(url);
		showGameMsg("Moving to new Map ... ");
		setTimeout("window.location.href='play.php?mapID=" + mapID + "';", 2000);
		
	}
}

function openStore()
{
	if(selectedUnitID > 0)
	{
		url="store.php?unitID= " + selectedUnitID;
		loadDiv("popupPanelContents", url);
		document.getElementById("popupPanel").style.visibility = "visible";
		
	}
}


function train(tag)
{
	if(selectedUnitID > 0)
	{
		url="listTraining.php?unitID= " + selectedUnitID + "&tag=" + tag;
		loadDiv("popupPanelContents", url);
		document.getElementById("popupPanel").style.visibility = "visible";
		
	}
}

function openHerbalist()
{
	if(selectedUnitID > 0)
	{
		url="herbalist.php?unitID= " + selectedUnitID;
		loadDiv("popupPanelContents", url);
		document.getElementById("popupPanel").style.visibility = "visible";
		
	}
}


function store(tag)
{
	if(selectedUnitID > 0)
	{
		url="storeEquipment.php?tag=" + tag + "&unitID= " + selectedUnitID;
		loadDiv("popupPanelContents", url);
		document.getElementById("popupPanel").style.visibility = "visible";
		
	}
}

function selectUnit(unitID)
{
	//
	//de-select any old selected unit
	//
	if(selectedUnitID > 0)
	{
		objUnit = document.getElementById("unit_" + selectedUnitID);
		x = parseInt(objUnit.getAttribute("tileX"));
		y = parseInt(objUnit.getAttribute("tileY"));
		tile = document.getElementById("tile_" + x + "_" + y + "_selected");
		if(tile != null)
		{
			tile.style.visibility = "hidden";
		}
		
		clearMovement(selectedUnitID);
		clearTarget(selectedUnitID);
	}
	
	//
	// select new unit
	//
	selectedUnitID = unitID;
	var objUnit = document.getElementById("unit_" + unitID);
	x = parseInt(objUnit.getAttribute("tileX"));
	y = parseInt(objUnit.getAttribute("tileY"));
	tile = document.getElementById("tile_" + x + "_" + y + "_selected");
	tile.style.visibility = "visible";
	
	showMovementRange(objUnit);
	showTargetRange(objUnit);
	
	
	
}

function unitClicked(objUnit, x, y)
{
	
	showDisplay(objUnit.getAttribute("unitID"));
	
	debug("isControllable:" + objUnit.getAttribute("isControllable") );
	debug("life:" + objUnit.getAttribute("life") );
	debug("waitTime:" + objUnit.getAttribute("waitTime") );
	
	if(objUnit.getAttribute("isControllable") == "Y" 
		&& objUnit.getAttribute("life") >= 0
		&& objUnit.getAttribute("waitTime") <= 0)
	{
		
		selectUnit(objUnit.getAttribute("unitID"));
	}
	else
	{
		//
		//enemy unit clicked...
		//do we launch an attack?
		//
		if(selectedUnitID > 0 && objUnit.getAttribute("isControllable") != "Y")
		{
			if(canTarget(x, y) == "Y")
			{
				attack(selectedUnitID, objUnit.getAttribute("unitID"));
			}
		}
	}
}

function attack(attackerID, defenderID)
{
	attacker = document.getElementById("unit_" + attackerID);
	defender = document.getElementById("unit_" + defenderID);
	targetX = defender.getAttribute("tileX");
	targetY = defender.getAttribute("tileY");
	
	
	if(attacker.getAttribute("waitTime") <= 0)
	{
		//setWaitTime(attackerID, attackWaitTime);
		var url = "fight.php?attackerID=" + attackerID;
		url+= "&defenderID=" + defenderID;
		requestData(url, fightComplete);
		debug("Fighting");
	}
	else
	{
		showUnitMsg(attackerID, "i can not");
		debug("canTarget:" + canTarget(targetX, targetY) );
		debug("WaitTime:" + attacker.getAttribute("waitTime"));
	}
}

function fightComplete()
{
	if (xmlHttp.readyState==4)
	{ 
		data = xmlHttp.responseText.split(",");
		defenderID = data[0];
		attackerID = data[1];
		defMsg = data[2];
		attMsg = data[3];
		defLife = data[4];
		waitTime = data[5];
		attAnima = data[6];
		fullMsg = data[7];

		setWaitTime(attackerID, waitTime);
		
		if(fullMsg == "")
		{
			showUnitMsg(defenderID, defMsg);
			setTimeout("showUnitMsg(" + attackerID + ", '" + attMsg + "')", 2000);
		}
		else
		{
			showGameMsg(fullMsg);
			setTimeout("hideGameMsg()",5000);
		}

		if(attAnima != "")
		{
			showAttackAnimation(attackerID, 2, attAnima);
		}
		defUnit = document.getElementById("unit_" + defenderID);
		defUnit.setAttribute("life",defLife);		

		if(defLife < 0)
		{
			removeUnit(defenderID);
			refreshUnitDisplay(attackerID);
		}
						
		ajaxDone();  //MAKE SURE YOU CALL THIS!
		
	}
}

function doneMoving(unitID)
{
	//
	//must be defined
	//called after unit finishes moving
	//
	if(selectedUnitID == unitID)
	{
		selectUnit(selectedUnitID);
	}
}

function saveMove(unitID, x, y)
{
	var url = "actions.php?action=move&unitID=" + unitID;
	url+= "&x=" + x;
	url+= "&y=" + y;
	sendData(url);
	//alert(url);
}

