var neige = 0;
var flocons = new Array(24);
var vx = new Array(24);
var vy = new Array(24);

function neigeOn() {
  var i;

  if (neige == 1) return

  // Get handles to all the flocon layers.
  if (neige == 0)
  for (i = 0; i < flocons.length; i++) {
    flocons[i] = getLayer("flocon" + (i + 1));
    initFlocon(i);
    showLayer(flocons[i]);
  }
  neige = 1;
  updateFlocons();
}

function neigeOff() {
  neige = 2;
}

function initFlocon(n) 
{

  var x, y;

  // Randomly place an flocon on the window 90%+

  x = Math.floor(Math.random() * 200 ); // getWindowWidth());
  y = Math.floor(Math.random() * 200 ); //getWindowHeight());
  vy[n] = Math.floor(Math.random() * 9 + 1);
  vx[n] = Math.floor(Math.random()*(vy[n]/4 + 1));
  if (Math.random() > 0.5) vx[n] = -vx[n];
  moveLayerTo(flocons[n], x, y);
}

function initFlocon2(n) {

  var x, y;

  // Randomly place an flocon on top

  x = Math.floor(Math.random() * 450); // getWindowWidth());
  y = 0;
  moveLayerTo(flocons[n], x, y);
}

function updateFlocons() {

  var i;

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

    // Hit the pointer?

    if (getTop(flocons[i]) >  450) //getWindowWidth())
      initFlocon2(i);     

    if (getTop(flocons[i]) >  450) //getWindowHeight() + getPageScrollY())
      initFlocon2(i);

    else if ((getTop(flocons[i]) > 450 + getPageScrollX())
             && (getLeft(flocons[i]) > 450))
      initFlocon2(i);


    // If not, move the flocon and set the image based on angle.
    else {
        moveLayerBy(flocons[i], vx[i], vy[i]);
        vx[i] += Math.floor(Math.random() * 5 - 2);
        if (vx[i] > 4) vx[i] = 4;
        if (vx[i] < -4) vx[i] = -4;
    }
  }

  // Set up next call.
  if (neige == 1)
  setTimeout('updateFlocons()', 50);
  return;
}
