// The original Pinwheel Javascript was written by
// Roy Whittle (Roy@Whittle.com), www.Roy.Whittle.com
// Adopted for W3C DOM in Internet Explorer 5.x am Netscape 6.x by
// Stefan Kuhn (stefan@come2kuhn.de), www.come2kuhn.de

// [CONFIGURATION]
var anz        = 16;   // Punktanzahl (1-32)
var size       = 1;    // Punktgroesse (1-5)
var RadMin     = 0;   // Startradius (0-100, in 5er-Schritten)
var Angle      = 22.5; // Kreis-Schritt (in Grad, frei waehlbar)
var RadStepAnz = 18;    // Puls-Steps (0-32, Pulsierschritte)
var RadStep    = 2    // Puls-Schritt (0-32, Groesse der Pulsierschritte)
var step3D     = 0;    // 3D-Speed (0-32)
var col        = new Array('#009E00','#00ff00'); // Farbe 1, Farbe 2
// [END OF CONFIGURATION]

// Don't change anything of the following code
// window.onerror=null;

var StepAngle   = (Angle)*2*Math.PI/360;
var StepAngle3D = step3D*StepAngle/8;
var ThisStep3D  = 0;
var ColNr       = 0;
var ColCounter  = 0;
var height      = 0;
var width       = 0;
var xpos        = 0;
var ypos        = 0;
var ax          = 0;
var ay          = 0;

ns4=(document.layers)?1:0;
ie4=(document.all)?1:0;
dom=(document.getElementById)?1:0;
gecko=(navigator.product)?1:0;
unit=(ns4)?'':'px';

function nelem(id)
{ var no            = document.createElement('DIV');
  no.id             = id;
  no.style.position = 'absolute';
  no.style.top      = '0px';
  no.style.left     = '0px';
  no.style.width    = size + 'px';
  no.style.height   = size + 'px';
  no.style.fontSize = size + 'px';
  no.style.backgroundColor=col[col.length-1];
  document.body.appendChild(no);
}

var dots = new Array();
function init()
{ function dot(nr)
  { if (ie4 && !dom) { this.obj = document.all['d'+nr].style; }
    if (ns4) { this.obj = document.layers['d'+nr]; }
    if (dom) { this.obj = document.getElementById('d'+nr).style; }
    this.step = nr%RadStepAnz;
    this.cosAngle = Math.cos(i*StepAngle);
    this.sinAngle = Math.sin(i*StepAngle);
  }
  for (i = 0; i < anz; i++)
  { if (dom) { nelem('d'+i); }
    dots[i] = new dot(i);
  }
  height = GetClientTop()+GetClientHeight()-1;
  width  = GetClientLeft()+GetClientWidth()-1;
  if (ie4)   { document.onmousemove=Mouse }
  if (ns4)   { window.captureEvents(Event.MOUSEMOVE);window.onMouseMove=Mouse; }
  if (gecko) { document.addEventListener('mousemove',Mouse,false) }
}

function Mouse(evnt)
{ xpos = (ie4)?event.x+document.body.scrollLeft:window.pageYOffset+evnt.pageX;
  ypos = (ie4)?event.y+document.body.scrollTop:window.pageYOffset+evnt.pageY;
}

function GetClientHeight() {return (ie4)?document.body.clientHeight:window.innerHeight;}
function GetClientWidth()  {return (ie4)?document.body.clientWidth:window.innerWidth;}
function GetClientTop()    {return (ie4)?document.body.scrollTop:window.pageYOffset;}
function GetClientLeft()   {return (ie4)?document.body.scrollLeft:window.pageXOffset;}

function TwistnSpin()
{ if (step3D > 0)
  { ThisStep3D -= StepAngle3D;
    Cos3D = Math.cos(ThisStep3D);
    Sin3D = Math.sin(ThisStep3D);
  }
  else
  { Cos3D = 1;
    Sin3D = 1;
  }
  ay += (ypos - ay) / 10;
  ax += (xpos - ax) / 10;
  for (i = 0; i < anz; i++)
  { dots[i].step = (dots[i].step+1)%RadStepAnz;
    radius       = RadMin+(dots[i].step*RadStep);
    var top      = Math.round(ay + radius * dots[i].cosAngle * Cos3D)
    var left     = Math.round(ax + radius * dots[i].sinAngle * Sin3D)
    if ( (top+size  >= height) || (left+size >= width) )
    { dots[i].obj.top ='-'+size+unit;
      dots[i].obj.left='-'+size+unit;
    }
    else
    { dots[i].obj.top = top  + unit;
      dots[i].obj.left= left + unit;
    }
  }
  // Color-Animation
  if (ns4)
  { dots[ColCounter].obj.bgColor=col[ColNr]; }
  else
  { dots[ColCounter].obj.backgroundColor=col[ColNr]; }
  ColCounter++;
  if (ColCounter == anz) { ColCounter=0; ColNr++; }
  if (ColNr == col.length) ColNr=0;
}

function Starter()
{ init();
  setInterval('TwistnSpin()', 25);
}

function resize()
{ var oh = height;
  var ow = width;
  height = GetClientTop()+GetClientHeight()-1;
  width  = GetClientLeft()+GetClientWidth()-1;
  if ((oh!=height) || (ow!=width))
  { if(ns4) location.reload();
    else setTimeout('resize()',10);
  }
}

// Initialisation for ie4 & ns4
if (!dom)
{ for (i = 0; i < anz; i++)
  { if (ns4) { document.write('<LAYER NAME="d'+i+'" LEFT=0 TOP=0 BGCOLOR='+col[col.length-1]+' CLIP="0,0,'+size+','+size+'"></LAYER>'); }
    if (ie4) { document.write('<DIV ID=d'+i+' STYLE="position:absolute;top:0px;left:0px;width:'+size+'px;height:'+size+'px;background:'+col[col.length-1]+';font-size:3px"></DIV>'); }
  }
}

window.onresize=resize;
window.onload=Starter;

