//Generic navigation and utility functions
function initImages () {
    var i, current, imageName
    for (imageName in document.images) {
        if (!workImage[imageName]) {
         current = -1;
         for (i=0; i<document.images.length; ++i) {
            if (document.images[i].name == imageName)
              current = i;
          }
	  if (current > -1) {
            workImage[imageName] = document.images[current];
          }
        }
    }
}

function NavBot (background, prevURL, nextURL, fyiURL, logURL, navURL) {
  this.bg = background;
  this.URL = new Object;
  this.URL["prev"] = prevURL;
  this.URL["next"] = nextURL;
  this.URL["fyi"] = fyiURL;
  this.URL["log"] = logURL;
  this.URL["nav"] = navURL;

  this.wow = "WOW";

  this.setBot = setBot;
  this.goBot = goBot;
  this.createBot = createBot;
  this.createTextBot = createTextBot;

  this.prev = new Image;
  this.prev.src = "images-nav/prev-" + this.bg + ".gif";
  this.next = new Image;
  this.next.src = "images-nav/next-" + this.bg + ".gif";
  this.log = new Image;
  this.log.src = "images-nav/teasers-" + this.bg + ".gif";
  this.fyi = new Image;
  this.fyi.src = "images-nav/facts-" + this.bg + ".gif";

  this.bot = new Object;
  this.bot["default"] = new Image;
  this.bot["default"].src = "images-nav/robot-" + this.bg + ".gif";
  this.bot["ul"] = new Image;
  this.bot["ul"].src = "images-nav/robot-ul-" + this.bg + ".gif";
  this.bot["ll"] = new Image;
  this.bot["ll"].src = "images-nav/robot-ll-" + this.bg + ".gif";
  this.bot["ur"] = new Image;
  this.bot["ur"].src = "images-nav/robot-ur-" + this.bg + ".gif";
  this.bot["lr"] = new Image;
  this.bot["lr"].src = "images-nav/robot-lr-" + this.bg + ".gif";
  this.bot["on"] = new Image;
  this.bot["on"].src = "images-nav/robot-on-" + this.bg + ".gif";
}

function setBot (dir) {
  if (document.Loaded) {
    workImage["robot"].src = this.bot[dir].src;
    if (dir == "ul") {window.status = "Previous Page"; return true;}
    if (dir == "ll") {window.status = "Light Fact"; return true;}
    if (dir == "ur") {window.status = "Next Page"; return true;}
    if (dir == "lr") {window.status = "Brain Teaser"; return true;}
    if (dir == "on") {window.status = "Navigation Help"; return true;}
    window.status = "";
    return false;
  }
}

function goBot (where) {
  if (this.URL[where] == "NONE") {
    alert ("Sorry, No " + where + " here!");
    return;
  }
  if (this.URL[where] == "BACK") {
    top.history.back();
    return;
  }
  if (where == "prev" || where=="next") {
    top.location = this.URL[where];
  } else {
    popup (this.URL[where], where);
  }
}

function createBot () {
   document.write ("<table border=0 cellpadding=0 cellspacing=0>\n");
   document.write ("<tr>\n");
   document.write ("<td valign=\"top\" align=\"right\">\n");
  if (this.URL["prev"] != "NONE") {
     document.write ("<a href=\"javascript:void navbot.goBot('prev')\" \n");
     document.write ("   onMouseOver=\"return navbot.setBot('ul')\"\n");
     document.write ("   onMouseOut=\"navbot.setBot('default')\"><img name=\"buttonPrev\" width=\"65\" height=\"27\" src=\"images-nav/prev-" + this.bg + ".gif\" border=0></a>\n");
   }
   document.write ("</td>\n");
   document.write ("<td valign=\"center\" align=\"center\" rowspan=2>\n");
   document.write ("<a href=\"javascript:void navbot.goBot('nav')\" \n");
   document.write ("   onMouseOver=\"return navbot.setBot('on')\"\n");
   document.write ("   onMouseOut=\"navbot.setBot('default')\"><img name=\"robot\" width=\"61\" height=\"50\" src=\"images-nav/robot-" + this.bg + ".gif\" border=0></a>\n");
   document.write ("</td>\n");
   document.write ("<td valign=\"top\" align=\"left\">\n");
   if (this.URL["next"] != "NONE") {
     document.write ("<a href=\"javascript:void navbot.goBot('next')\" \n");
     document.write ("   onMouseOver=\"return navbot.setBot('ur')\"\n");
     document.write ("   onMouseOut=\"navbot.setBot('default')\"><img name=\"buttonNext\" width=\"65\" height=\"27\" src=\"images-nav/next-" + this.bg + ".gif\" border=0></a>\n");
   }
   document.write ("</td>\n");
   document.write ("</tr><tr>\n");
   document.write ("<td valign=\"bottom\" align=\"right\">\n");
   if (this.URL["fyi"] != "NONE") {
     document.write ("<a href=\"javascript:void navbot.goBot('fyi')\" \n");
     document.write ("   onMouseOver=\"return navbot.setBot('ll')\"\n");
     document.write ("   onMouseOut=\"navbot.setBot('default')\"><img name=\"buttonFYI\" width=\"65\" height=\"44\" src=\"images-nav/facts-" + this.bg + ".gif\" border=0></a>\n");
   }
   document.write ("</td>\n");
   document.write ("<td valign=\"bottom\" align=\"left\">\n");
   if (this.URL["log"] != "NONE") {
     document.write ("<a href=\"javascript:void navbot.goBot('log')\" \n");
     document.write ("   onMouseOver=\"return navbot.setBot('lr')\"\n");
     document.write ("   onMouseOut=\"navbot.setBot('default')\"><img name=\"buttonLog\" width=\"65\" height=\"43\" src=\"images-nav/teasers-" + this.bg + ".gif\" border=0></a>\n");
   }
   document.write ("</td></tr></table>\n");
}

function createTextBot () {
  document.write ("|\n");
  if (this.URL["prev"] != "NONE") {
    document.write ("<a href=\"javascript:void navbot.goBot('prev')\" \n");
    document.write ("   onMouseOver=\"navbot.setBot('ul')\"\n");
    document.write ("   onMouseOut=\"navbot.setBot('default')\">Back</a>\n");
  }
  if (this.URL["next"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('next')\" \n");
    document.write ("   onMouseOver=\"navbot.setBot('ur')\"\n");
    document.write ("   onMouseOut=\"navbot.setBot('default')\">Forward</a>\n");
  }
  if (this.URL["fyi"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('fyi')\" \n");
    document.write ("   onMouseOver=\"navbot.setBot('ll')\"\n");
    document.write ("   onMouseOut=\"navbot.setBot('default')\">Light Facts</a>\n");
  }
  if (this.URL["log"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('log')\" \n");
    document.write ("   onMouseOver=\"navbot.setBot('lr')\"\n");
    document.write ("   onMouseOut=\"navbot.setBot('default')\">Brain Teasers</a>\n");
  }
  document.write ("|\n");
}

function popup (url, winName) {
  if (popUps[winName]) {
    if (popUps[winName].close) {
      popUps[winName].close();
    }
  }
  popUps[winName] = window.open(url, winName, "scrollbars,resizable,width=600,height=300");
}

function fullWindow (url) {
  window.open (url, "External", "toolbar,location,status,menubar,scrollbars,resizable,width=400,height=300");
}

function loadInit () {
  initImages();
  document.Loaded = true;
}

function Animate (name, image, baseURL, ext, delay, loop, nframes) {
  this.name = name;
  this.image = image;
  this.baseURL = baseURL;
  this.ext = ext;
  this.delay = delay;
  this.loop = loop;
  this.nframes = nframes;

  this.go = animateGo;
  this.stop = animateStop;

  this.images = new Array (nframes);
  for (i=0; i<nframes; ++i) {
    this.images[i] = new Image;
    this.images[i].src = baseURL + (i+1) + ext;
  }
  this.current = 0;
  this.done = false;
}

function animateGo () {
  if (!document.Loaded) {
    alert ("Animation has not finished loading yet!");
    return;
  }
  if (++this.current == this.nframes) {
    this.current = 0;
    this.done = !this.loop;
  }
  if (this.done) {
    this.done = false;
  } else {
    this.image.src = this.images[this.current].src;
    this.to = window.setTimeout (this.name + ".go()", this.delay);
  }
}

function animateStop () {
  window.clearTimeout (this.to);
  this.done = false;
  this.image.src = this.images[0].src;
}

function Button (name, imageURL, ext, status) {
    this.name = name;
    this.status = status;

    this.active = true;

    this.defaultImage = new Image;
    this.defaultImage.src = imageURL + ext;
    this.onImage = new Image;
    this.onImage.src = imageURL + "-on" + ext;
    this.downImage = new Image;
    this.downImage.src = imageURL + "-down" + ext;
    this.inactiveImage = new Image;
    this.inactiveImage.src = imageURL + "-done" + ext;
     
    this.on = buttonOn;
    this.down = buttonDown;
    this.def = buttonDefault;
    this.deactivate = buttonDeactivate;
    this.activate = buttonActivate;
}

function buttonOn () {
    if (this.active && document.Loaded) {
      workImage[this.name].src = this.onImage.src;
      if (this.status != "") {
        window.status = this.status;
        return true;
      }
    }
    return false;
}

function buttonDefault () {
  if (document.Loaded) {
    workImage[this.name].src = this.defaultImage.src;
    if (this.status != "") {
      window.status = "";
      return true;
    }
  }
  return false;
}

function buttonDown () {
   if (this.active && document.Loaded) {workImage[this.name].src = this.downImage.src;}
   return false;
}

function buttonDeactivate () {
  if (document.Loaded) {
    workImage[this.name].src = this.inactiveImage.src;
    this.active = false;
    return true;
  }
}

function buttonActivate () {
  this.active = true;
  this.def ();
  return true;
}

function closeOut () {
  var popup;
  for (popup in popUps) {
    if (popUps[popup].close) {
      popUps[popup].close();
    }
  }
}

function glossary (term) {
  var winName = "glossary";
  var url = "../glossary/" + term + ".html";
  if (popUps[winName]) {
    if (popUps[winName].close) {
      popUps[winName].close();
    }
  }
  popUps[winName] = window.open(url, winName, "scrollbars,resizable,width=400,height=100");
}

// Added 7/7/99 to handle new brain-teaser format - EW
function answer (term) {
  var winName = "answer";
  var url = "brain-answers/brain-answer-" + term + "_nf.html";
  if (popUps[winName]) {
    if (popUps[winName].close) {
      popUps[winName].close();
    }
  }
  popUps[winName] = window.open(url, winName, "scrollbars,resizable,width=400,height=300");
}

function Reload (imgName, imgSrc) {
  this.name = imgName;
  this.src = imgSrc;

  this.reload = ReloadImage;  
}

function ReloadImage () {
  workImage[this.name].src = this.src;
}

// Added 9/2/99 to handle popup status
function go (where)
  {
    window.open(where,"original");
    top.window.close();
  }

var workImage = new Object;
var popUps = new Object;
workImage.IsLoaded = false;
