/*
Animated bugs! Script
By Mike Hall (MHall75819@aol.com)
Permission grbuged to Dynamicdrive.com to feature script in archive
For full source code, visit http://dynamicdrive.com
*/
var myimages=new Array()
function preloadimages(){
for (i=0;i<preloadimages.arguments.length;i++){
myimages[i]=new Image()
myimages[i].src=preloadimages.arguments[i]
}
}

// 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.

preloadimages("a110_files/bug1.gif",
                       "a110_files/bug2.gif",
                       "a110_files/bug3.gif",
                       "a110_files/bug4.gif",
                       "a110_files/bug5.gif",
                       "a110_files/bug6.gif",
                       "a110_files/bug7.gif",
                       "a110_files/bug8.gif" );

  //startLoadBar(images);
preloadimages( "a110_files/transparent.gif",
                       "a110_files/button19.gif",
                       "a110_files/top19_new.gif",
                       "a110_files/peg19.gif" );
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;
    mouseY = event.clientY + document.body.scrollTop;
  }
  return true;
}

var bugs = new Array(8);

function endLoadBar() {

  var i;

  // Get handles to all the bug layers.

  for (i = 0; i < bugs.length; i++) {
    bugs[i] = getLayer("bug" + (i + 1));
    if (isMinNS4)
      bugs[i].image = bugs[i].document.images["bugimg" + (i + 1)];
    if (isMinIE4)
      bugs[i].image = document.images["bugimg" + (i + 1)];
    initbug(i);
    showLayer(bugs[i]);
  }
  updatebugs();
}

function initbug(n) {

  var s, x, y;

  // Randomly place an bug 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(bugs[n]);
  if (s == 1)
    x = getWindowWidth();
  if (s == 2)
    y = -getHeight(bugs[n]);
  if (s == 3)
    y = getWindowHeight();
  x += getPageScrollX();
  y += getPageScrollY();
  moveLayerTo(bugs[n], x, y);
}

function updatebugs() {

  var i, dx, dy, theta, d;

  // Move each bug toward the mouse pointer, if she hits it, drop her back onto
  // the page randomly.

  d = 3;
  for (i = 0; i < bugs.length; i++) {

    // Find the angle between the bug and the pointer.

    dx = mouseX - getLeft(bugs[i]);
    dy = mouseY - getTop(bugs[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)
      initbug(i);

      // If not, move the bug and set the image based on angle.

    else if (theta > 23 && theta <= 68) {
        moveLayerBy(bugs[i], d, -d);
        bugs[i].image.src = "a110_files/bug8.gif";
    }
    else if (theta > 68 && theta <= 113) {
        moveLayerBy(bugs[i], 0, -d);
        bugs[i].image.src = "a110_files/bug7.gif";
    }
    else if (theta > 113 && theta <= 158) {
        moveLayerBy(bugs[i], -d, -d);
        bugs[i].image.src = "a110_files/bug6.gif";
    }
    else if (theta > 158 && theta <= 203) {
        moveLayerBy(bugs[i], -d, 0);
        bugs[i].image.src = "a110_files/bug4.gif";
    }
    else if (theta > 203 && theta <= 248) {
        moveLayerBy(bugs[i], -d, d);
        bugs[i].image.src = "a110_files/bug1.gif";
    }
    else if (theta > 248 && theta <= 293) {
        moveLayerBy(bugs[i], 0, d);
        bugs[i].image.src = "a110_files/bug2.gif";
    }
    else if (theta > 293 && theta <= 338) {
        moveLayerBy(bugs[i], d, d);
        bugs[i].image.src = "a110_files/bug3.gif";
    }
    else {
        moveLayerBy(bugs[i], d, 0);
        bugs[i].image.src = "a110_files/bug5.gif";
    }
  }

  // Set up next call.

  setTimeout('updatebugs()', 50);
  return;
}

