
if (flying_object == "ant")
{

	// These variables will hold the current mouse pointer position.
	var mouseX = 0;
	var mouseY = 0;

	// Set up event capturing.

	if (isMinNS4)	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getMousePosition;
}

function getMousePosition(e) 
{

	if (flying_object == "ant")
	{

		// Save cursor position using browser-specific code.

		  if (isMinNS4) {
		    mouseX = e.pageX;
		    mouseY = e.pageY;
		  }
		  if (isMinIE4) {
		    mouseX = event.clientX + document.body.scrollLeft;
		    mouseY = event.clientY + document.body.scrollTop;
  		  }
		  return true;
	}
}

if (flying_object == "ant")
{
	var ants = new Array();
}

function initAnts() 
{

	if (flying_object == "ant")
	{
		  var i, layer;

		  // Get handles to all the ant layers.

		  i = 0;
		  while ((layer = getLayer("ant" + (i + 1))) != null) {
			    ants[i] = layer;
			    if (isMinNS4)
			      ants[i].image = ants[i].document.images["antImg" + (i + 1)];
			    if (isMinIE4)
			      ants[i].image = document.images["antImg" + (i + 1)];
			    setAnt(i);
			    showLayer(ants[i]);
			    i++;
		  }
		  updateAnts();
	}
}

function setAnt(n) 
{
	if (flying_object == "ant")
	{

	  var s, x, y;

	  // Randomly place an ant on the window.

	  x = Math.floor(Math.random() * getWindowWidth());
	  y = Math.floor(Math.random() * getWindowHeight());
	  s = Math.floor(Math.random() * 4);
	  if (s == 0)
	    x = -getWidth(ants[n]);
	  if (s == 1)
	    x = getWindowWidth();
	  if (s == 2)
	    y = -getHeight(ants[n]);
	  if (s == 3)
	    y = getWindowHeight();
	  x += getPageScrollX();
	  y += getPageScrollY();
	  moveLayerTo(ants[n], x, y);
	}
}

function updateAnts() 
{
	if (flying_object == "ant")
	{

	  var i, dx, dy, theta, d;

	  // Move each ant toward the mouse pointer, if she hits it, drop her back onto
	  // the page randomly.

	  d = 6;
	  for (i = 0; i < ants.length; i++) 
	  {

	    // Find the angle between the ant and the pointer.

	    dx = mouseX - getLeft(ants[i]);
	    dy = mouseY - getTop(ants[i]);
	    theta = Math.round(Math.atan2(-dy, dx) * 180 / Math.PI);
	    if (theta < 0)
	      theta += 360;

	    // Hit the pointer?

	    if (Math.abs(dx) < d && Math.abs(dy) < d)
	      setAnt(i);

	    // If not, move the ant and set the image based on angle.

	    else if (theta > 23 && theta <= 68) {
	        moveLayerBy(ants[i], d, -d);
	        ants[i].image.src = ant_ne.src;
	    }
	    else if (theta > 68 && theta <= 113) {
	        moveLayerBy(ants[i], 0, -d);
	        ants[i].image.src = ant_n.src;
	    }
	    else if (theta > 113 && theta <= 158) {
	        moveLayerBy(ants[i], -d, -d);
	        ants[i].image.src = ant_nw.src;
	    }
	    else if (theta > 158 && theta <= 203) {
        	moveLayerBy(ants[i], -d, 0);
	        ants[i].image.src = ant_w.src;
	    }
	    else if (theta > 203 && theta <= 248) {
	        moveLayerBy(ants[i], -d, d);
	        ants[i].image.src = ant_sw.src;
	    }
	    else if (theta > 248 && theta <= 293) {
	        moveLayerBy(ants[i], 0, d);
	        ants[i].image.src = ant_s.src;
	    }
	    else if (theta > 293 && theta <= 338) {
	        moveLayerBy(ants[i], d, d);
	        ants[i].image.src = ant_se.src;
	    }
	    else {
	        moveLayerBy(ants[i], d, 0);
	        ants[i].image.src = ant_e.src;
	    }
  	}

	// Set up next call.

	setTimeout('updateAnts()', 50);
	return;

     }
}
