// =============================== //
// Bouncing Images                 //
// v1.1 - Feb 5, 2005              //
// ------------------------------- //
// Written by Lloyd Hassell        //
// Website: lloydhassell.com       //
// Email: lloydhassell@hotmail.com //
// =============================== //
// History:                        //
// 2008.10.12 kennyb Maintenance   //
// =============================== //

// Featured on: Dynamic Drive HTML code library (http://www.dynamicdrive.com)

// INITIALIZATION:
bouncingImages = new Object();

// CONFIGURATION:
bouncingImages.imgCount = 1;
bouncingImages.imgWidth = 80;
bouncingImages.imgHeight = 60;
bouncingImages.frameRate = 50;
bouncingImages.minRandomSpeed = 1;
bouncingImages.maxRandomSpeed = 3;
bouncingImages.imgSrc = 'pics/dadalib8060.gif';

// MAIN:
bouncingImages.isLoaded = false;
bouncingImages.dirX = new Array();
bouncingImages.dirY = new Array();
bouncingImages.posX = new Array();
bouncingImages.posY = new Array();
bouncingImages.speedX = new Array();
bouncingImages.speedY = new Array();
var winWidth, winHeight, imgSelector;

imgSelector = Math.floor(Math.random() * 10);
switch (imgSelector) {
   case 0:
     bouncingImages.imgSrc = 'pics/warisa.gif';
     break;
   case 1:
     bouncingImages.imgSrc = 'pics/ww2shot.gif';
     break;
   case 2:
     bouncingImages.imgSrc = 'pics/mare_nectaris.gif';
     break;
   case 3:
     bouncingImages.imgSrc = 'gifs/vglove.gif';
     break;
   case 4:
     bouncingImages.imgSrc = 'gifs/launch8060.gif';
     break;
   case 5:
     bouncingImages.imgSrc = 'gifs/isonumb.gif';
     break;
   case 6:
     bouncingImages.imgSrc = 'gifs/k1.gif';
     break;
   case 7:
     bouncingImages.imgSrc = 'gifs/stjarna.gif';
     break;
   case 8:
     bouncingImages.imgSrc = 'gifs/apache.gif';
     break;
   case 9:
     bouncingImages.imgSrc = 'gifs/b52bomb.gif';
     break;
   case 10:
     bouncingImages.imgSrc = 'pics/eyecandy1.gif';
     break;
   default:
     bouncingImages.imgSrc = 'pics/stjarna.gif';
}

if (dyn) var preloadImgObj = loadImg(bouncingImages.imgSrc); 

function loadBouncingImages() {
   if (dyn && !bouncingImages.isLoaded) {
      winWidth = getWinWidth();
      winHeight = getWinHeight();
      for (var layerLoop = 0; layerLoop < bouncingImages.imgCount; layerLoop++) {
         bouncingImages.dirX[layerLoop] = (Math.round(Math.random()) == 0) ? 'left' : 'right';
         bouncingImages.dirY[layerLoop] = (Math.round(Math.random()) == 0) ? 'up' : 'down';
         bouncingImages.posX[layerLoop] = Math.floor(Math.random() * (winWidth - bouncingImages.imgWidth - 1)) + getDocScrollLeft();
         bouncingImages.posY[layerLoop] = Math.floor(Math.random() * (winHeight - bouncingImages.imgHeight - 1)) + getDocScrollTop();
         bouncingImages.speedX[layerLoop] = Math.round(Math.random() * (bouncingImages.maxRandomSpeed - bouncingImages.minRandomSpeed)) + bouncingImages.minRandomSpeed;
         bouncingImages.speedY[layerLoop] = Math.round(Math.random() * (bouncingImages.maxRandomSpeed - bouncingImages.minRandomSpeed)) + bouncingImages.minRandomSpeed;
         var tempLayerObj = addLayer('bouncingImagesLyr' + layerLoop);
         bouncingImages['layerObj' + layerLoop] = tempLayerObj;
         setLayerSize(tempLayerObj,bouncingImages.imgWidth,bouncingImages.imgHeight);
         setLayerClip(tempLayerObj,0,bouncingImages.imgWidth,bouncingImages.imgHeight,0);
         setLayerHTML(tempLayerObj,getImgTag('bouncingImagesImg' + layerLoop,preloadImgObj.src,bouncingImages.imgWidth,bouncingImages.imgHeight,0));
         moveLayerTo(tempLayerObj,bouncingImages.posX[layerLoop],bouncingImages.posY[layerLoop]);
         showLayer(tempLayerObj);
         }
      bouncingImages.isLoaded = true;
      moveBouncingImages();
      }
   }

function moveBouncingImages() {
   for (var layerLoop = 0; layerLoop < bouncingImages.imgCount; layerLoop++) {
      if (bouncingImages.dirX[layerLoop] == 'left') {
         if (bouncingImages.posX[layerLoop] > bouncingImages.speedX[layerLoop]) bouncingImages.posX[layerLoop] -= bouncingImages.speedX[layerLoop];
         else {
            bouncingImages.dirX[layerLoop] = 'right';
            bouncingImages.posX[layerLoop] = 0;
            }
         }
      else if (bouncingImages.dirX[layerLoop] == 'right') {
         if (bouncingImages.posX[layerLoop] + bouncingImages.imgWidth < winWidth - bouncingImages.speedX[layerLoop]) bouncingImages.posX[layerLoop] += bouncingImages.speedX[layerLoop];
         else {
            bouncingImages.dirX[layerLoop] = 'left';
            bouncingImages.posX[layerLoop] = winWidth - bouncingImages.imgWidth;
            }
         }
      if (bouncingImages.dirY[layerLoop] == 'up') {
         if (bouncingImages.posY[layerLoop] > bouncingImages.speedY[layerLoop]) bouncingImages.posY[layerLoop] -= bouncingImages.speedY[layerLoop];
         else {
            bouncingImages.dirY[layerLoop] = 'down';
            bouncingImages.posY[layerLoop] = 0;
            }
         }
      else if (bouncingImages.dirY[layerLoop] == 'down') {
         if (bouncingImages.posY[layerLoop] + bouncingImages.imgHeight < winHeight - bouncingImages.speedY[layerLoop]) bouncingImages.posY[layerLoop] += bouncingImages.speedY[layerLoop];
         else {
            bouncingImages.dirY[layerLoop] = 'up';
            bouncingImages.posY[layerLoop] = winHeight - bouncingImages.imgHeight;
            }
         }
      }
   for (var layerLoop = 0; layerLoop < bouncingImages.imgCount; layerLoop++) moveLayerTo(bouncingImages['layerObj' + layerLoop],bouncingImages.posX[layerLoop] + getDocScrollLeft(),bouncingImages.posY[layerLoop] + getDocScrollTop());
   window.setTimeout('moveBouncingImages()',bouncingImages.frameRate);
   }
