/*-----------------------------------------------------------------
               Expanding menu and breadcrumbs script 
                           Version 1.0
 
             Created by Stephen Davies and Iain Pople,
        University Web Centre, The University of Melbourne
  For more information contact web-info@webcentre.unimelb.edu.au
            
 Copyright 2004. The University of Melbourne. All rights reserved.
-----------------------------------------------------------------*/

// Define some global variables
imageDir = 'http://undergraduate.law.unimelb.edu.au/wsimages/';
var menuNode = [];
// This variable prevents double running IE Mac after pressing back
var menuSetState = false;
var submenuId = 0;   //ensure ULs are uniquely numbered when multiple menus

function menuSet(menuId) {

//This IF statement is not necessary if the web page is written so that menuSet is always called with a valid menuId parameter
if (arguments.length==0) {
	menuId='topmenu';
}

if (!menuSetState) {	
  menuSetState = true;  //remove to support multiple menus
  if (document.getElementById && document.createElement) {
    // Is there a valid menu in the document?
	if (! document.getElementById(menuId)) {
		return;
	}
	var lis = document.getElementById(menuId).getElementsByTagName('li');
    var uls = document.getElementById(menuId).getElementsByTagName('ul');
    var ans = document.getElementById(menuId).getElementsByTagName('a');
    var fpath = (document.location.pathname);
	var fpathIE = document.location.protocol+'//'+document.location.hostname+document.location.pathname;
    var b, ref, cur, k, q, z, plist, control;

	if (! window.menuParent) {
		 menuParent = null;
	}

   // Identify the current page and highlight it appropriately.
    for (b = 0; b < ans.length; b++) {
      ref = ans[b].getAttribute('href');
      cur = ans[b];
    
      if ((ref == fpath ||  ref == fpathIE || ref == menuParent
		   || ref == (document.location.protocol+'//'+document.location.hostname+menuParent) 
		   || ref == theFU 	|| ref == theFUIE 		)
		   && cur.className != 'duplicate') {
        
        cur.className = ('current');
        // Current is Level 1 menu Item
        if (cur.parentNode.parentNode.id == menuId) {
          cur.parentNode.id = "cursec";
        }
        // Current is Level 2 menu item
        else if (cur.parentNode.parentNode.parentNode.parentNode.id == menuId) {
          cur.parentNode.parentNode.parentNode.id = "cursec";
          cur.parentNode.id = "cursubsec";
        }    
        // Current is Level 3 menu item
        else {
          cur.parentNode.parentNode.parentNode.parentNode.parentNode.id = "cursec";
          cur.parentNode.parentNode.parentNode.id = "cursubsec";
        }
		break;
      }
    }
    
    // breadcrumb script
    if (false) {
      var bcdiv = document.getElementById('breadcrumbs')
      var delimiter = ' > ';
      var bchref, bclink, delim, crumb;

      for (var f = 0; f < lis.length; f++) {
        if (lis[f].id || lis[f].getElementsByTagName('a')[0].className == 'current') {
          delim = document.createTextNode(delimiter);
          bcdiv.appendChild(delim);
          if (lis[f].getElementsByTagName('a')[0].className == 'current') {
            bclink = lis[f].getElementsByTagName('a')[0].firstChild.nodeValue;
            crumb = document.createTextNode(bclink);
            bcdiv.appendChild(crumb);
          }
          else {
            a = document.createElement('a');
            bchref = lis[f].getElementsByTagName('a')[0].getAttribute('href');
            a.setAttribute('href', bchref); 
            bclink = lis[f].getElementsByTagName('a')[0].firstChild.nodeValue;
            crumb = document.createTextNode(bclink);
            a.appendChild(crumb);
            bcdiv.appendChild(a);
          }
        }
      }
    }

    
     // Add bullet image for LIs with no children
    for (k = 0; k < lis.length; k++) {
      
      if (lis[k].getElementsByTagName('ul').length < 1) {
        
        q = document.createElement('img');
        q.setAttribute('src', imageDir+'dot.gif');
        q.setAttribute('style','display: inline');
        q.setAttribute('height', '13');
        q.setAttribute('width', '12');
        q.setAttribute('alt', '');
        q.setAttribute('border', '0');
        lis[k].insertBefore(q, lis[k].firstChild);
      }
    }
    
    for (z = 0; z < uls.length; z++) {
    
      uls[z].id = submenuId;
      menuControl(submenuId, 'x', uls[z].parentNode.firstChild.firstChild.nodeValue, menuId);
      control = menuControl(submenuId, 'c', uls[z].parentNode.firstChild.firstChild.nodeValue, menuId); 
      plist = uls[z].parentNode;
      
      // If not current section then UL should be collapsed
      if (!plist.id) {
        uls[z].style.display = 'none';
        control = menuNode[submenuId+'x'];
      }
      plist.insertBefore(control, plist.firstChild);
	  submenuId++;
   }


  }
}
}

function menuControl(idnum, xorc, titl, menuId) {
  var i = document.createElement('img');
  var a = document.createElement('a');  
  if (xorc == 'x') {
    i.setAttribute('src', imageDir+'plus.gif');
    i.setAttribute('alt', 'expand '+titl+' menu');
    a.setAttribute('href', 'javascript:menuExpand(\''+idnum+'\',\''+menuId+'\');');
    a.setAttribute('title', 'expand '+titl+' menu');
  }
  else {
    i.setAttribute('src', imageDir+'minus.gif');
    i.setAttribute('alt', 'collapse '+titl+' menu');
    a.setAttribute('href', 'javascript:menuCollapse(\''+idnum+'\',\''+menuId+'\');');
    a.setAttribute('title', 'collapse '+titl+' menu');
  }
  i.setAttribute('height', '13');
  i.setAttribute('width', '12');
  i.setAttribute('style','display: inline');
  i.setAttribute('border', '0');
  a.appendChild(i);
  return menuNode[idnum+xorc] = a;
}

function menuCollapse(ulid, menuId) {
//This IF statement is not necessary if the web page is written so that menuSet is always called with a valid menuId parameter 
if (arguments.length == 1) {
	menuId='topmenu';
 }

 var tempsubsec;
 var ul = document.getElementById(ulid);
 ul.style.display = 'none';
 ul.parentNode.replaceChild(menuNode[ulid+'x'], menuNode[ulid+'c']);
 // Remove tempsec ID now that UL has been closed
 if (ul.parentNode.id == 'tempsec' || ul.parentNode.id == 'tempsubsec') {
  // For IE Mac
  ul.parentNode.id = '';
  // Other Browsers
  ul.parentNode.removeAttribute('id');
 }
}

function menuExpand(ulid, menuId) {
//This IF statement is not necessary if the web page is written so that menuSet is always called with a valid menuId parameter
 if (arguments.length == 1) {
	menuId='topmenu';
 }

 var oldtempsec, cursec, cursubsec, cursecUl, cursubsecUl;
 var ul = document.getElementById(ulid);

 // Highlight open section
 // Level 2
 if (ul.parentNode.parentNode.parentNode.parentNode.id == menuId) {
  // Close open Sub Section
  if (oldtempsubsec = document.getElementById('tempsubsec')) {
   menuCollapse(oldtempsubsec.getElementsByTagName('ul')[0].id, menuId);   
  }
  // Close current subsection if open
  if (cursubsec = document.getElementById('cursubsec')) {
   if (cursubsecUl = cursubsec.getElementsByTagName('ul')[0]) {
	   if (cursubsecUl.style.display != 'none') {
		menuCollapse(cursubsecUl.id, menuId);
		}
   	}
  }
  // Dont set id if in current section
  if (ul.parentNode.parentNode.parentNode.id != 'cursec') {
   ul.parentNode.parentNode.parentNode.id = 'tempsec';
  }
  if (ul.parentNode.id != 'cursubsec') {
   ul.parentNode.id = 'tempsubsec';
  }
 }
 // Level 1
 else if (ul.parentNode.parentNode.id == menuId) {
  // Close previous open section
  if (oldtempsec = document.getElementById('tempsec')) {
   // Is UL part of an open section?
   if (ul.parentNode.parentNode.parentNode.id != 'tempsec') {
    menuCollapse(oldtempsec.getElementsByTagName('ul')[0].id, menuId);
   }
  }
  // Close current section if open
  if (cursec = document.getElementById('cursec')) {
   if (cursecUl = cursec.getElementsByTagName('ul')[0]) {
	   if (cursecUl.style.display != 'none') {
		menuCollapse(cursecUl.id, menuId);
	   }
   }
   // Close current subsection if open
   if (cursubsec = document.getElementById('cursubsec')) {
	   if (cursubsecUl = cursubsec.getElementsByTagName('ul')[0]) {
		   if (cursubsecUl.style.display != 'none') {
			menuCollapse(cursubsecUl.id, menuId);
			}
   		}
	}
  }
  // Close sub section too
  if (oldtempsubsec = document.getElementById('tempsubsec')) {
   oldtempsubsecUl = oldtempsubsec.getElementsByTagName('ul')[0];
   if (oldtempsubsecUl.style.display != 'none') {
    menuCollapse(oldtempsubsecUl.id, menuId);
   }
  } 
  // dont set id if it is in Current Section
  if (ul.parentNode.id != 'cursec') {
   ul.parentNode.id = 'tempsec';
  } 
  // ***
  else {
   if (cursubsec = document.getElementById('cursubsec')) {
    if (cursubsecUl = cursubsec.getElementsByTagName('ul')[0]) {
		// menuExpand(cursubsecUl.id, menuId);
		cursubsecUl.style.display = 'block';
		cursubsecUl.parentNode.replaceChild(menuNode[cursubsecUl.id+'c'], menuNode[cursubsecUl.id+'x']);

	}
   }
  }
 }
 ul.style.display = 'block';
 ul.parentNode.replaceChild(menuNode[ulid+'c'], menuNode[ulid+'x']);
}


// Debug
var _console = null;

function debug(msg)
{
 if ((_console == null) || (_console.closed)) {
  _console = window.open("","console","width=600,height=300,resizable");
  _console.document.open("text/plain");
 }
 _console.focus();
 _console.document.writeln(msg);
}
