/*
collapsible menus
written by Kae Verens - kae@verens.com
you are free to use this. please retain these comments, though.
if there are any questions/suggestions for the code, please email me.

usage:
1. create your menu as a hierarchy of unordered lists. for example
 <ul>
  <li><a href="/">home</a>
   <ul>
    <li><a href="/aboutme">about me</a></li>
    <li><a href="/contact">contact me</a></li>
   </ul>
  </li>
  <li><a href="/gallery">photo gallery</a></li>
 </ul>
2. place this script in your directory
3. link this script into every page that has the menu:
 <script type="text/javascript" src="cm.js"></script>

*/



var parentLinkIsOpener=0; // whether to use parent link/text as opener, overriding its own link

if (window.attachEvent) {
	window.attachEvent('onload', readCookie);
	window.attachEvent('onload',cm);
	window.attachEvent('onunload', writeCookie);
} else if (window.addEventListener) {
	window.addEventListener('load',cm,false);
	window.addEventListener('load', readCookie, false);
	window.addEventListener('unload', writeCookie, false);
}

function cm(){
 cmId=0;
 if(!document.getElementsByTagName)return; // reject non-compliant browsers
 a=document.getElementById('nav').getElementsByTagName('ul');
 for(i=0;a[i];i++){
  if(a[i].getElementsByTagName('ul')){
   // a[i] is an object which has a collapsible list in it
   b=a[i].childNodes;
   for(j=0;b[j];j++){
    if(b[j].nodeName=='LI'){
     d=b[j].getElementsByTagName('ul');
     if(d.length){
      c=document.createElement('a');
      c.setAttribute('href','javascript:cmSwitch("cm'+(cmId)+'")');
      c.setAttribute('class','hasitem'); // sets different class for object with collapsible list
	  c.setAttribute('className', 'hasitem');     
      c.setAttribute('id','cm'+(cmId)+'a');
      
              if(parentLinkIsOpener){
               // we've chosen to use the parent link as the opener
               var e=b[j].innerHTML;
               e=e.replace(/(\n|\r)/g,'');
               e=e.replace(/(<ul|<UL).*/,'');
               e=e.replace(/<[^>]*>/g,'');
               c.innerHTML=e;
               b[j].replaceChild(c,b[j].childNodes[0]);
              }else{
               // create a new [+] link as the opener
               c.style.display='inline';
               c.innerHTML='[+]';
               b[j].insertBefore(c,b[j].firstChild);
              }
      
      d[0].setAttribute('id','cm'+(cmId++));
      d[0].style.display='none';
     } 
    }
   }
  }
 }
}

function cmSwitch(id){
 a=document.getElementById(id);
 b=document.getElementById(id+'a');
 
 if (a.style.display=='block') {
    a.style.display = 'none';
    b.setAttribute("class","hasitem");
	b.setAttribute("className", "hasitem");
    b.setAttribute('onfocus','this.blur();'); // removes dotted focus line
 } else {
    a.style.display = 'block';
    b.setAttribute('class','listexpand');
	b.setAttribute("className", "listexpand");
    b.setAttribute('onfocus','this.blur();'); // removes dotted focus line
 }
 
 if(parentLinkIsOpener) { return; }

 b.innerHTML=(b.innerHTML=='[+]')?'[-]':'[+]';
}

function writeCookie() {
	var expireDate = new Date;
	expireDate.setYear(expireDate.getFullYear() + 1);

	var cookieVal = "junk";

	// Find the elements that are expanded

	for (i = 0; a = document.getElementById("cm" + i + "a"); i++) {
		if (a.getAttribute("class") == "listexpand")
			cookieVal += (":cm" + i);
		else if (a.getAttribute("className") == "listexpand")
			cookieVal += (":cm" + i);
	}

	// Write the cookie

	document.cookie = "Expanded=" + escape(cookieVal) + ";expires=" + expireDate.toGMTString();
}

function readCookie() {
	// Retrive cookie info
	var cookieVal = "" + document.cookie;

	var index1 = cookieVal.indexOf("Expanded");
	if (index1 == -1) return; // no cookie set

	var index2 = cookieVal.indexOf(';',index1);
	if (index2 == -1) index2 = cookieVal.length;

	cookieVal = unescape(cookieVal.substring(index1+8+1, index2));

	// Process the cookie info (first (0) element is junk)

	var idArray = cookieVal.split(":");

	for (i = 1; i < idArray.length; i++) {
		cmSwitch(idArray[i]);
	}	
}

