var mc = Math.cos(0.2), ms = Math.sin(0.2);
var mcm = Math.cos(-0.2), msm = Math.sin(-0.2);
var mcx = Math.cos(-0.9), msx = Math.sin(-0.9);
var numStars = 8; // even for rand plode theory
var xstep=new Array(numStars);
var ystep=new Array(numStars);

var nxpos=1.0, nypos=1.0;

var tmrId = null;
var  ii = -1, dd = 0, aa = "commandString";
var tmrIson = 0;
var xv=new Array(numStars);
var yv=new Array(numStars);
var X,Y,W,H,cx,cy,w,h;
var ns5 = ((navigator.appName == "Netscape")&&(parseInt(navigator.appVersion) >= 5))? 1:0;
var ns4 = (document.layers)? 1:0;
var ie4 = (document.all)? 1:0;
var ie5 = 0, Xpos = 20, Ypos = 180;
var once = 0;
function props() {
  W = screen.width;
  H = screen.height;
  X = ((ns4) || (ns5))? window.screenX : self.screenLeft;
  Y = ((ns4) || (ns5))? window.screenY : self.screenTop;
  w = ((ns4) || (ns5))? Math.max(document.width,window.innerWidth) : document.body.clientWidth;
  h = ((ns4) || (ns5))? Math.max(document.height,window.innerHeight) : document.body.clientHeight;
  // w = 778;
  h = 1700;
  // IE
  // window.document.body.offsetWidth  window.document.body.offsetHeight
  // document.body.scrollWidth document.body.scrollHeight
  // NOTdocument.body.offsetWidth;
  cx = Math.floor(w/2); cy = Math.floor(h/2);
}
function init() {
  init2();
  props();
  if (ns4) initMouseEvents();
  if (ie4) {
    if ((navigator.userAgent.indexOf('MSIE 5') > 0) || (navigator.userAgent.indexOf('MSIE 6') > 0)) { ie5 = 1; ie4 = 0 }
  }
  for (var i = 0 ; i < numStars; i++) {
    xstep[i] = 20;
    ystep[i] = 180;
  }
  startTrails();
}
function init2() {
  BrowserIs();
  NetscapeEngine = (navigator.appName == "Netscape");
  MSEngine = ((navigator.appName == 'Microsoft Internet Explorer') && ((navigator.userAgent.indexOf('Opera') < 0) || (navigator.userAgent.indexOf('Opera 7') >= 0)));
  AdvNetscapeEngine = ((navigator.appName == "Netscape")&&(navigator.appVersion >= '5'));
  OldNetscapeEngine = ((navigator.appName == "Netscape")&&(navigator.appVersion < '5'));
  normal = new MakeArray(2);
  over = new MakeArray(2);
  /* normal[0].src = "linebrd1.gif";
  over[0].src = "line.gif"; */
}

/* secTimer usage: stopTimer(); secTimer(iteration, delay, action); 0 iteration = infinity
example: stopTimer(); secTimer(3,3,"alert('hello')"); go 3 times every 3 secs; start in 3 secs from now; */

function secTimer(i,d,a) {
  ii = i; dd = d; aa = a;
  if (i == 0) {
    tmrId = setTimeout("secTimer(ii,dd,aa)",d*1000);
    if (tmrIson) eval(a);
    tmrIson = 1;
  }
  else if (i) {
    if (tmrIson) {
      eval(a);
      i -=1; ii = i;
    }
    if (ii > 0) {
      tmrId = setTimeout("secTimer(ii,dd,aa)",d*1000);
      tmrIson = 1;
    }
    else tmrIson = 0;
  }
}
function stopTimer() {
  echo("timer off");
  ii = -1;
  tmrIson = 0;
  clearTimeout(tmrId);
  setTimeout("echo(' ')", 5*1000);
}
function startTrails() {
  if (tmrIson) stopTimer();
  if (!tmrIson) secTimer(0,0.05,'trails()');
}
function plode() {
  stopTimer();
  echo('plode');
  var r = rand(numStars/2-2,numStars/2), i;
  // echo(r);
  mc = Math.cos(3.14159 / (numStars/2));  ms = Math.sin(3.14159 / (numStars/2));
  var x0 = xstep[r] - Xpos;
  var y0 = ystep[r] - Ypos;
  var d = vlen(x0,y0);
  if (d == 0) d = 1;
  x0 /= d;
  y0 /= d;
  x0*=(numStars-1);
  y0*=(numStars-1);
  for ( i = 0 ; i < numStars ; i++ ) {
    nxpos = x0*mc - ms*y0;
    nypos = x0*ms + mc*y0;
    x0 = nxpos;
    y0 = nypos;
    xv[i] = nxpos;
    yv[i] = nypos;
  }
  mc = Math.cos(0.2); ms = Math.sin(0.2);
  secTimer(0,0.05,'vectorlaunch()');
  setTimeout("echo(' ')", 5*1000);
}
function normalize(a,b) {
  var d = vlen(a,b);
  if (d == 0) d=1;
  return new Array((a / d), (b / d));
}
function vectorlaunch() {
  var tlayr, j;
  for ( j = 0 ; j < numStars ; j++ ) {
    xstep[j] += xv[j];
    ystep[j] += yv[j];
    if ((MSEngine) || (AdvNetscapeEngine)) {
      tlayr = eval("sl" + j);
      // tlayr.style.left = Math.floor(xstep[j]);
      // tlayr.style.top = Math.floor(ystep[j]);
      MoveLayer(tlayr,Math.floor(xstep[j]),Math.floor(ystep[j]));
    }
    else if (0) {
      // ns4 use layer tag in doc
      tlayr = "sl"+j;
      document.layers[tlayr].left = xstep[j];
      document.layers[tlayr].top = ystep[j];
    }
    // w&h minus image w&h
    if (xstep[j] >= (w - 56)) xv[j] = -xv[j];
    if (ystep[j] >= (h - 40)) yv[j] = -yv[j];
    if (xstep[j] <= 0) xv[j] = -xv[j];
    if (ystep[j] <= 0) yv[j] = -yv[j];
  }
}
function round(a,b) {
  var c = Math.pow(10,b);
  return Math.floor(c * (0.5 / c + a)) / c;
}
function windChill(form) {
  var wind=eval(form.wind.value);
  var temp=eval(form.temp.value);
  var oldchill=(0.0817*(3.71*Math.sqrt(wind)+5.81-0.25*wind)*(temp-91.4)+91.4);
  // var oldchill = 91.4 - (0.474677 - 0.020425 * temp + 0.303107 * Math.sqrt(wind)) * (91.4 - temp);
  var newchill=(35.74+0.6215*temp-35.75*Math.pow(wind,0.16)+0.4275*temp*Math.pow(wind,0.16));
  if (oldchill > temp) oldchill = temp;
  if (newchill > temp) newchill = temp;
  form.oldwindchill.value = round(oldchill,2);
  form.newwindchill.value = round(newchill,2);
}
function echo() {
  var arg = arguments, i = 0, s = " ";
  for (i=0;i<arg.length;i++) {
    s = s + " " + arg[i];
  }
  window.status = s;
}
function divv(a,b,c,d) {
  return 0;
}
function vlen(a,b) {
  return Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
}
function vectorlen(n) {
  return vlen(Xpos - xstep[n], Ypos - ystep[n]);
  return round(vlen(Xpos - xstep[n], Ypos - ystep[n] ),2);
}
function rand(mn,mx) {
  var r = -1;
  while ((r < mn) || (r > mx)) {
    r = Math.random();
    r = round(r * mx,0);
  }
  return r;
}
function randnum(mn,mx) {
  var r = -1;
  while ((r < mn) || (r > mx)) {
    r = Math.abs(Math.sin((new Date()).getTime()));
    r = round(r * mx,0);
  }
  return r;
}
function plus(a,b) {
  var a = new Number(a);
  var b = new Number(b);
  return eval(a+b);
}
function warn(s) {
  alert(s +" "+ ii);
}
function mouseMove(e) {
  if (MSEngine) {
    Xpos = document.body.scrollLeft+event.clientX;
    Ypos = document.body.scrollTop+event.clientY;
  }   
  else { Xpos = e.pageX; Ypos = e.pageY; }
  if ((Xpos > (w - 100)) || (Ypos > (h - 100))) {
    if (!once) plode();
    once = 1;
  }
  else if ((Xpos < (w - 100)) || (Ypos < (h - 100))) {
    if (once) { once = 0; startTrails(); }
  }
  Xpos -= 6;
  Ypos -= 10;
  //  echo(1,2,Xpos,Ypos);
}
function initMouseEvents() {
  document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;

function mouseUp(e) {
  // if (Id2) clearTimeout(Id2); kills plode
  if ((Xpos < (w - 100)) && (Ypos < (h - 100))) {
    if ((MSEngine) || (AdvNetscapeEngine)) {
      plode();
      tmrId2 = setTimeout('startTrails()',15*1000);
    }
    else if (e.which == 1) {
      plode();
      tmrId2 = setTimeout('startTrails()',15*1000);
    }
  }
}
function trails() {
  var tlayr, nz = 0.0;
  if ((MSEngine) || (AdvNetscapeEngine)) {
    for (var i = 0 ; i < numStars ; i++)  {
      // echo(i);
      if (vectorlen(i) > 30.0) {
        nxpos = (Xpos - xstep[i]) / (i+2) + xstep[i];
        nypos = (Ypos - ystep[i])  / (i+2) + ystep[i];
        tlayr = eval("sl" + i);
        MoveLayer(tlayr,Math.floor(nxpos),Math.floor(nypos));
        xstep[i] = nxpos;
        ystep[i] = nypos;
      }
      else {
        if (i%2) {
          nxpos = ((xstep[i] - Xpos) * mc) - (ms * (ystep[i] - Ypos)) * 0.97
          nypos = ((xstep[i] - Xpos) * ms) + (mc * (ystep[i] - Ypos)) * 0.97
        }
        else {
          nxpos = ((xstep[i] - Xpos) * mcm) - (msm * (ystep[i] - Ypos)) * 0.96
          nypos = ((xstep[i] - Xpos) * msm) + (mcm * (ystep[i] - Ypos)) * 0.96
        }
        // keep track 2d movement
        xstep[i] = nxpos + Xpos;
        ystep[i] = nypos + Ypos;

        // 3D view
        nz = nypos * msx
        nypos = nypos * mcx
        nxpos = (-800 / (-1000 - nz)) * nxpos + Xpos;
        nypos = (-800 / (-1000 - nz)) * nypos + Ypos;
        if (0) {
          // ns4 use layer tag in doc
          tlayr ="sl"+i;
        }
        tlayr = eval("sl" + i);
        MoveLayer(tlayr,Math.floor(nxpos),Math.floor(nypos));
      }
    }
  }
}
function MakeArray(n) {
  this.length = n;
  for (var i=0;i<n;i++) {
    this[i] = new Image();
  }
  return this;
}
function msover(num) {
  // if (browser) eval('
  document.images[num].src = over[num].src;
  //');
}
function msout(num) {
  // if (browser) eval('
  document.images[num].src = normal[num].src;
  //');
}
function BrowserIs() {
  IE5 = false;
  browser=navigator.appName;
  version=navigator.appVersion;
  if (browser.indexOf("Microsoft") >=0 ) {
    if (version.indexOf("MSIE 5.0") >= 0) {
      IE5 = true;
    }
  }
}
function HideLayers() {
  with(this) {
    for (var i=0;i < arguments.length;i++) {
      if ((MSEngine) || (AdvNetscapeEngine)) arguments[i].style.visibility='hidden';
      else if (OldNetscapeEngine) arguments[i].visibility='hide';
      else MoveLayer(arguments[i],-32000,0);
      arguments[i].IsVisible = false;
    }
  }
} 
function ShowLayers() {
  with(this) {
    for (var i=0;i < arguments.length;i++) {
      if ((MSEngine) || (AdvNetscapeEngine)) arguments[i].style.visibility='visible';
      else if (OldNetscapeEngine) arguments[i].visibility='show';
      arguments[i].IsVisible = true;
    }
  }
}
function MoveLayer(Layer,L,T,_Show) {
  with(this) {
    if (OldNetscapeEngine) {
      Layer.moveTo(L,T);
      // document.layers[tlayr].left = Math.floor(nxpos);
      // document.layers[tlayr].top = Math.floor(nypos);
    }
    else {
      Layer.style.left = L;
      Layer.style.top = T;
    }
    if (_Show) ShowLayers(Layer);
  }
}
function  MoveLayerEx(Layer,SubLayer,X,Y,L,T,W,H) {
  with(this) {
    if (MSEngine) {
      SubLayer.style.backgroundPosition=''+(-L)+'px '+(-T)+'px';
      MoveLayer(Layer,X,Y);
      return;
    }
    else if (AdvNetscapeEngine) { MoveLayer(SubLayer,-L,-T); T=0; L=0; } 
    else with (Layer.clip) { left=L; top=T; width=W; height=H; }
    MoveLayer(Layer,X-L,Y-T);
  }
}
function echo() {
  with(this) {
    var arg = arguments, i = 0, s = " ";
    for (i=0;i<arg.length;i++) {
      if (arg[i] != null) s = s + " " + arg[i];
    }
    window.status = s;
  }
}
function _string() {
  with(this) {
    var arg = arguments, i = 0, s = " ";
    for (i=0;i<arg.length;i++) {
      if (arg[i] != null) s = s + " " + arg[i];
    }
    return s;
  }
}
function mdo2() {
  stopTimer();
  secTimer(3,3,"warn('hello')");
}
function mdo() {
  // echo(browser,IE5);
  var i = 2;
  var lay = eval("sl" + i);
  document.calc.results.value =_string(browser,version,"IE5=",IE5,W,H,X,Y,w,h);
  // document.calc.results.value = 0 + " " + lay.style.top;
  startTrails();
  // plode();
  // echo(W,H,X,Y,w,h);
  echo(window.document.body.offsetHeight);
  // echo(NetscapeEngine,MSEngine,AdvNetscapeEngine,OldNetscapeEngine);
  // where (-1 == false) (0 == true) gofigure!
  // echo(browser.indexOf("Microsoft"));
  // echo(version.indexOf("MSIE 5.0"));
  // echo(browser.indexOf("Netscape"));
  // echo(version.indexOf("5.0"));
  // echo(version.indexOf("5.0 (Windows; en-US)")): both are true (0)
  if (0) {
    if (layer1.IsVisible) HideLayers(layer1);
    else ShowLayers(layer1);
    MoveLayer(layer0,180,180,0);
    MoveLayer(layer1,380,180,0);
    MoveLayer(layer2,580,480,0);
    // echo(1,2,3,0,lay.style.z-index);
    // wrong or null
  }
  // echo(1,2,3,0,layer1.style.top,parseInt(layer1.style.top),"hello0");
  // layer1.style.top <- value ends in px ; must move layer before accessing else null
  // echo(layer1.all[0].style.top);
}




