selectedUnitID = 0;
var attackWaitTime = 10;
var movementWaitTime =0;

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

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

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;
	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 clickTile(x, y)
{
	
	//
	// Did they click an object
	//
	objItem = getObjectAt(x,y);
	
	if(objItem!= 0)
	{
		if(objItem.getAttribute("triggerCode") == "Prize"
			&& selectedUnitID > 0
			&& getDistanceFromUnit(selectedUnitID, x, y) <= 2)
		{
			roll = getRandomNumber(1, 3);
			removeObject(objItem.getAttribute("objectID"));
			
			var selectUnit = document.getElementById("unit_" + selectedUnitID);	
			
			if(roll == 1)
			{
				showUnitMsg(selectedUnitID, "+25 HP");
				life = parseInt(selectUnit.getAttribute("life"));
				selectUnit.setAttribute("life", life+25);
			}
			
			if(roll == 2)
			{
				showUnitMsg(selectedUnitID, "+10 damage");
				damage = parseInt(selectUnit.getAttribute("damage"));
				selectUnit.setAttribute("damage", damage+5);
			}


			if(roll == 3)
			{
				showUnitMsg(selectedUnitID, "+1 range");
				range = parseInt(selectUnit.getAttribute("range"));
				selectUnit.setAttribute("range", range+1);
			}			
		}
	}
	
	//
	// 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)
		{
			//alert("Moving Unit");
			moveUnit(selectedUnitID, x , y);
			clearMovement(selectedUnitID);
			setWaitTime(selectedUnitID, movementWaitTime);
		}
	}

}

function unitClicked(objUnit, x, y)
{

	showDisplay(objUnit.getAttribute("unitID"));
	
	//alert("here");
	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("ownerID") != 1)
		{
			//alert(getNearestEnemy(selectedUnitID));
			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(isWithinTargetRange(attackerID, targetX, targetY) == "Y" && attacker.getAttribute("waitTime") <= 0)
	{
		damage = attacker.getAttribute("damage");
		life = defender.getAttribute("life");
		defender.setAttribute("life",life-damage);
		showUnitMsg(defenderID, "-" + damage);
		setWaitTime(attackerID, attackWaitTime);
		
		if(parseInt(life) <= parseInt(damage))
		{
			removeUnit(defenderID);
			addPrize(targetX, targetY);
		}
	}
	else
	{
		if(selectedUnitID == attackerID)
		{
			alert("Unable to attack.");
		}
	}
}

function fightComplete()
{
	if (xmlHttp.readyState==4)
	{ 
		updateMap();
		ajaxDone();  //MAKE SURE YOU CALL THIS!
	}
}

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

numPrizes=0;
function addPrize(x, y)
{
	prize=new Object();
	prize.objectID = 100+numPrizes;	//must be unique
	prize.imageName='prize';
	prize.name = 'Prize';
	prize.x = x;
	prize.y = y;
	prize.triggerCode = 'Prize';
	addTileObject(prize);
	numPrizes++;


}