window.onload = begin;
window.onresize = update;

width = 0;
height = 0;
lets='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
a='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0';
distvect = a.split(' ');
xvect = a.split(' ');
yvect = a.split(' ');

function begin() {

  update();

  for(i = 0; i < 26; i++) {
    distvect[i] = i;
    setpath(i);
  }

  move();

}

function update() {

  width = Math.floor(document.getElementById('starfield').clientWidth/2) - 1;
  height = Math.floor(document.getElementById('starfield').clientHeight/2)-1;

}

function setpath(k) {

  xvect[k] = 0;
  yvect[k] = 0;
  r=Math.PI*2*(Math.random()-.5);

  if(Math.abs(Math.tan(r)) > height/width) {

    xvect[k] = Math.floor(width + height/Math.tan(r));
    if(r < 0) yvect[k] = 2*height;

  } else {

    yvect[k] = Math.floor(height + width*Math.tan(r));
    if(Math.abs(r) > Math.PI/2) xvect[k] = 2*width;

  }

}

function move() {

  for(i = 0; i < 26; i++) {

    distvect[i]++;
    n = lets.charAt(i);

    document.getElementById('A' + n).style.left=(Math.floor(distvect[i]*distvect[i]/676*(xvect[i]-width)+width))+"px";
    document.getElementById('A' + n).style.top=(Math.floor(distvect[i]*distvect[i]/676*(yvect[i]-height)+height))+"px";
    document.getElementById('B' + n).style.left=(Math.floor((distvect[i]-.5)*distvect[i]/676*(xvect[i]-width)+width))+"px";
    document.getElementById('B' + n).style.top=(Math.floor((distvect[i]-.5)*distvect[i]/676*(yvect[i]-height)+height))+"px";
    document.getElementById('A' + n).style.height=(Math.floor(1 + distvect[i]/9))+"px";
    document.getElementById('A' + n).style.width=(Math.floor(1 + distvect[i]/9))+"px";
    document.getElementById('B' + n).style.height=(Math.floor(1 + distvect[i]/9))+"px";
    document.getElementById('B' + n).style.width=(Math.floor(1 + distvect[i]/9))+"px";

    if(distvect[i] >= 26) {

      distvect[i] = 1 + 8*(i % 3);
      setpath(i);

    }

  }

  setTimeout("move()",50);

}
