function getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function setMouseOvers(obj) {
	for (i=0; i<obj.getElementsByTagName("LI").length; i++) {
		node = obj.getElementsByTagName("LI")[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {
        obj = document.getElementById("nav");
        col = obj.getElementsByTagName("LI");
        for (i=0; i<col.length; i++) {
          col[i].className = col[i].className.replace(" over", "");
        }
        
        col = getElementsByClassName(document, '');
        col = getElementsByClassName(document, 'subnav');
        for(j=0;j<col.length; j++) {
          if (col[j].tagName == "DIV") {
            col[j].className = col[j].className.replace(" over", "");
          }
        }        
        col = getElementsByClassName(document, this.className.replace(" over", ""));
        
        for(j=0;j<col.length; j++) {
          if (col[j].tagName == "DIV") {
            col[j].className += " over";
          }
        }
        this.className += " over";
			}

			node.onmouseout=function() {
                if (this.className.indexOf('none') >= 0) {
                    this.className = this.className.replace(" over", "");
                }
        /*
        col = getElementsByClassName(document, this.className.replace(" over", ""));
        for(j=0;j<col.length; j++) {
          if (col[j].tagName == "DIV") {
            col[j].className = col[j].className.replace(" over", "");
          }
        }      
        */
			}
		}
	}
}
function setSubMouseOvers(obj) {
	for (i=0; i<obj.getElementsByTagName("LI").length; i++) {
		node = obj.getElementsByTagName("LI")[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {
        this.className += " over";
      }
			node.onmouseout=function() {
        this.className = this.className.replace("over", "");
			}
    }
  }
}

function setCurrentSubnav() {
    thisPage = location.href;
    selectedClass = '';
    
    mainNavs = document.getElementById('nav').getElementsByTagName('li');
    
    for(i=0; i<mainNavs.length; i++) {
        link = mainNavs[i].getElementsByTagName('a');
        if (link.length >= 1) {
            link = link[0];
        } else {
            link = empty;
        }
        if (link) {
            if (link.href == thisPage) {
                selectedClass = mainNavs[i].className.replace("over","");
                mainNavs[i].className += " over"; 
            } 
        }
    }

    if (selectedClass != '') {
        col = getElementsByClassName(document, 'subnav');
        for(j=0;j<col.length; j++) {
          if (col[j].tagName == "DIV") {
            col[j].className = col[j].className.replace(" over", "");
          }
        }     
        
        col = getElementsByClassName(document, selectedClass);
        for(j=0;j<col.length; j++) {
          if (col[j].tagName == "DIV") {
            col[j].className += " over";
          }
        }    
    }
}

startList = function() {
  navRoot = document.getElementById("nav");
  setMouseOvers(navRoot);	
  
  subNavRoot = document.getElementById("subnav");
  setSubMouseOvers(subNavRoot);
  
  setCurrentSubnav();
}
window.onload=startList;