//--------------------------------------------------------------------------
//
//                   COMMAND HANDLER
//
// This file explicitly handles new commands from any
// player.
//
//--------------------------------------------------------------------------


function performCommand(command, dataString) 
{ 
	var data = dataString.split(",");
	
	commandID = data[0];
	command = data[1];
	userID = data[2];
	sentBy = data[3];
	sentByName = data[4];
	turnID = data[5];
	unitID = data[6];
	x = data[7];
	y = data[8];
	damage = data[9];
	life = data[10];
	waitTime = data[11];
	loserID = data[12];
	soundToPlay = data[13];
	popUpMsg = data[14];
	showImage = data[15];
	isOwner = data[16];
	
	if(command == "move")
	{
		//alert("Moving " + unitID + "; " + x + "," + y);
		startAnimateUnit(unitID, x, y);
		hideWaitTime(unitID);	
	}
	
	if(command == "damage")
	{
		//alert(showImage);
		showUnitAnimation(unitID, "images/actionImgs/animation_" + showImage + ".gif");
		//alert(damage);
		showUnitMsg(unitID, damage);
		updateUnits();
	}

	if(command == "attack")
	{
		showUnitMsg(unitID, getRandomAttackMsg());
		showUnitAnimation(unitID, "images/actionImgs/animation_attacking.gif");
	}

	if(command == "cast")
	{
		//alert(showImage);
		showUnitAnimation(unitID, "images/actionImgs/animation_" + showImage + ".gif");
		//alert(damage);
		showUnitMsg(unitID, damage);
		updateUnits();
	}
	
	if(command == "cleared")
	{
		displayCompleted();
	}
	
	if(command == "dead")
	{
		killUnit(unitID);
	}
	
	if(command == "leave" && isOwner != 'Y')
	{
		killUnit(unitID);
	}
	
	if(command == "spawn")
	{
		//alert("Spawning");
		if(isOwner != "Y")
		{
			deployNewUnit(unitID, x, y);
		}
	}
}

function getRandomAttackMsg()
{
	r = Math.floor(Math.random()*8);
	//alert(r);
	switch (r)
	{
		case 0:
			return "tag, your it";
			break;
		case 1:
			return "fear me";
			break;
		case 2:
			return "death & honor";
			break;
		case 3:
			return "why you";
			break;
		case 4:
			return "kill";
			break;
		default : return "he he";
	}
}

var lastShowTargetUnitID= 0;
function doneMoving(unitID)
{
	showWaitTime(unitID);	
}


//
// This will be called whenever a unit is clicked
// do not change the parameter set or it will 
// cause an error.
//
function friendlyUnitClicked(objUnit, x, y)
{
	setDisplay(objUnit.getAttribute("unitID"));
	if(objUnit.getAttribute("waitTime") <= 0)
	{
		selectedUnitID = unitID;
		selectedFriendlyUnitID = unitID;
		showUnitMenu();
	}

}

function enemyUnitClicked(objUnit, x, y)
{
	setDisplay(objUnit.getAttribute("unitID"));	
	
	if(canTarget(objUnit))
	{
		showUnitMsg(selectedFriendlyUnitID, getRandomAttackMsg());
		showNormal(selectedFriendlyUnitID);	
		attemptToTarget(selectedFriendlyUnitID,objUnit.getAttribute("unitID")); 
		//alert("Done");
	}	
}

function menuClicked(menuCommand)
{
	if(menuCommand == "movement")
	{
		//if we were showing another unit's movement,
		//lets clear it
		if(lastShowMoveUnitID > 0)
		{
			showNormal(lastShowMoveUnitID);	
		}

		//show movement for the clicked unit
		showMovement(selectedFriendlyUnitID);	
	}

	if(menuCommand == "target")
	{
		if(lastShowTargetUnitID > 0)
		{
			showNormal(lastShowTargetUnitID);	
		}
		selectedUnitID = unitID;
		showNormal(selectedFriendlyUnitID);
		showTarget(selectedFriendlyUnitID);
		
	}	
	
	closeUnitMenu();
}

function confirmCast(actionID, unitID)
{
	u = document.getElementById("unit_" + unitID);
	castXmlHttp=GetXmlHttpObject();

	if (castXmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	url = "actions/castSpell.php?action=cast&actionID=" + actionID + "&unitID=" + unitID;
	//document.write(url);
	//castXmlHttp.onreadystatechange=cast_Complete;
	castXmlHttp.open("GET",url,true)
	castXmlHttp.send(null);
	
	closeUnitMenu();

}








