window.onload = init;
window.onresize = reInit;




function init()
{
	bounceDoug();
	setTimeout("taunt()", 1000);
	
	document.onkeydown = ericPunch;
	document.onkeyup = ericPunch;
	document.onmousedown = ericPunch;
	document.onmouseup = ericPunch;
	
	return true;
}




function reInit()
{
	window.location = window.location;
	
	return true;
}




function bounceDoug()
{
	var doug = document.getElementById("doug");
	var dougInner = document.getElementById("dougInner");
	var explosion = document.getElementById("explosion");
	var currX = doug.offsetLeft;
	var currY = dougInner.offsetTop;
	var incrementX = 5;
	var incrementY = 2;
	
	explosion.className = "";
	
	if (typeof doug.directionX == "undefined")
	{
		doug.directionX = "right";
		doug.directionY = "down";
		doug.originalX = currX;
		doug.originalY = currY;
	}
	
	if (doug.directionX == "right")
	{
		if (currX > doug.originalX + 150)
		{
			incrementX = -incrementX;
			doug.directionX = "left";
		}
	}
	else
	{
		if (currX > doug.originalX)
		{
			incrementX = -incrementX;
		}
		else
		{
			doug.directionX = "right";
		}
	}
	
	if (doug.directionY == "down")
	{
		if (currY > doug.originalY + 10)
		{
			incrementY = -incrementY;
			doug.directionY = "up";
		}
	}
	else
	{
		if (currY > doug.originalY)
		{
			incrementY = -incrementY;
		}
		else
		{
			doug.directionY = "down";
		}
	}
	
	moveIncrement(doug, incrementX, 0);
	moveIncrement(dougInner, 0, incrementY);
	
	doug.moveTimer = setTimeout("bounceDoug()", 35);
	
	return true;
}




function moveIncrement(target, moveX, moveY)
{
	var currX = target.offsetLeft;
	var currY = target.offsetTop;
	
	target.style.left = currX + moveX + "px";
	target.style.top = currY + moveY + "px";
	
	return true;
}




function ericPunch(event)
{
	var doug = document.getElementById("doug");
	var eric = document.getElementById("eric");
	
	if (typeof event == "undefined")
	{
		event = window.event;
	}
	
	if (event.type == "mousedown" || event.type == "keydown")
	{
		eric.className = "punch";
		
		if (doug.offsetLeft < doug.originalX + 50)
		{
			hitDoug();
		}
	}
	else
	{
		eric.className = "";
	}
	
	return true;
}




function hitDoug()
{
	var doug = document.getElementById("doug");

	if (doug.moveTimer != null)
	{
		clearTimeout(doug.moveTimer);
		doug.moveTimer = null;
		
		var explosion = document.getElementById("explosion");

		explosion.style.left = doug.offsetLeft - 94 + "px";
		explosion.style.top = doug.offsetTop - 45 + "px";
		explosion.className = "explode";
		
		if (typeof doug.hits == "undefined")
		{
			doug.hits = 1;
		}
		else
		{
			doug.hits++;
		}
		
		if (doug.hits > 5)
		{
			doug.hits = 0;
			doug.className = "";
		}		
		else if (doug.hits > 4)
		{
			doug.className = "russ";
		}		
		else if (doug.hits > 2)
		{
			doug.className = "bruised";
		}

		setTimeout("bounceDoug()", 1000);
	}
	
	return true;
}




function taunt()
{
	var doug = document.getElementById("doug");
	var speechBubble = document.getElementById("speechBubble");
	var speechBubbleInner2 = document.getElementById("speechBubbleInner2");
	var taunts = ["You call that a closing tag!?!?!", "I <em>float</em> like a butterfly and <em>overflow</em> like a bee!", "Your mumma uses absolute font sizing!"];
	
	if (typeof speechBubble.counter == "undefined" || speechBubble.counter > 2)
	{
		speechBubble.counter = 0;
	}
	
	speechBubbleInner2.innerHTML = taunts[speechBubble.counter++];
	speechBubble.style.left = doug.offsetLeft + "px";
	speechBubble.style.top = doug.offsetTop + "px";
	
	speechBubble.className = "speaking";
	
	setTimeout("hideTaunt()", 3000);
	speechBubble.speakTimer = setTimeout("taunt()", 5000);
	talkDoug();
	
	return true;
}




function hideTaunt()
{
	var speechBubble = document.getElementById("speechBubble");
	
	speechBubble.className = "";
	
	stopTalkDoug();
	
	return true;
}




function talkDoug()
{
	var doug = document.getElementById("doug");
	var dougMouth = document.getElementById("dougMouth");
	
	if (dougMouth.className == "" && doug.className != "russ" && !dougMouth.stop)
	{
		dougMouth.className = "talking";
	}
	else
	{
		dougMouth.className = "";
	}
	
	if (dougMouth.stop)
	{
		dougMouth.stop = false;
	}
	else
	{
		dougMouth.moveTimer = setTimeout("talkDoug()", 250);
	}
	

	return true;
}



function stopTalkDoug()
{
	var dougMouth = document.getElementById("dougMouth");
	
	dougMouth.stop = true;
	
	return true;
}