//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) {
    MM_findObj("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);
	STSCI_openMBrWindow(this.URL[where], where, "scrollbars,resizable,width=600,height=300");
  }
}

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')\">Back</a>\n");
  }
  if (this.URL["next"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('next')\">Forward</a>\n");
  }
  if (this.URL["fyi"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('fyi')\">Light Facts</a>\n");
  }
  if (this.URL["log"] != "NONE") {
    document.write ("| \n");
    document.write ("<a href=\"javascript:void navbot.goBot('log')\">Brain Teasers</a>\n");
  }
  document.write ("|\n");
}

function createTextBotOrig () {
  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) {
  STSCI_openMBrWindow(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;
  STScI_windowOpen();
}

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 MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

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

	// Preload the default and on images....
    this.defaultImage = new Image;
    this.defaultImage.src = imageURL + ext;
    this.onImage = new Image;
    this.onImage.src = imageURL + "-on" + ext;
	
	// ...but don't preload the down and inactive images.
    this.downImage = new Object;
    this.downImage.src = imageURL + "-down" + ext;
    this.inactiveImage = new Object;
    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) {
      MM_findObj(this.name).src = this.onImage.src;
      if (this.status != "") {
        window.status = this.status;
        return true;
      }
    }
    return false;
}

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

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

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

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

function closeOut() {
  STSCI_closeMBrWindows();
  STScI_windowClose();
}

function glossary (term) {
  var url = "../glossary/" + term + ".html";
  STSCI_openMBrWindow(url,"glossary", "scrollbars,resizable,width=400,height=100");
}

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

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

  this.reload = ReloadImage;  
}

function ReloadImage () {
  MM_findObj(this.name).src = this.src;
}

// Added 9/2/99 to handle popup status
function go (where)
  {
    window.open(where,"original");
    top.window.close();
  }
  
// radioValue: Return the value of the selected radio object.
function radioValue(r) {
  var v = null;
  for (var i=0; i<r.length; i++) {
    if (r[i].checked)
	  v = r[i].value;
  }
  return v;
}

// radioSetCheck: Set the checked radio value.
function radioSetCheck(r, v) {
  for (var i=0; i<r.length; i++) {
    if (r[i].value == v)
	  r[i].checked = true;
  }
}

// Yet another popup-window handling routines.  Should be better written. ?
function STSCI_closeMBrWindows() {
  for (var i in document.STScI_popups) {
    if (document.STScI_popupsState[i] == "open") {
      document.STScI_popups[i].close();
	}
  }
  document.MM_returnValue = false;
}
function STSCI_openMBrWindow(theURL,winName,features) {
  if (document.STScI_popupsState[winName]) {
    if (document.STScI_popupsState[winName] == "open") {
	  if (document.STScI_popups[winName].close)
        document.STScI_popups[winName].close();
	}
  }
  //document.STScI_popups[winName] = window.open(theURL,winName,features);
	if ((a = request_popup_window(theURL, winName, features, top.popup_warning_target)) != null) {
  	document.STScI_popups[winName] = a;
	}
  document.MM_returnValue = false;
}

function STScI_windowClose() {
  if (window.opener)
    if (window.opener.setClose)
      window.opener.setClose(window.name);
}

function STScI_windowOpen() {
  if (window.opener)
    if (window.opener.setOpen)
	  window.opener.setOpen(window.name);
}

function setOpen(winName) {
  document.STScI_popupsState[winName] = "open";
}

function setClose(winName) {
  document.STScI_popupsState[winName] = "close";
}

/**
 * Gracefully attempt to open a popup window.  If the window is blocked by a popup blocker,
 * report this to the visitor and provide a link which will let them open the window.
 * Actions taken by a user (clicking a link, for example) will usually be allowed by
 * popular popup blockers.
 */
function request_popup_window(url, name, features, target) {
	var opened_window = window.open(url, name, features);
  if ((opened_window == null) || (typeof(opened_window.closed) == "undefined")) {
		if (target != null) {
			var aTag = document.createElement("div");
			aTag.onclick = function() {
				request_popup_window(url, name, features, target); 
				aTag.innerHTML = "";
			}
			aTag.innerHTML = "It looks like your popup blocker is enabled.  Click here to open the requested popup window.";
			target.innerHTML = "";
			target.appendChild(aTag);			
		}
	}

	return opened_window;
}

// General initializations.
var workImage = new Object;
var popUps = new Object;
document.STScI_popups = new Array;
document.STScI_popupsState = new Array;

workImage.IsLoaded = false;
