/* 
   Copyright (c) 2005, Brad Neuberg, 
   bkn3@columbia.edu
   http://codinginparadise.org
   
   Permission is hereby granted, free of charge, 
   to any person obtaining a copy of this software
   and associated documentation files (the
   "Software"), to deal in the Software without 
   restriction, including without limitation 
   the rights to use, copy, modify, merge, 
   publish, distribute, sublicense, and/or sell 
   copies of the Software, and to permit persons 
   to whom the Software is furnished to do so, 
   subject to the following conditions:
   
   The above copyright notice and this 
   permission notice shall be included in all 
   copies or substantial portions of the Software.
   
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT 
   WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
   PURPOSE AND NONINFRINGEMENT. 
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
   HOLDERS BE LIABLE FOR ANY 
   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
   IN AN ACTION OF CONTRACT, TORT 
   OR OTHERWISE, ARISING FROM, OUT OF OR IN 
   CONNECTION WITH THE SOFTWARE OR 
   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// initialize ourselves when the page is finished
// loading
window.onload = initialize;

// an array of our topics
var topics = new Array();

function initialize() {
  // initialize the DhtmlHistory
  // framework
  dhtmlHistory.initialize();
  
  // if this is the first time the page
  // has loaded, fetch the list of
  // topics remotely
  if (dhtmlHistory.isFirstLoad()) {
    topics = loadTopics();  
    historyStorage.put("topics", topics);
  }
  else {
    // else, simply extract it from our
    // history storage *** Changed by Andrew due to excessive page caching
  //  topics = historyStorage.get("topics");
// the new script below solves the caching problem and allows for returns from other sites via the back button
    topics = loadTopics();  
    historyStorage.put("topics", topics);
  }
  
  // display our topics list
  displayTopicsList(); 
  
  // initialize our initial state from
  // the browser location after the hash
  var currentTopic = 
        dhtmlHistory.getCurrentLocation(); 
  displayTopic(currentTopic);
  
  // catch when a user clicks on a new
  // topic
  var menu = 
        document.getElementById("menu");
  xAddEventListener(menu, "click",
                    handleTopicChange,
                    false);
  
  // set ourselves up to listen to 
  // history events
  dhtmlHistory.addListener(
                    handleHistoryEvent);
}

function handleHistoryEvent(newLocation,
                            historyData) {
  var topicID = newLocation;
  
  // display this topic
  displayTopic(topicID);                      
}

function handleTopicChange(e) {
  var evt = new xEvent(e);
  var target = evt.target;
  var topicID = target.getAttribute("topicID");
  
  // display this topic
  var content = displayTopic(topicID);
  
  // add this to our history
  dhtmlHistory.add(topicID, content);
  
  // cancel the default behavior of hyperlinks
  return evt.cancel();
}

function displayTopic(topicID) {
  var topic;
  
  // if no topic passed in then get the
  // default topic
  if (topicID == null || 
      topicID == "") {
    for (var i = 0; i < topics.length; 
                                    i++) {
      if (topics[i].isDefault) {
        topic = topics[i];
        break;
      }                                  
    }
  }
  else {
    // fetch the topic with this ID
    for (var i = 0; i < topics.length; 
                                    i++) {
      if (topics[i].id == topicID) {
        topic = topics[i];
        break;
      }
    }
  }
    
  // see if we have cached the contents
  // of this topic in our history storage
  var content;
//  if (historyStorage.hasKey(topic.id)) {
//    content = historyStorage.get(
//                                topic.id);
//  }
//  else { 
{    
    // get the filename to load
    var url = topic.src;
               
    // load this file synchronously
    var request = new XMLHttpRequest();
    request.open("GET", url, false);
    request.send(null);
    content = request.responseText;
    
    // persist this value into our 
    // history storage
    historyStorage.put(topic.id, content);
  }
  
  // now place this content and title
  // into the HTML
  var topicTitle = 
    document.getElementById("topicTitle");
    if (topic.title == "Home" || topic.title == "home") {
       // do nothing
    } else {
          topicTitle.innerHTML = topic.title;
  }
  var topicContent =
    document.getElementById(
                          "topicContent");
  topicContent.innerHTML = content;
  
  return content;
}
function loadTopics() {

var topics = new Array();

/****** MENU ITEMS ******/

// Home
var home = new Object();
home.id = "home";
home.title="Home";
home.src="home.html";
home.isDefault = true;
topics.push(home);

// JHP Group Tools
var jhpgrouptools = new Object();
jhpgrouptools.id = "jhpgrouptools";
jhpgrouptools.title="JHP Group Tools";
jhpgrouptools.src="jhpgroup.html";
jhpgrouptools.isDefault = false;
topics.push(jhpgrouptools);

// Journels/Search Engines
var JournelsSearchEngines = new Object();
JournelsSearchEngines.id = "JournelsSearchEngines";
JournelsSearchEngines.title="Journels/Search Engines";
JournelsSearchEngines.src="onlinejournels.html";
JournelsSearchEngines.isDefault = false;
topics.push(JournelsSearchEngines);

// Materials Property DB
var materialsproperty = new Object();
materialsproperty.id = "materialsproperty";
materialsproperty.title="Materials Property DB";
materialsproperty.src="onlinematerials.html";
materialsproperty.isDefault = false;
topics.push(materialsproperty);

// Group Meeting Schedule
var group = new Object();
group.id = "group";
group.title="Group Meeting Schedule";
group.src="meetings.html";
group.isDefault = false;
topics.push(group);

// Downloads
var downloads = new Object();
downloads.id = "downloads";
downloads.title="Downloads";
downloads.src="downloads.html";
downloads.isDefault = false;
topics.push(downloads);

// Purchasing Information
var purchasinginformation = new Object();
purchasinginformation.id = "purchasinginformation";
purchasinginformation.title="Purchasing Information";
purchasinginformation.src= "purchasing.html";
purchasinginformation.isDefault = false;
topics.push(purchasinginformation);

// Transfer Files
var jftp = new Object();
jftp.id = "jftp";
jftp.title="Transfer Files";
jftp.src= "jftp.php";
jftp.isDefault = false;
topics.push(jftp);

// Search Papers
var searchi = new Object();
searchi.id = "searchi";
searchi.title="Search Papers";
searchi.src= "search.html";
searchi.isDefault = false;
topics.push(searchi);

// Upload Papers
/*var upload = new Object();
upload.id = "upload";
upload.title="Upload Papers";
upload.src= "upload.php";
upload.isDefault = false;
topics.push(upload); */

// Web Apps
var apps = new Object();
apps.id = "apps";
apps.title="Nucleus Apps";
apps.src= "apps.php";
apps.isDefault = false;
topics.push(apps);

// Divider
var topic10 = new Object();
topic10.id = "topic10";
topic10.title="----------------------------";
topic10.src= "";
topic10.isDefault = false;
topics.push(topic10);

// John H. Perepezko
var JohnPerepezko = new Object();
JohnPerepezko.id = "JohnPerepezko";
JohnPerepezko.title="John H. Perepezko";
JohnPerepezko.src= "http://nucleus.msae.wisc.edu/%7Eperepezk/";
JohnPerepezko.isDefault = false;
topics.push(JohnPerepezko);

// Seth Imhoff
var SethImhoff = new Object();
SethImhoff.id = "SethImhoff";
SethImhoff.title="Seth Imhoff";
SethImhoff.src= "sdimhoff.php";
SethImhoff.isDefault = false;
topics.push(SethImhoff);

// C. Santhaweesuk
var CharuaypornSanthaweesuk = new Object();
CharuaypornSanthaweesuk.id = "CharuaypornSanthaweesuk";
CharuaypornSanthaweesuk.title="C. Santhaweesuk";
CharuaypornSanthaweesuk.src= "charuays.php";
CharuaypornSanthaweesuk.isDefault = false;
topics.push(CharuaypornSanthaweesuk);

// Megan Jarosinski
var MeganJarosinski = new Object();
MeganJarosinski.id = "Megan Jarosinski";
MeganJarosinski.title="Megan Jarosinski";
MeganJarosinski.src= "megan.php";
MeganJarosinski.isDefault = false;
topics.push(MeganJarosinski);

// Peter Williams
var PeterWilliams = new Object();
PeterWilliams.id = "PeterWilliams";
PeterWilliams.title="Peter Williams";
PeterWilliams.src= "peter.php";
PeterWilliams.isDefault = false;
topics.push(PeterWilliams);

// Ridwan Sakidja
var RidwanSakidja = new Object();
RidwanSakidja.id = "RidwanSakidja";
RidwanSakidja.title="Ridwan Sakidja";
RidwanSakidja.src= "sakidja.php";
RidwanSakidja.isDefault = false;
topics.push(RidwanSakidja);

// Josh Bero 
var jbero = new Object();
jbero.id = "JoshBero";
jbero.title="Josh Bero";
jbero.src= "jbero.php";
jbero.isDefault = false;
topics.push(jbero);

// Travis Sossaman 
var ecolon = new Object();
ecolon.id = "TravisSossaman";
ecolon.title="Travis Sossaman";
ecolon.src= "sossaman.php";
ecolon.isDefault = false;
topics.push(ecolon);

// Hourlies 
var hourlies = new Object();
hourlies.id = "hourlies";
hourlies.title="Hourlies";
hourlies.src= "hourlies.php";
hourlies.isDefault = false;
topics.push(hourlies);

// Phone Numbers
var hourlies = new Object();
hourlies.id = "phones";
hourlies.title="Phone Numbers";
hourlies.src= "apps/docs/phones.php?home=true";
hourlies.isDefault = false;
topics.push(hourlies);



/****** ADD NEW MENU ITEMS HERE ******/

/*
// Travis Sossaman
var TravisSossaman = new Object();
TravisSossaman.id = "TravisSossaman";
TravisSossaman.title="Travis Sossaman";
TravisSossaman.src= "sossaman.php";
TravisSossaman.isDefault = false;
topics.push(TravisSossaman);

// Andrew Martin
var AndrewMartin = new Object();
AndrewMartin.id = "AndrewMartin";
AndrewMartin.title="Andrew Martin";
AndrewMartin.src= "asmartin.php";
AndrewMartin.isDefault = false;
topics.push(AndrewMartin);

// Paul Calhoun
var PaulCalhoun = new Object();
PaulCalhoun.id = "PaulCalhoun";
PaulCalhoun.title="Paul Calhoun";
PaulCalhoun.src= "paul.php";
PaulCalhoun.isDefault = false;
topics.push(PaulCalhoun);

// Cam Barnes
var CamBarnes = new Object();
CamBarnes.id = "CamBarnes";
CamBarnes.title="Cam Barnes";
CamBarnes.src= "barnes.php";
CamBarnes.isDefault = false;
topics.push(CamBarnes);
*/




return topics;

}
/* old function removed by Andrew. We now use the above function because some
// browsers had compatibility problems executing the Element.getAttribute() function below
// if using this function, you must include all the menu items in a file called menu.xml in the http_root
// if using this function, you must also add this to the header of index.php:
//      <!-- The Sarissa Library -->
 //    <script type="text/javascript"
 //            src="lib/sarissa/sarissa.js">
 //    </script>
function loadTopics() {
  // load our remote menu.xml document
  // synchronously into an XML DOM object
  var topicsXML = Sarissa.getDomDocument();
  topicsXML.async = false;
  topicsXML.load("menu.xml");
  
  // parse out our topics from the XML,
  // building up a JavaScript array that
  // mirrors these values
  var topics = new Array();
  var topicElements = 
    topicsXML.getElementsByTagName(
                                "topic");
  for (var i = 0; 
          i < topicElements.length;
          i++) {
    var currentTopic = new Object();
    currentTopic.id = 
          topicElements[i].getAttribute(
                                "id");
    currentTopic.title =
          topicElements[i].getAttribute(
                                "title");
    currentTopic.src =
          topicElements[i].getAttribute(
                                "src");
    currentTopic.isDefault =
          topicElements[i].getAttribute(
                             "default");
                               
    if (currentTopic.isDefault == null ||
        currentTopic.isDefault 
                            == undefined)
      currentTopic.isDefault = false;    
    
    // add a toString() method for 
    // debugging 
    currentTopic.toString = function() {
      return "[id="+this.id
             + ", title="+this.title
             + ", src="+this.src
             + ", isDefault="
             + this.isDefault
             + "]";
    };
    
    topics.push(currentTopic); 
  }
  
  return topics;
}
 */
function displayTopicsList() {
  var menu = 
          document.getElementById("menu");
  for (var i = 0; i < topics.length; 
                                i++) {
    // use each topic to update
    // our user interface
    var newTopic = 
          document.createElement("a");
    
    newTopic.href = topics[i].src;
    newTopic.title = topics[i].title;
    // note: avoid using the id attribute of 
    // hyperlinks if they will clash with a
    // location you store into history; if these
    // values are the same you can run into
    // some strange bugs in Internet Explorer;
    // to avoid this, we use setAttribute with
    // a custom attribute named "topicID"
    newTopic.setAttribute("topicID", topics[i].id);
    newTopic.innerHTML = topics[i].title;
    
    menu.appendChild(newTopic);
  }
}
