// this is performed when the page loads
// (it probably should not be done from the body's onload attribute, but i can't do it via window.onload because of constructHintCounter())
// if there is no cookie yet set initial settings in togglerInitializeSettings (course navtree closed and author links open)

function togglerInitialSettings() {
  if (readCookie('cnx_course_navigation') == null) togglerInitializeSettings('cnx_course_navigation');
  if (readCookie('cnx_author_links') == null) togglerInitializeSettings('cnx_author_links');
}


// if user doesn't have a cookie set yet, set elements as follows:
// - course navigation tree is hidden
// - author links are shown
// these also override the default (hardcoded) settings for non-JS users which are:
// - course navigation tree is always shown
// - togglers ("show/hide" links) are not shown

function togglerInitializeSettings(id) {
  if (document.getElementById(id)) {
  // errors will result if we refer to this element without being in a course
    if (id == 'cnx_course_navigation') {
      document.getElementById(id + '_show').style.display = 'block';
      document.getElementById(id + '_hide').style.display = 'none';
      document.getElementById(id + '_contents').style.display = 'none';
    }
    if (id == 'cnx_author_links') {
      document.getElementById(id + '_togglers').style.display = 'block';
      document.getElementById(id + '_show').style.display = 'none';
      document.getElementById(id + '_hide').style.display = 'block';
      document.getElementById(id + '_contents').style.display = 'block';
    }
    createCookie(id, document.getElementById(id + '_contents').style.display, 365);
  }
}


// this is the function called by the user via the onclick handler

function toggler(id) {
  var contents = document.getElementById(id + '_contents').style;
  var show = document.getElementById(id + '_show').style;
  var hide = document.getElementById(id + '_hide').style;
  if (show.display == 'block' || show.display == '') {
    contents.display = 'block';
    show.display = 'none';
    hide.display = 'block';
  } else {
    contents.display = 'none';
    show.display = 'block';
    hide.display = 'none';
  }
  createCookie(id, document.getElementById(id + '_contents').style.display, 365);
}


// the cookie scripts were copied from here: http://www.xs4all.nl/~ppk/js/cookies.html
// which asks if i want to be redirected here: http://www.quirksmode.org/
// which has an apparently cnx-friendly copyright notice here: http://www.quirksmode.org/about/copyright.html

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } 
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
