//
// javascript for MIT Facts site
//

////
//// The code below deals with the "keys" that highlight
//// navigation links. There are also functions to handle
//// the previous/next buttons, which partly depend on the keys.
////
////

///
/// Configuration
///

//
// background colors for moused-over and highlighted links
//
var keyovercol = "#aec973";    // light color used for mouseover
var keyhighcol = "#a3c16c";    // darker color used when key selected
var keyoffcol = "transparent"; // default (off) color

//
// path to the web site (to save browser from sending cookie to all MIT sites)
//
//var cookiepath = "/facts";
var cookiepath = "/";

//
// list of associations between keys and links
//
// (these just make the table a little easier to read)
var YYY = 1;
var nnn = 0;
// maximum key value
var maxkey = 4;
// the columns below:
//   url: the URL of the page (blank for the key links) (used by previous/next)
//   linkid: the id of the link
//   keys: which categories is the link/page in
//      0=unused, 1=Overview, 2=Education, 3=Research, 4=Community
var keydata = Array();
keydata.push({ url:"",                   linkid:"nkeyoverview",    keys:[ 0, YYY, nnn, nnn, nnn ]});
keydata.push({ url:"",                   linkid:"nkeyeducation",   keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"",                   linkid:"nkeyresearch",    keys:[ 0, nnn, nnn, YYY, nnn ]});
keydata.push({ url:"",                   linkid:"nkeycommunity",   keys:[ 0, nnn, nnn, nnn, YYY ]});
keydata.push({ url:"index.html",         linkid:"",                keys:[ 0, nnn, nnn, nnn, nnn ]});
keydata.push({ url:"focus.html",         linkid:"n1focus",         keys:[ 0, YYY, YYY, nnn, nnn ]});
keydata.push({ url:"mission.html",       linkid:"n1mission",       keys:[ 0, YYY, YYY, YYY, YYY ]});
keydata.push({ url:"presidents.html",    linkid:"n1presidents",    keys:[ 0, YYY, nnn, nnn, YYY ]});
keydata.push({ url:"admin-org.html",     linkid:"n1admin",         keys:[ 0, YYY, nnn, nnn, nnn ]});
keydata.push({ url:"campaign.html",      linkid:"n1campaign",      keys:[ 0, YYY, nnn, nnn, YYY ]});
keydata.push({ url:"academic.html",      linkid:"n1academic",      keys:[ 0, YYY, YYY, nnn, nnn ]});
keydata.push({ url:"accreditation.html", linkid:"n1accreditation", keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"undergraduate.html", linkid:"n1undergraduate", keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"graduate.html",      linkid:"n1graduate",      keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"research.html",      linkid:"n1research",      keys:[ 0, YYY, nnn, YYY, nnn ]});
keydata.push({ url:"lincoln.html",       linkid:"n1lincoln",       keys:[ 0, nnn, nnn, YYY, nnn ]});
keydata.push({ url:"campus.html",        linkid:"n1campus",        keys:[ 0, YYY, nnn, nnn, YYY ]});
keydata.push({ url:"faculty.html",       linkid:"n1faculty",       keys:[ 0, YYY, YYY, YYY, YYY ]});
keydata.push({ url:"enrollment.html",    linkid:"n1enrollment",    keys:[ 0, YYY, YYY, nnn, YYY ]});
keydata.push({ url:"international.html", linkid:"n1international", keys:[ 0, nnn, YYY, nnn, YYY ]});
keydata.push({ url:"admission.html",     linkid:"n1admission",     keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"tuition.html",       linkid:"n1tuition",       keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"housing.html",       linkid:"n1housing",       keys:[ 0, nnn, nnn, nnn, YYY ]});
keydata.push({ url:"computing.html",     linkid:"n1computing",     keys:[ 0, nnn, YYY, YYY, nnn ]});
keydata.push({ url:"libraries.html",     linkid:"n1libraries",     keys:[ 0, nnn, YYY, YYY, nnn ]});
keydata.push({ url:"press.html",         linkid:"n1press",         keys:[ 0, nnn, nnn, YYY, nnn ]});
keydata.push({ url:"arts.html",          linkid:"n1arts",          keys:[ 0, nnn, YYY, nnn, YYY ]});
keydata.push({ url:"athletics.html",     linkid:"n1athletics",     keys:[ 0, nnn, YYY, nnn, YYY ]});
keydata.push({ url:"community.html",     linkid:"n1community",     keys:[ 0, nnn, nnn, nnn, YYY ]});
keydata.push({ url:"industry.html",      linkid:"n1industry",      keys:[ 0, nnn, nnn, YYY, YYY ]});
keydata.push({ url:"partnerships.html",  linkid:"n1partnerships",  keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"alliances.html",     linkid:"n1alliances",     keys:[ 0, nnn, nnn, YYY, nnn ]});
keydata.push({ url:"ocw.html",           linkid:"n1ocw",           keys:[ 0, nnn, YYY, nnn, nnn ]});
keydata.push({ url:"graduation.html",    linkid:"n1graduation",    keys:[ 0, YYY, nnn, nnn, YYY ]});
keydata.push({ url:"alum.html",          linkid:"n1alum",          keys:[ 0, nnn, nnn, nnn, YYY ]});
keydata.push({ url:"geographic.html",    linkid:"n1geographic",    keys:[ 0, nnn, nnn, nnn, YYY ]});
keydata.push({ url:"financial.html",     linkid:"n1financial",     keys:[ 0, YYY, nnn, YYY, nnn ]});
keydata.push({ url:"faqs.html",          linkid:"n1faqs",          keys:[ 0, YYY, nnn, nnn, nnn ]});
keydata.push({ url:"map.html",           linkid:"n1map",           keys:[ 0, YYY, nnn, nnn, YYY ]});
//keydata.push({ url:"gallery.html",       linkid:"n1gallery",       keys:[ 0, nnn, nnn, nnn, nnn ]});


///
/// State variable: which key was last clicked
///
var currentkey = 0;

///
/// Functions called when user clicks or mouses over a key
///

// click a key: either turn off current key, or highlight the clicked key
function keyclick(key, linkobj) {
  // check if we should turn off current key
  if (key == currentkey) {
    key = 0;
  }
  // do highlighting and save key
  keyhighlight(key, false);
  currentkey = key;

  // save key to cookie
  save_key_to_cookie(currentkey);
  // remove focus from link
  if (linkobj)
    linkobj.blur();
  // return false so we don't hyperlink
  return false;
}
function keyover(key) {
  if (currentkey == 0) {
    keyhighlight(key, true);
  }
}
function keyout(key) {
  if (currentkey == 0) {
    keyhighlight(0, true);
  }
}

//
// work function: sets background colors of all links in keydata table
//  key: which key is active
//  over: true to use mouseover color, false to use highlight color
//
function keyhighlight(key, over) {
  // walk through all data
  for (var i in keydata) {
    // if there is a link...
    if (keydata[i].linkid.length > 0) {
      // get link object
      var obj = MM_findObj(keydata[i].linkid);
      if (obj && obj.style) {
        // if it should be turned on, set the background color
        if (keydata[i].keys[key]) {
          obj.style.backgroundColor = (over ? keyovercol : keyhighcol);
        }
        // otherwise set to off color
        else {
          obj.style.backgroundColor = keyoffcol;
        }
      }
    }
  }
}



///
/// Initialize key from cookie and highlight links
///
function initkey() {
  // get key from cookie
  var key = get_key_from_cookie();
  // bring into range
  if (key < 0 || key > maxkey)
    key = 0;
  // simulate click on a key
  keyclick(key, null);
}

///
/// Get/save key in cookie
///

// get current key from cookie
function get_key_from_cookie() {
  var key = 0;
  var cookies = document.cookie.split(';');
  for (i = 0; i < cookies.length; i++) {
    if (cookies[i].indexOf('mitfactskey') > -1) { // found in cookie
      cookieparts = cookies[i].split('=')
      key = parseInt(cookieparts[1]);
    }
  }
  return key;
}

// save current key to cookie
function save_key_to_cookie(key) {
  //var expire = new Date ();
  //expire.setTime (expire.getTime() + (10*365*24*60*60*1000)); //expires in 10 years
  //expire = '; expires='+expire.toGMTString();
  var expire = ''; // let it expire when browser closes
  var str = 'mitfactskey='+key+'; path='+cookiepath+expire;
  document.cookie=str;
}


///
/// Write the key links onto the page
///
function writekeys() {
  document.writeln("<div class='divnavsection divgraybottom'>");
  document.writeln("  <h2><a href='#' onclick='return keyclick(0, this)'><img src='img/nav/themes.gif' width='236' height='14' border='0' alt='Themes' title='' /></a></h2>");
  document.writeln("  <ul class='keys'>");
  document.writeln("    <li><a href='#' id='nkeyoverview' onclick='return keyclick(1, this)' onmouseover='keyover(1)' onmouseout='keyout(1)' title='(Click to lock &amp; unlock)'>Overview</a></li>");
  document.writeln("    <li><a href='#' id='nkeyeducation' onclick='return keyclick(2, this)' onmouseover='keyover(2)' onmouseout='keyout(2)' title='(Click to lock &amp; unlock)'>Education</a></li>");
  document.writeln("    <li><a href='#' id='nkeyresearch' onclick='return keyclick(3, this)' onmouseover='keyover(3)' onmouseout='keyout(3)' title='(Click to lock &amp; unlock)'>Research</a></li>");
  document.writeln("    <li><a href='#' id='nkeycommunity' onclick='return keyclick(4, this)' onmouseover='keyover(4)' onmouseout='keyout(4)' title='(Click to lock &amp; unlock)'>Community</a></li>");
  document.writeln("  </ul>");
  document.writeln("</div>");
}



///
/// Functions to handle previous/next page buttons
///

//
// Return the index in the keydata array of the current page. (-1 if not found)
//
function getpageindex() {
  var ipage = -1;
  var tmp = document.URL.split(/[\/\\]/);  // split URL on forward or backward slash
  var pageurl = tmp.pop();                 // get the last part

  // allow for URL ending in slash, which would only be the home page
  if (pageurl == "")
    pageurl = "index.html";

  // search through array to find matching URL
  for (var i = 0; i < keydata.length; i++) {
    if (keydata[i].url == pageurl) {
      ipage = i;
      break;
    }
  }
  return ipage;
}

//
// Next page
// If a key is selected (currentkey > 0), go to next page in group.
// Otherwise go to next page in list.
//
function nextpage() {
  // find out what page we are on right now
  var currentpage = getpageindex();

  // proceed only if we can identify current page
  if (currentpage >= 0) {
    var newpage = currentpage; // by default, stay on current page

    // key highlighted - find next page with current key
    if (currentkey > 0) {
      for (var i = 1; i < keydata.length; i++) {
        var maybepage = (currentpage+i) % keydata.length;
        // check that the entry has a URL, and that the keys match
        if (keydata[maybepage].url.length > 0 && keydata[maybepage].keys[currentkey]) {
          newpage = maybepage;
          break;
        }
      }

    }

    // no key highlighted, so go to next page
    else {
      for (var i = 1; i < keydata.length; i++) {
        var maybepage = (currentpage+i) % keydata.length;
        // check that the entry has a URL
        if (keydata[maybepage].url.length > 0) {
          newpage = maybepage;
          break;
        }
      }
    }
    window.location.href = keydata[newpage].url;
  }
}

//
// Previous page
// Like next page, except it looks backwards through the list
//
function previouspage() {
  // find out what page we are on right now
  var currentpage = getpageindex();

  // proceed only if we can identify current page
  if (currentpage >= 0) {
    var newpage = currentpage; // by default, stay on current page

    // key highlighted - find next page with current key
    if (currentkey > 0) {
      for (var i = keydata.length-1; i > 0; i--) {
        var maybepage = (currentpage+i) % keydata.length;
        // check that the entry has a URL, and that the keys match
        if (keydata[maybepage].url.length > 0 && keydata[maybepage].keys[currentkey]) {
          newpage = maybepage;
          break;
        }
      }

    }

    // no key highlighted, so go to next page
    else {
      for (var i = keydata.length-1; i > 0; i--) {
        var maybepage = (currentpage+i) % keydata.length;
        // check that the entry has a URL
        if (keydata[maybepage].url.length > 0) {
          newpage = maybepage;
          break;
        }
      }
    }
    window.location.href = keydata[newpage].url;
  }
}


///
/// Write previous/next buttons onto the page
///
function writeprevnext() {
  var currentpage = getpageindex();
  // proceed only if we can identify current page
  if (currentpage >= 0) {
    document.writeln("<div id='divprevious'><a href='javascript:previouspage()' onmouseover=\"MM_swapImage('ntprevious','','img/toolnav/previous_hi.gif',1)\" onmouseout='MM_swapImgRestore()'><img src='img/toolnav/previous.gif' width='69' height='17' border='0' alt='Previous page' title='Previous page' name='ntprevious' id='ntprevious' /></a></div>");
    document.writeln("<div id='divnext'><a href='javascript:nextpage()' onmouseover=\"MM_swapImage('ntnext','','img/toolnav/next_hi.gif',1)\" onmouseout='MM_swapImgRestore()'><img src='img/toolnav/next.gif' width='44' height='17' border='0' alt='Next page' title='Next page' name='ntnext' id='ntnext' /></a></div>");
  }
}
