/*
 * Year In Review navigation javascript.
 *
 * Naming conventions:
 * - nav: The main (top) nav buttons.
 * - subnav: The second-level nav buttons that may be associated with
 *   a top nav button.
 *
 * Nav button states:
 * - NAV_ACTIVE, SUBNAV_ACTIVE: The nav item (button) corresponding to the
 *   page currently being viewed.
 * - nav_highlighted: The nav item being hovered over. It
 *   may be the same as NAV_ACTIVE.
 * - subnav_displayed: The group of subnav items that are currently being
 *   displayed. On page load, this should be the same as SUBNAV_ACTIVE.
 *
 */

/*
 * Called onmouseover of a nav item/button.
 *
 * Args
 * - img: the image that has been moused over.
 *
 * Optional Args:
 * - id of corresponding subnav div
 */
function nav_over(img) {
  idx = get_preload_idx(img.src);
  
  /* Turn previously highlighted nav item off if necessary. */
  if (nav_highlighted == idx) {
    // Mousing back into the same (inactive) nav item before timer expires.
    clearTimeout(nav_off_timer);
  } else if (nav_highlighted != NAV_ACTIVE) {
    // Mousing into an inactive, not already highlighted nav item.
    highlighted_nav_off_now();
  }

  /* Turn this nav item on if necessary. */
  if (idx != NAV_ACTIVE && idx != nav_highlighted) {
    img.src = nav_on[idx].src;
    nav_highlighted = idx;
  }

  /* Subnav */
  subnav = "";
  if (arguments.length > 1) {
    subnav = arguments[1];
  }
  
  if (subnav == subnav_displayed) {
    clearTimeout(subnav_hide_timer);
    return null;
  }
  
  if (subnav && subnav_displayed) {
    // nav item with subnav -> nav item with subnav
    if (subnav_displayed && subnav != subnav_displayed) {
      document.getElementById(subnav_displayed).style.display = 'none';
      document.getElementById(subnav).style.display = 'block';
      subnav_displayed = subnav;
    }
  } 
  // nav item with subnav -> nav item without subnav
  else if (!subnav && subnav_displayed) {
    document.getElementById(subnav_displayed).style.display = 'none';
    subnav_displayed = null;
  }
  // nav item without subnav -> nav item with subnav
  else if (subnav && !subnav_displayed) {
    document.getElementById(subnav).style.display = 'block';
    subnav_displayed = subnav;
  }
}

/*
 * Given the source string for an image, return its position in the nav_on and
 * nav_off image arrays. If the image is not found in the array, null is
 * returned.
 *
 * Args
 * - src: The full source string for an image, e.g.,
 *   http://yearinreview.duke.edu/images/nav_president_on.gif
 */
function get_preload_idx(src) {
  //
  // Slice out pertinent part of image filename.
  //
  var arr = src.split('/');
  var name = arr[arr.length-1];
  name = name.slice(0, name.lastIndexOf('_'));

  // Search array in preload.js for image's index and return it.
  for (var i = 0; i < nav_off.length; i++) {
    if (nav_off[i].src.indexOf(name) != -1) {
      return i;
    }
  }

  // We shouldn't get here, there was an error.
  return null;
}

/*
 * Turn off the currently highlighted nav item immediately.
 */
function highlighted_nav_off_now() {
  clearTimeout(nav_off_timer);
  turn_nav_off(nav_highlighted);
}

/*
 * Called onmouseout of a nav item/button.
 *
 * Args
 * - img: the image that has been moused over.
 */
function nav_out(img) {
  idx = get_preload_idx(img.src);
  if (idx != NAV_ACTIVE) {
    start_nav_off_timer(idx);
  }
}

/*
 * Kickoff timer to turn a nav item off.
 *
 * Args
 * - idx: index of the nav item image in preloaded array.
 */
function start_nav_off_timer(idx) {
  var code = "turn_nav_off('" + idx + "')";
  nav_off_timer = setTimeout(code, TIMER_DURATION);
}

/*
 * Turn a nav item off, update state. May also update subnav display.
 *
 * Args
 * - idx: index of the nav item image in preloaded array.
 */
function turn_nav_off(idx) {
  /* Nav */
  document.getElementById("nav_" + idx).src = nav_off[idx].src;
  nav_highlighted = NAV_ACTIVE;
  
  /* Subnav */
  if (subnav_displayed != SUBNAV_ACTIVE) {
    if (subnav_displayed) {
      document.getElementById(subnav_displayed).style.display = 'none';
    }
    if (SUBNAV_ACTIVE) {
      document.getElementById(SUBNAV_ACTIVE).style.display = 'block';
    }
    subnav_displayed = SUBNAV_ACTIVE; 
  }
}

/*
 * Called onmouseover of a subnav container.
 */
function subnav_over() {
  clearTimeout(nav_off_timer);
  clearTimeout(subnav_hide_timer);
}

/*
 * Called onmouseout of a subnav container.
 *
 * Args
 * - container_elem: subnav items container.
 */
function subnav_out(container_elem) {
  if (subnav_displayed != SUBNAV_ACTIVE) {
    start_subnav_hide_timer(container_elem.id);
  }
  if (nav_highlighted != NAV_ACTIVE) {
    start_nav_off_timer(nav_highlighted);
  }
}

/*
 * Show a subnav group.
 *
 * Args
 * - subnav: the id of the subnav group's container element.
 */
function show_subnav(subnav) {
  document.getElementById(subnav).style.display = 'block';
}

/*
 * Kickoff the timer to hide subnav.
 */
function start_subnav_hide_timer(subnav_id) {
  var code = "hide_subnav('" + subnav_id + "')";
  subnav_hide_timer = setTimeout(code, TIMER_DURATION);
}
function hide_subnav(subnav_id) {
   document.getElementById(subnav_id).style.display = 'none';
   if (SUBNAV_ACTIVE) {
     document.getElementById(SUBNAV_ACTIVE).style.display = 'block';
     subnav_displayed = SUBNAV_ACTIVE;
   }
   if (nav_highlighted != NAV_ACTIVE) {
   }
}

/*
 * Previous reports functions.
 */
function start_prevrep_hide_timer() {
  prevrep_timer = setTimeout("hide_prevrep()", TIMER_DURATION);
}
function hide_prevrep() {
  // Change the color of 'Previous Reports'
  document.getElementById('previous_reports').style.color = '#996600';
  document.getElementById('previous_reports_subnav').style.display = "none";
}
function cancel_prevrep_hide_timer() {
  clearTimeout(prevrep_timer);
}
function prevrep_over() {
  cancel_prevrep_hide_timer();
  // Change the color of 'Previous Reports'
  document.getElementById("previous_reports").style.color = '#666666';
  document.getElementById("previous_reports_subnav").style.display = "block";
}