// JavaScript Document
     if (document.getElementsByTagName){
  //append this style to the page. the style hides all of the menu options when page loads.
    var myCSS = ".menu-option{display:none;} body{display:none;}";
    var hstyle = document.createElement("style");
    hstyle.setAttribute("type", "text/css");
    //only IE uses style.styleSheet, the rest use style.sheet
    if(hstyle.styleSheet){
    //Many browsers won't let you change the style's cssText, but IE does.
      hstyle.styleSheet.cssText = myCSS;
    } else {
    //this is the standard way to do it, which IE does not support.
      myCSS = document.createTextNode(myCSS);
      hstyle.appendChild(myCSS);
    }
    document.getElementsByTagName("head")[0].appendChild(hstyle);
  }


function displayLinks(node) {
  //window.alert(node.nodeName)
  el = node.nextSibling;
  while (el){
    if (el.className == "menu-option"){
      if (! el.style.display){
        el.style.display = "block";
		node.className = "menu-heading active";
      }
      else if (el.style.display == "none"){
        el.style.display = "block";
		node.className = "menu-heading active";
      }
      else{
        el.style.display = "none";
		node.className = "menu-heading";
      }

    }
    if (el.className && el.className.indexOf("menu-heading") > -1 ){
      el=0;
	  //stop loop
    }
    el = el.nextSibling;
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(collapseList);
addLoadEvent(function(){
  var body = document.getElementsByTagName('body')[0];
  body.style.display = "block";
})


function collapseList() {
  var link = window.location;
  var linksplit = link.href.split("/");
  var page = linksplit[linksplit.length-1];
  var pagepath = link.pathname;
  if (page == ""){
    page = "index.html"
  }
  //window.alert(link.pathname);
  if (document.getElementById) {
    links = document.getElementsByTagName("li");
    var menulinks = new Array();
    for (i=0; i < links.length; i++){
      if (links[i].className.indexOf("menu-heading") > -1 || links[i].className == "menu-option"){
        menulinks.push(links[i]);
      }
    }
    for (i=0; i < menulinks.length; i++) {
      node = menulinks[i];
      if (node.className.indexOf("menu-heading") > -1) {
        var ctrigger = 0;
        var uncollapse = new Array();
        if (menulinks[i+1] && menulinks[i+1].className == "menu-option"){
		//if there are links underneath a menu heading, make the menu click action unhide it's links
          node.onclick = function () {
            displayLinks(this);
            return false;
          }
        //var ctrigger = 0;
          //document.write("<h3>"+node.childNodes[1].firstChild.nodeValue+"</h3>");
        //var uncollapse = new Array();
          //for (j=i+1; menulinks[j].className != "menu-heading"; j++){
          //for (j=i+1; menulinks[j].className != 'menu-heading'; j++){
          for (j=i+1; j<menulinks.length; j++){ 
		  //loop through menu options
               var liNode = menulinks[j];
		  	   if (liNode.className.indexOf( "menu-heading" ) > -1 ){
				   break;	
			   }
               for (var a=0; a < liNode.childNodes.length; a++){
                   if (liNode.childNodes[a].tagName == "A"){
				   //if this list item has a link, store it as linknode
                      var linknode = liNode.childNodes[a];
                   }
               }
               if (linknode && liNode.className == "menu-option"){//&& liNode.className == "menu-option"
                   uncollapse.push(liNode);
                   var linkref = linknode.getAttribute('href');
				   linkref = linkref.replace("..", "");
                   var linkrefsplit = linkref.split('?');
				   var linkpath = '/';
				   linkref = linkrefsplit[0];
				   linkref = linkref.replace("http://www.unf.edu", "");
				   if (linkrefsplit.length > 4){
				      for (var a=3; a < linkrefsplit.length; a++){
				         linkpath+=linkrefsplit[a]+'/';	
					  }
					  linkpath = linkpath.substring(0, linkpath.length-1);
				   }
				   else
				     linkpath = 'index.html';
                   //linkref = linkrefsplit[linkrefsplit.length-1];
                   if (pagepath.indexOf(linkref) > -1){
				   	   //window.alert("trigger"+page+" "+linkref);
                       ctrigger = 1;
                   }
               }
          }
          //window.alert(liNode.parentNode.lastChild.lastChild.firstChild.nodeValue);
          if (ctrigger == 1){
            for (j=0; j < uncollapse.length; j++){
              //window.alert("trigger");
              uncollapse[j].style.display = "block";
            }
          }
        }
      }
    }
  }
}

