// <script language="JavaScript">


var myimages=new Array()
function preloadimages(){
for (i=0;i<preloadimages.arguments.length;i++){
myimages[i]=new Image()
myimages[i].src=preloadimages.arguments[i]
}
}
preloadimages("images/antdl.gif",
                       "images/antdn.gif",
                       "images/antdr.gif",
                       "images/antlt.gif",
                       "images/antrt.gif",
                       "images/antul.gif",
                       "images/antup.gif",
                       "images/antur.gif");


// 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 init() {

  // Start loading images.

  //startLoadBar(images);
endLoadBar()
}

function getMousePosition(e) {

  // Save cursor position using browser-specific code.

  if (isMinNS4) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  }
  if (isMinIE4) {
    mouseX = event.clientX + document.body.scrollLeft - 50;
    mouseY = event.clientY + document.body.scrollTop;
  }
  return true;
}

var ants = new Array(8);

function endLoadBar() {

  var i;

  // Get handles to all the ant layers.

  for (i = 0; i < ants.length; i++) {
    ants[i] = getLayer("ant" + (i + 1));
    if (isMinNS4)
      ants[i].image = ants[i].document.images["antimg" + (i + 1)];
    if (isMinIE4)
      ants[i].image = document.images["antimg" + (i + 1)];
    initAnt(i);
    showLayer(ants[i]);
  }
  updateAnts();
}

function initAnt(n) {

  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() {

  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 = 3;
  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)
      initAnt(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 = "images/antur.gif";
    }
    else if (theta > 68 && theta <= 113) {
        moveLayerBy(ants[i], 0, -d);
        ants[i].image.src = "images/antup.gif";
    }
    else if (theta > 113 && theta <= 158) {
        moveLayerBy(ants[i], -d, -d);
        ants[i].image.src = "images/antul.gif";
    }
    else if (theta > 158 && theta <= 203) {
        moveLayerBy(ants[i], -d, 0);
        ants[i].image.src = "images/antlt.gif";
    }
    else if (theta > 203 && theta <= 248) {
        moveLayerBy(ants[i], -d, d);
        ants[i].image.src = "images/antdl.gif";
    }
    else if (theta > 248 && theta <= 293) {
        moveLayerBy(ants[i], 0, d);
        ants[i].image.src = "images/antdn.gif";
    }
    else if (theta > 293 && theta <= 338) {
        moveLayerBy(ants[i], d, d);
        ants[i].image.src = "images/antdr.gif";
    }
    else {
        moveLayerBy(ants[i], d, 0);
        ants[i].image.src = "images/antrt.gif";
    }
  }

  // Set up next call.

  setTimeout('updateAnts()', 50);
  return;
}

// </script>