// BubbleDrift: designed to animate graphics to Bubble-up or Drift-down
window.onerror = null; // error trapping
var dLyr = (document.layers)?1:0;  // layer support
var dAll = (document.all)?1:0; // object support
var n, i, up, mv, stop = false, dWth = 600, dHgt = 400; // initialize defaults
var bdsrc, bdlnk, bdnm, bdx, bdy; // user defined values (passed in)
var xd = new Array(), yd = new Array(); // displacement
var xp = new Array(), yp = new Array(); // position
var xs = new Array(), ys = new Array(); // step

function init(n,tmp,bdsrc,bdlnk,bdnm,bdx,bdy) { // initialize routines
 if (dLyr || dAll) {
  up = (tmp > 0)?1:0; // direction (positive value indicates up)
  if (dLyr) { // detect client dimensions
   dWth = self.innerWidth;
   dHgt = self.innerHeight;
  } else if (dAll) {
   dWth = window.document.body.clientWidth;
   dHgt = window.document.body.clientHeight;
  }
  for (i = 0; i < n; ++i) { // define objects and movement
   xd[i] = 0; // initial X displacement
   xp[i] = Math.random() * (dWth - 50); // X position
   yp[i] = Math.random() * dHgt; // Y position
   yd[i] = Math.random() * 20; // displacement
   xs[i] = 0.02 + Math.random() / 10; // X step
   ys[i] = 0.8 + Math.random(); // Y step
   if (up) ys[i] = -(ys[i]); // direction
   // build objects to animate (first object contains a surprise link)
   if (dLyr) {
    document.write("<layer name=\"bd"+ i + "\" visibility=\"show\">");
   } else if (dAll) {
    document.write("<div id=\"bd"+ i + "\" style=\"position:absolute;");
    document.write(" z-index:" + i + "; visibility:visible;\">");
   }
   if (i == 0) document.write("<a href=\"" + bdlnk + "\">");
   document.write("<img src=\"" + bdsrc + "\" alt=\"" + bdnm + "\" border=\"0\"");
   document.write("\" width=\"" + bdx + "\" height=\"" + bdy + "\" />");
   if (i == 0) document.write("</a>");
   if (dLyr) {
    document.write("</layer>");
   } else if (dAll) {
    document.write("</div>");
   }
  }
  document.write("<small><a href=\"javascript:toggle()\">[stoppa/starta animation]</a></small>");
  bubbledrift(); // start timed animations
 }
}

function toggle() {
 if (stop) {stop = false; bubbledrift();} else {stop = true;}
}

function bubbledrift() {  // main animation routine
 if (stop) return;
 for (i = 0; i < n; ++ i) {  // repeat for each object
  yp[i] += ys[i]; // add step to position
  xd[i] += xs[i]; // add step to displacement
  // farthest distance to travel, 95px from top upward, 10px from bottom downward
  if (up) {mv = (yp[i] < 95)?1:0;} else {mv = (yp[i] > dHgt - bdy - 10)?1:0;}
  if (mv) {
   xp[i] = Math.random() * (dWth - yd[i] - bdx);
   // starting point of travel, 10px from bottom upward, 10px from top downward
   if (up) {yp[i] = dHgt - bdy - 10;} else {yp[i] = 10;}
   xs[i] = 0.02 + Math.random() / 10;
   ys[i] = 0.8 + Math.random();
   if (up) ys[i] = -(ys[i]);
   if (dLyr) {
    dWth = self.innerWidth;
    dHgt = self.innerHeight;
   } else if (dAll) {
    dWth = document.body.clientWidth;
    dHgt = document.body.clientHeight;
   }
  }
  if (dLyr) {
   document.layers["bd"+i].top = yp[i];
   document.layers["bd"+i].left = xp[i] + yd[i] * Math.sin(xd[i]);
  } else if (dAll) {
   document.all["bd"+i].style.pixelTop = yp[i];
   document.all["bd"+i].style.pixelLeft = xp[i] + yd[i] * Math.sin(xd[i]);
  }
 }
 setTimeout("bubbledrift()", 10);
}

