var isNetscape = (document.layers) ? 1:0;
var isIE = (document.all) ? 1:0;
var canDHTML = isNetscape || isIE;

var initialAngle = Math.random() * Math.PI * 2;
var angle = initialAngle;
var leftMargin = 0;
var topMargin = 0;
var isMoving = false;
var isMouseOver = false;

var frameDelay = 100;
var minimumSpeed = 0;
var initialSpeed = 20;
var acceleration = -2;

function setUpSplat()
{
	windowX = windowWidth();
	windowY = windowHeight();

	if (canDHTML)
	{
		window.onresize = checkWindowSize;
        if (isNetscape)
		{
                button = document.splatDiv;
                button.xpos = parseInt(button.left);
                button.ypos = parseInt(button.top);
        }
        if (isIE)
		{
                button = splatDiv.style;
                button.xpos = parseInt(button.posLeft);
                button.ypos = parseInt(button.posTop);
        }
	setMovementParameters();
	}
}

function checkWindowSize()
{
	setMovementParameters();

	// got this from www.terrarium.ml.html who credits www.webmonkey.ocm
	// I assume that NS 4 sometimes fails to update window.innerheight and .innerwidth on resize?
	// if this occurs, reload the page

	if ((windowX == windowWidth()) && (windowY == windowHeight()))
		this.location = this.history.go(0);
}

function setMovementParameters()
{
	if (button.clip)
	{
		rightMargin = leftMargin + bounceWidth() - button.clip.right;
		bottomMargin = topMargin + bounceHeight() - button.clip.bottom;
	}
	else
	{
		if ((button.posWidth>=0) && (button.posHeight>=0))
		{
			rightMargin = leftMargin + bounceWidth() - button.posWidth;
			bottomMargin = topMargin + bounceHeight() - button.posHeight;
		}
	}
}

function bounceWidth()
{
	return (documentWidth() > windowWidth()) ? documentWidth() : windowWidth();
}

function bounceHeight()
{
	return (documentHeight() > windowHeight()) ? documentHeight() : windowHeight();
}

function documentWidth()
{
	if (isNetscape)
	{
		return document.width;
	}
	else
	{
		if (isIE)
		{
			return document.body.scrollWidth;
		}
		else
		{
			return null;
		}
	}
}

function documentHeight()
{
	if (isNetscape)
	{
		return document.height;
	}
	else
	{
		if (isIE)
		{
			return document.body.scrollHeight;
		}
		else
		{
			return null;
		}
	}
}

function windowWidth()
{
	if (isNetscape)
	{
		return window.innerWidth;
	}
	else
	{
		if (isIE)
		{
			return document.body.clientWidth;
		}
		else
		{
			return null;
		}
	}
}

function windowHeight()
{
	if (isNetscape)
	{
		return window.innerHeight;
	}
	else
	{
		if (isIE)
		{
			return document.body.clientHeight;
		}
		else
		{
			return null;
		}
	}
}

function splatMouseOver()
{
    isMouseOver = true;
	if (isInitialized)
	{
        runAway();
	}
}     

function splatMouseOut()
{
	isMouseOver = false;
}

function runAway()
{
	if (canDHTML && !isMoving)
	{
		isMoving = true;
		angle = Math.random() * Math.PI * 2;
		move(initialSpeed,angle);
	}
}

function move(speed,angle)
{
	if (Math.abs(speed) <= minimumSpeed)
	{
		isMoving = false;
		shouldRunAway();
	}
	else
	{
		button.xpos += speed * Math.cos(angle);
        button.ypos += speed * Math.sin(angle);

        if (button.xpos <= leftMargin)
        {
			angle += Math.PI;
			button.xpos = leftMargin;
        }

        if (button.xpos >= rightMargin)
        {
			angle += Math.PI;
			button.xpos = rightMargin;
        }

        if (button.ypos <= topMargin)
        {
			angle += Math.PI;
			button.ypos = topMargin;
		}

        if (button.ypos >= bottomMargin)
        {
			angle += Math.PI;
			button.ypos = bottomMargin;
        }

        button.left = button.xpos;
        button.top = button.ypos;
        speed += acceleration;
        setTimeout("move(" + speed + "," + angle + ")",frameDelay);
	}
}

function shouldRunAway()
{
	if(isMouseOver)
	{
		runAway();
	}
}
