 var theCount=2;
 var toanimate=0;
 var topreview=0;
function preview () { // toggles to_preview
  if (topreview) { topreview = 0; Reinput (); }
  else { topreview = 1; Reinput (); }
}
function Reinput () {
  var thislambda = myWavelength.getValue(0);	// thislambda*10 = wavelength
  var thisthick = myWallSlider.getValue(0);	// thisthick*10 = bead diameter
  var thisangle = myAngleSlider.getValue(0);	// thisangle*5 = incident angle
  if (thislambda == "NaN") thislambda = 23;
  if (thisthick == "NaN") thisthick = 0;
  if (thisangle == "NaN") thisangle = 0;

 // get the picture's location, and move and resize walls to fit it
  var pixcoors = findPos(document.pix);
  document.getElementById('overlay').style.left = pixcoors[0] + 'px';
  document.getElementById('overlay').style.top = pixcoors[1] + 'px';

  document.lssform.wallsize.value = thisthick * 30 + 200;
  document.lssform.inangle.value = thisangle*5;
  document.lssform.lambda.value = thislambda * 20;
  // Find closest color
  closestcolor = parseInt((thislambda-17)/2);
  if (closestcolor > 8) { closestcolor = 8; }
  if (closestcolor < 0) { closestcolor = 0; }
  document.getElementById('wavecolor').innerHTML = aNames[closestcolor];
  document.legend.src="/images/legend"+aLambdas[closestcolor]+".jpg";
  if (topreview) { // if input preview is on
    var ifile = "/images/pbg/i" + (thisthick*3+20) + "0-" + thisangle*5 + ".gif";
    document.getElementById('overlay').src=ifile;
  }
  else { // input preview is not on
    document.getElementById('overlay').src='/images/blank.gif';
  }
  thisFrame = myFrame.getValue(0);
  if (thisFrame == "NaN") thisFrame = 1.0;
  theCount = parseInt(thisFrame) + 1;

  document.getElementById('framen').innerHTML = thisFrame;
  document.getElementById('timevalue').innerHTML = thisFrame*3;

  var pixdir = "/images/pbg/" + thislambda*2 + "0-" + (thisthick*3+20) + "0-" + thisangle*5 + "-";
  var pixfile = "";
  if ( theCount < 10 ) { pixfile = pixfile + "0"; }
  pixfile = pixfile + theCount;
  document.pix.src= pixdir + "s" + pixfile + ".jpg";
  document.getElementById('pixlink').href= pixdir + pixfile + ".jpg";
}
function UpdateIt () { Reinput(); }
function animate() {
    if (theCount == 16 ) { theCount = 2 }
    else { theCount++ }
    myFrame.setValue(theCount - 1);
    if (toanimate) { setTimeout("animate()", 300) }
}
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft;
    curtop  = obj.offsetTop;
    while ( obj = obj.offsetParent) {
      curleft += obj.offsetLeft;
      curtop  += obj.offsetTop;
    }
  }
  return [curleft, curtop];
}
