﻿/*
	Brandeis University
	"Epsilon" Theme - Menu Expander Script
	Based on menuExpandable3.js by Dave Lindquist (dave@gazingus.org)
	Originally edited by Danny Silverman (zeno@brandeis.edu)
	Significant changes by Ian Rifkin (irifkin@brandeis.edu)
 */
 
 function assignIdClass()
 {
  treeID = document.getElementById('tree');
  ullistFind = treeID.getElementsByTagName('ul');
  ullist = ullistFind.item(0);
  ullist.id="list";
  lichild = ullist.getElementsByTagName("li");
  hrefchild = lichild.item(0).getElementsByTagName('a');
  ulchild = hrefchild.item(0).getElementsByTagName('ul');
  x=1;
  lisibling = lichild.item(0);
  while (lisibling != null)
  {
   if (lisibling.getElementsByTagName('ul').item(0) != null)
   {
   lisibling.className='t';
   lisibling.childNodes[0].className='trigger';
   lisibling.childNodes[0].id='t-' + x;
   lisibling.childNodes[2].className='l2';
   lisibling.childNodes[2].id=x;
   x++;
   }
	if (navigator.appVersion.indexOf("Win")!=-1 && navigator.appName == "Microsoft Internet Explorer")
	{
  	 lisibling = lisibling.nextSibling;
   	}
   	else
   	{
	 lisibling = lisibling.nextSibling.nextSibling;
   	}
   }
  }
function initTree(menuId, section) {
	triggerId = 't-' + menuId;
	var menu = document.getElementById(menuId);
	var trigger = document.getElementById(triggerId);
	if (menu == null || trigger == null) return;
	trigger.onclick = function() {
		var display = menu.style.display;
		if(display == 'block') {
			this.parentNode.style.backgroundImage = 'url(/epsilon/' + section + '/images/closed.gif)';
			menu.style.display = 'none';
			return false;
		} else {
			this.parentNode.style.backgroundImage = 'url(/epsilon/' + section + '/images/open.gif)';
			menu.style.display = 'block';
		}
	}
}

function selectedTree(menuId, section) {
	triggerId = 't-' + menuId;
	var menu = document.getElementById(menuId);
	var trigger = document.getElementById(triggerId);
	if (menu == null || trigger == null) return;
	trigger.parentNode.style.backgroundImage = 'url(/epsilon/' + section + '/images/open.gif)';
	var display = menu.style.display;
	menu.style.display = 'block';
	return false;
}

function makeActive()
{
 treeID = document.getElementById('tree');
 assignIdClass();
 fullurl = document.location.href;
 // The following removes index.*.* from the current page's URL
 if (fullurl.indexOf("index") != -1)
 {
  fullurl = fullurl.substring(0, fullurl.indexOf("index"));
 }
 anchorTags =  treeID.getElementsByTagName("a");
 // The browser handles translating the above to a full URL
 i=0;
 while (i < anchorTags.length)
 {
  // The following removes index.*.* from link
  if (anchorTags[i].href.indexOf("index") != -1)
  {
   noIndexHref = anchorTags[i].href.substring(0, anchorTags[i].href.indexOf("index"));
  }
  else
  {
   //Link does not contain index.*.*
   noIndexHref = anchorTags[i].href;
  }
  //take off slash at end of link
  if (noIndexHref.charAt(noIndexHref.length-1) == '/')
  {
   noIndexHref = noIndexHref.substring(0, noIndexHref.length-1);
  }
  //take off slash at end of URL
  if (fullurl.charAt(fullurl.length-1) == "/")
  {
   fullurl = fullurl.substring(0, fullurl.length-1);
  }

  //Does the link match the URL of the current page && the link is not just a #
  if (noIndexHref == fullurl && anchorTags[i].href.charAt(anchorTags[i].href.length-1) != "#")
  {
   //The following checks if the href is a heading
   if (anchorTags[i].parentNode.parentNode.parentNode.id == "tree" && anchorTags[i].parentNode.getElementsByTagName('li').item(0) != null)
   {
    // the following opens up the selected tree
    theul = anchorTags[i].parentNode.getElementsByTagName('ul').item(0).id;
    selectedTree(theul, "default");
   }
   else
   {
    // theul must be a UL and the anchor tag's parent must be an LI
    theul = anchorTags[i].parentNode.parentNode;
// check if it is a 2nd level link with 3rd level links below it
if (anchorTags[i].parentNode.childNodes[2] != null)
	{
	// just make the link active (highlighting the text)
    anchorTags[i].className='active';
    }
else{
	// make the link and the li active, to highlight the whole piece
    anchorTags[i].className='active';
    anchorTags[i].parentNode.className='active';
	}
// the following opens up the selected tree
	// if theul's parents (an li) parents (the real ul) parent = div id=tree
	if (theul.parentNode.parentNode.parentNode.id == "tree")
	{
		// open theul
    	selectedTree(theul.id, "default");
    }
    else
    {
		// else open theul's parent parents
    	selectedTree(theul.parentNode.parentNode.id, "default");
    }
   }
  }
  i++;
 }
}

function treenumber()
{
	treeID = document.getElementById("tree");
	howmanyUL = treeID.getElementsByTagName("ul");
	x = 1;
	while (x <= howmanyUL.length)
	{
		initTree(x, "default");
		x++;
	}
}