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("TrolleyCursors/BagTrolleydl1.gif",

			"TrolleyCursors/BagTrolleydl2.gif",

            "TrolleyCursors/BagTrolleydn1.gif",

            "TrolleyCursors/BagTrolleydn2.gif",

            "TrolleyCursors/BagTrolleydr1.gif",

            "TrolleyCursors/BagTrolleydr2.gif",

            "TrolleyCursors/BagTrolleylt1.gif",

            "TrolleyCursors/BagTrolleylt2.gif",

            "TrolleyCursors/BagTrolleyrt1.gif",

            "TrolleyCursors/BagTrolleyrt2.gif",

            "TrolleyCursors/BagTrolleyul1.gif",

            "TrolleyCursors/BagTrolleyul2.gif",

            "TrolleyCursors/BagTrolleyup1.gif",

            "TrolleyCursors/BagTrolleyup2.gif",

            "TrolleyCursors/BagTrolleyur1.gif",

            "TrolleyCursors/BagTrolleyur2.gif");





// These variables will hold the current mouse pointer position.



var mouseX = 0;

var mouseY = 0;



// Set up event capturing.

document.onmousemove = getMousePosition;



function init() 

{

	// Start loading images.

	//startLoadBar(myimages);

	endLoadBar()

}



function getMousePosition(e) {



  // Save cursor position using browser-specific code.

  mouseX = event.clientX + document.body.scrollLeft;

  mouseY = event.clientY + document.body.scrollTop;

	

  return true;

}



var Trolleys = new Array(1);

var KatiesLeg = new Array(1);

var TrolleyDirection = new Array(1);



function endLoadBar() 

{

  var i;



  // Get handles to all the Trolley layers.

  for (i = 0; i < Trolleys.length; i++) 

  {

    Trolleys[i] = getLayer("Trolley" + (i + 1));

    Trolleys[i].image = document.images["imgTrolley" + (i + 1)];

    initTrolley(i);

    showLayer(Trolleys[i]);

  }

  updateTrolleys();

}



function initTrolley(n) {



  var s, x, y;



  // Randomly place a Trolley 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(Trolleys[n]);

  if (s == 1)

    x = getWindowWidth();

  if (s == 2)

    y = -getHeight(Trolleys[n]);

  if (s == 3)

    y = getWindowHeight();

  x += getPageScrollX();

  y += getPageScrollY();

  moveLayerTo(Trolleys[n], x, y);

  

  KatiesLeg[n] = 1;

  TrolleyDirection[n] = -1;

}



function updateTrolleys() {



  var i, dx, dy, theta, d;

  var Hysteresis = 10; // degrees

  var bFindNewDirection = true;



  // Move each Trolley toward the mouse pointer, if she hits it, drop her back onto

  // the page randomly.



  d = 3;



  for (i = 0; i < Trolleys.length; i++) {



    // Find the angle between the Trolley and the pointer.

    dx = mouseX - getLeft(Trolleys[i]) - 50;

    dy = mouseY - getTop(Trolleys[i]);

    theta = Math.round(Math.atan2(dx, -dy) * 180 / Math.PI);

    if (theta < 0)

      theta += 360;



    // Hit the pointer?

    if (Math.abs(dx) < d && Math.abs(dy) < d)

    {

	      initTrolley(i);

	}

	else

	{

		// If not, move the Trolley and set the image based on angle.

		if (KatiesLeg[i] == 1)

			KatiesLeg[i] = 2;

		else

			KatiesLeg[i] = 1;



		// Angles:  23, 68, 113, 158, 203, 248, 293, 338



		// Check if we can just continue on same direction

		switch(TrolleyDirection[i])

		{

			case -1: //uninitialised

				bFindNewDirection = true;

				break;

			case 0: //ur

				if (theta > (23 - Hysteresis) && theta <= (68 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 1: //rt

				if (theta > (68 - Hysteresis) && theta <= (113 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 2: //dr

				if (theta > (113 - Hysteresis) && theta <= (158 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 3: //dn

				if (theta > (158 - Hysteresis) && theta <= (203 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 4: //dl

				if (theta > (203 - Hysteresis) && theta <= (248 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 5: //lt

				if (theta > (248 - Hysteresis) && theta <= (293 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 6: //ul

				if (theta > (293 - Hysteresis) && theta <= (338 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

			case 7: //up

				if (theta > (338 - Hysteresis) && theta <= (23 + Hysteresis))

				{

					bFindNewDirection = false;

				}

				break;

		}			



		if (bFindNewDirection)

		{

			// Find which new direction to move in

			if (theta > 23 && theta <= 68)

			{

				TrolleyDirection[i] = 0;

			}

			else if (theta > 68 && theta <= 113) 

			{

				TrolleyDirection[i] = 1;

			}

			else if (theta > 113 && theta <= 158) 

			{

				TrolleyDirection[i] = 2;

			}

			else if (theta > 158 && theta <= 203) 

			{

				TrolleyDirection[i] = 3;

			}

			else if (theta > 203 && theta <= 248) 

			{

				TrolleyDirection[i] = 4;

			}

			else if (theta > 248 && theta <= 293 ) 

			{

				TrolleyDirection[i] = 5;

			}

			else if (theta > 293 && theta <= 338) 

			{

				TrolleyDirection[i] = 6;

			}

			else 

			{

				TrolleyDirection[i] = 7;

			}

		}

		

		// Now do the move

		switch(TrolleyDirection[i])

		{

			case 0: //ur

				moveLayerBy(Trolleys[i], d, -d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleyur" + KatiesLeg[i] + ".gif";

				break;

			case 1: //rt

				moveLayerBy(Trolleys[i], d, 0);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleyrt" + KatiesLeg[i] + ".gif";

				break;

			case 2: //dr

				moveLayerBy(Trolleys[i], d, d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleydr" + KatiesLeg[i] + ".gif";

				break;

			case 3: //dn

				moveLayerBy(Trolleys[i], 0, d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleydn" + KatiesLeg[i] + ".gif";

				break;

			case 4: //dl

				moveLayerBy(Trolleys[i], -d, d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleydl" + KatiesLeg[i] + ".gif";

				break;

			case 5: //lt

				moveLayerBy(Trolleys[i], -d, 0);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleylt" + KatiesLeg[i] + ".gif";

				break;

			case 6: //ul

				moveLayerBy(Trolleys[i], -d, -d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleyul" + KatiesLeg[i] + ".gif";

				break;

			case 7: //up

				moveLayerBy(Trolleys[i], 0, -d);

				Trolleys[i].image.src = "TrolleyCursors/BagTrolleyup" + KatiesLeg[i] + ".gif";

				break;

		}			

	}

  }



  // Set up next call.

  setTimeout('updateTrolleys()', 100);

  return;

}





