var uniqueobjid = 0;

function getNextObjId()
{
	return uniqueobjid++;
}

function setupHealthBar(objname, start_hp, start_moves)
{
	//TODO: implement "moves" bar (vertical?)
	var obj = document.getElementById(objname);
	
	var colbox = document.createElement("div");
	colbox.id = "hpbar_" + objname;
	colbox.style.backgroundColor=colorFromHealth(start_hp, start_hp);
	colbox.style.fontSize = '3px';
	colbox.style.position='relative';
	colbox.style.left= '0px';
	colbox.style.top= '-2px';	
	colbox.style.height='3px';
	colbox.style.width=Math.ceil(start_hp / 3) + 'px';	
	colbox.setAttribute("current_hp", start_hp);
	colbox.setAttribute("start_hp", start_hp);
			
	obj.appendChild(colbox);
}

function colorFromHealth(hp, start_hp)
{
	blendfactor = Math.abs(hp / start_hp);
	if(blendfactor > 1.0)
		blendfactor = 1.0;
	red = 127 + Math.floor((1.0 - blendfactor) * 128.0);
	green = Math.floor(blendfactor * 128.0);
	blue = green;
	
	if(red < 16)
		red = "0" + red.toString(16);
	else
		red = red.toString(16);
		
	if(green < 16)
		green = "0" + green.toString(16);
	else
		green = green.toString(16);
		
	if(blue < 16)
		blue = "0" + blue.toString(16);
	else
		blue = blue.toString(16);
	
	return "#" + red + green + blue;
}

function changeHealthBar(objname, hp_change)
{
	var barobj = document.getElementById("hpbar_" + objname);
	if(barobj != null)
	{
		curhp = barobj.getAttribute("current_hp");
		start_hp = barobj.getAttribute("start_hp");
		curhp = parseFloat(curhp) + parseFloat(hp_change);
		barobj.style.backgroundColor=colorFromHealth(Math.floor(curhp), parseInt(start_hp));
		
		newwidth = Math.ceil(curhp / 3);
		if(newwidth < 0)
			newwidth = 0;
		
		barobj.style.width=newwidth + "px";
		barobj.setAttribute("current_hp", curhp);
	}
}

function animateNumber_start(objname, num_value)
{
	animateNumber(objname, num_value, getNextObjId(), 0);
}

function animateNumber(objname, num_value, num_id, height)
{	
	if(height == 0)
	{
		var obj = document.getElementById(objname);
		
		var num = document.createElement("span");
		num.id = "animatedNumber" + num_id;
		if(num_value > 0)
		{
			if(num_value > 10000) //at boost
			{
				hp_steps = 0;
				num.style.color = "#00dddd";
				num_value -= 10000;
			}
			else //hp boost
			{
				hp_steps = num_value / 12;
				num.style.color = "#00dd00";				
			}
				
			obj.style.backgroundColor = "#ffffff";
		}
		else //hp damage
		{
			hp_steps = num_value / 12;
			//display positive but in red color
			num_value = Math.abs(num_value); 
			num.style.color = "#dd0000";
			obj.style.backgroundColor = "#000000";
		}
		num.innerHTML = "<b>" + num_value + "</b>";
		num.style.fontFamily = "sans-serif";
		num.style.fontSize = "20px";
		num.style.fontWeight = "bold";
		num.style.position = "absolute";
		if(num_value > 9)
			num.style.left = 5 + "px";
		else
			num.style.left = 10 + "px";
		num.style.top = (10-height) + "px";
		num.setAttribute("hp_steps", hp_steps);
		obj.appendChild(num);
	}
	else if(height < 12)
	{
		var obj = document.getElementById(objname);
		var num = document.getElementById("animatedNumber" + num_id);
		num.style.top = (10-height) + "px";
		hp_steps = num.getAttribute("hp_steps");
	}
		
	if(height < 12)
	{
		if(hp_steps != 0)
			changeHealthBar(objname, hp_steps);
			
		height+=1;
		setTimeout('animateNumber("' + objname + '", ' + num_value + ', ' + num_id + ', ' + height + ')', 50);
	}
	else
	{
		var obj = document.getElementById(objname);
		var num = document.getElementById("animatedNumber" + num_id);
		obj.removeChild(num);
		obj.style.backgroundColor = "";
	}
}

function animateMove_start(srcname, destname) 
{	
	var srcimage = document.getElementById(srcname);
	//var srcposx = parseInt(srcimage.style.left);
	//var srcposy = parseInt(srcimage.style.top);
	
	var srcposx = srcimage.getAttribute("x_pos");
	var srcposy = srcimage.getAttribute("y_pos");	
	
/*	var destimage = document.getElementById(destname);
	var destposx = parseInt(destimage.style.left);
	var destposy = parseInt(destimage.style.top);
	var distx = destposx - srcposx;
	var disty = destposy - srcposy;
	var steppingx = distx / 40;
	var steppingy = disty / 40;
*/	

	
	//TODO: setup z-topmost
	animateMove(srcposx, srcposy, srcname, destname, 0);
}

function blend(destpos, srcpos, blendfactor_zero_is_source_one_is_dest)
{
	return destpos * blendfactor_zero_is_source_one_is_dest + (1.0 - blendfactor_zero_is_source_one_is_dest) * srcpos;
}

function animateMove(srcposx, srcposy, srcname, destname, hin)
{
	var srcimage = document.getElementById(srcname);
	var destimage = document.getElementById(destname);
	
//	if(destimage == null)
//		alert("can't find destname:" + destname);
	
	if(hin < 1) //move towards target (enemy, etc.)
	{
		//var dist = Math.abs(parseInt(srcimage.style.left) - parseInt(destimage.style.left)) + Math.abs(parseInt(srcimage.style.top) - parseInt(destimage.style.top));
		
		hin+=0.25;
		srcimage.style.left = blend(parseInt(destimage.style.left), parseInt(srcimage.style.left), hin) + "px";
		srcimage.style.top  = blend(parseInt(destimage.style.top),  parseInt(srcimage.style.top),  hin) + "px";
	}
	else //return to starting position
	{
		//var dist = Math.abs(parseInt(srcimage.style.left) - srcposx) + Math.abs(parseInt(srcimage.style.top) - srcposy);
		
		hin+=0.25;
		zuruck = hin - 1.0;
		srcimage.style.left = blend(parseInt(srcposx), parseInt(srcimage.style.left), zuruck) + "px";
		srcimage.style.top  = blend(parseInt(srcposy), parseInt(srcimage.style.top),  zuruck) + "px";
	}
	
	if(hin < 2)
	{
		var newcall = 'animateMove("' + srcposx + '", "' + srcposy + '", "' + srcname + '", "' + destname + '" ,' + hin + ')';
		setTimeout(newcall,49);
	}
	else
	{
		//TODO: deal with z-reseting
	}
}

function highlightAction(targetstring, scroll)
{
	document.getElementById(targetstring).style.backgroundColor = "#666666";
	document.getElementById("loglist").scrollTop = scroll;
}

function getObjLeft(srcname)
{
	return parseInt(document.getElementById(srcname).style.left);
}
function getObjTop(srcname)
{
	return parseInt(document.getElementById(srcname).style.top);
}

function animateMoveTo(srcname, targetposx, targetposy, hin)
{
	if(hin == null)
		hin = 0.0;
		
	if(hin <= 1.0)
	{	
		hin += 0.125;
		var srcimage = document.getElementById(srcname);
		
		srcimage.setAttribute("x_pos", targetposx);
		srcimage.setAttribute("y_pos", targetposy);
		
		srcimage.style.left= blend(targetposx, parseInt(srcimage.style.left), hin) + "px";
		srcimage.style.top = blend(targetposy, parseInt(srcimage.style.top), hin) + "px";	
		setTimeout('animateMoveTo("' + srcname + '",' + targetposx + ',' + targetposy + ',' + hin + ')',48);
	}
}

function updateScore(labelname, side, score)
{
	if(side == 0)
		document.getElementById(labelname).innerHTML = "Enemy Guard's deaths: " + score;
	else
		document.getElementById(labelname).innerHTML = "Your Guard's deaths: " + score;
}
/*
function setupUnappeared(srcname)
{
	var srcobj = document.getElementById(srcname);
	alpha = 0;
	srcobj.style.opacity = alpha;
	srcobj.style.filter = 'alpha(opacity=' + (alpha * 100) + ')';		
}
function animateAppear(srcname)
{
	var srcobj = document.getElementById(srcname);
	alpha = srcobj.style.opacity;
	alpha = alpha + 0.3;
	srcobj.style.opacity = alpha;
	srcobj.style.filter = 'alpha(opacity=' + (alpha * 100) + ')';		
	if(alpha < 1.0)
		setTimeout('animateAppear("' + srcname + '");',25);
}
*/
