
function init(){ 
  for (i = 0; i < nDots; i++) 
    {
        dots[i] = new dot(i);
        dots[i].obj.left = dots[i].X + "px"; 
        dots[i].obj.top = dots[i].Y + "px"; 
    }
    startanimate(); 
}

function SetCorrectionValues(destination)
{
    if(destination=="in" && document.all){
        if(event.x>=0 && event.x<30 ||event.x>700){
            correctionX = (document.body.offsetWidth / 2) - 387 - 6;
            correctionY = (document.all)? 115: 0;
        }
    }
    else if(destination=="out" && document.all)
    {
        if(event.x<10 ||event.x>760){
            correctionX = 0;
            correctionY = 0;
        }
    }
}

function SetInitialCorrection()
{
    if(document.all)
    {
        //document.getElementById("tcorrection").value=this.id;
    }
}

function dot(i) { 
  this.X = Xpos;
  this.Y = Ypos;
  this.dx = 0;
  this.dy = 0;
  dObj="dot"+i;
  this.obj=document.getElementById(dObj).style;
  this.obj.zIndex=50;
} 

function startanimate() {	
   setInterval("animate()", 10); 
} 

function setInitPositions(dots) { 
  var startloc = document.all.tags("LI"),i = 0; 
  for(i=0;i<startloc.length&&i<(nDots-1);i++) {
    dots[i+1].X = startloc[i].offsetLeft;startloc[i].offsetParent.offsetLeft - DOTSIZE;
    dots[i+1].Y = startloc[i].offsetTop+startloc[i].offsetParent.offsetTop + 2*DOTSIZE;
  } 
  dots[0].X = dots[1].X;
  dots[0].Y = dots[1].Y - SEGLEN; 
} 

function popmousemove(e){
    //workaround for Explorer does not give the right coordinates with absolute frames 
    //document.getElementById("tleft").value=((document.body.offsetWidth / 2) - 387);
    //document.getElementById("tcorrection").value=event.x;
    //document.getElementById("tleft").value=correctionX;
    //document.getElementById("ttop").value=correctionX;

    Xpos=(!document.all)?e.pageX:event.x + correctionX;
    Ypos=(!document.all)?e.pageY:event.y + correctionY;
}

function vec(X, Y) 
{ 
    this.X = X; 
    this.Y = Y;
}

function springForce(i, j, spring) {
  var dx=(dots[i].X-dots[j].X),dy=(dots[i].Y-dots[j].Y);
  var len = Math.sqrt(dx*dx + dy*dy); 
  if (len > SEGLEN) 
  {
    var springF = SPRINGK * (len - SEGLEN);spring.X += (dx / len) * springF;
    spring.Y += (dy / len) * springF; 
  }
} 
function animate() { 
  var start = 0,d=document; 
  if (followmouse) 
  {
    dots[0].X = Xpos;
    dots[0].Y = Ypos;
    start = 1; 
    for(i=start;i<nDots;i++)
    {
        var spring=new vec(0, 0);
        if (i > 0)springForce(i-1, i, spring); 
        if (i<(nDots - 1)) springForce(i+1, i, spring); 
        var resist = new vec(-dots[i].dx * RESISTANCE,-dots[i].dy * RESISTANCE); 
        var accel = new vec((spring.X + resist.X)/ MASS,(spring.Y + resist.Y)/ MASS + GRAVITY);
        dots[i].dx += (DELTAT * accel.X);dots[i].dy += (DELTAT * accel.Y); 
        if (Math.abs(dots[i].dx) < STOPVEL && Math.abs(dots[i].dy) < STOPVEL && Math.abs(accel.X) < STOPACC && Math.abs(accel.Y) < STOPACC) 
        {
            dots[i].dx = 0;dots[i].dy = 0;
        }
        dots[i].X += dots[i].dx;
        dots[i].Y += dots[i].dy;
        
        var height, width;
        height =(isNetscape)? self.innerHeight:d.body.clientHeight + d.body.scrollTop;
        width =(isNetscape)?  self.innerWidth:document.body.clientWidth + document.body.scrollLeft;
        if (dots[i].Y >=  height - DOTSIZE - 1)
        {
            if (dots[i].dy > 0) dots[i].dy = BOUNCE * -dots[i].dy; 
                dots[i].Y = height - DOTSIZE - 1; 
        }
        if (dots[i].X >= width - DOTSIZE)
        {
            if (dots[i].dx > 0)
                dots[i].dx = BOUNCE * -dots[i].dx; dots[i].X = width - DOTSIZE - 1;
        }
        if (dots[i].X < 0)
        {
            if (dots[i].dx < 0)
                dots[i].dx = BOUNCE * -dots[i].dx; dots[i].X = 0;
        } 
        //document.getElementById("ttop").value=dots[i].Y + "px";
        //document.getElementById("tleft").value=dots[i].X + "px";
        //document.getElementById("ttop").value=dots[0].Y;
        //document.getElementById("tleft").value=dots[0].X;

        dots[i].obj.left = dots[i].X + "px"; 
        dots[i].obj.top =  dots[i].Y + "px";
    }
  }
}
