var menus = new Array();
var closes = new Array();

function sortMenusArray(){
	// first get all the un-ordered lists
	var uls = document.getElementsByTagName("ul");

	// now go through all the un-ordered lists
	for(var n=0; n<uls.length; n++){

		// now, if the un-ordered list has the expandable class...
		if( uls[n].className == "expandable" ){
			// create a new menu array
			menus[menus.length] = new Array();
			
			// create a var for recording the current spot in this menu's array
			var cur_spot = 0;

			// now loop through and store all of the list items (<li>)
			for(var o=0; o<uls[n].getElementsByTagName("li").length; o++){
				
				// make sure that the list item is directly under the menu <ul> (to support nested menus)
				if(uls[n].getElementsByTagName("li")[o].parentNode == uls[n]){
					
					menus[menus.length-1][cur_spot] = new Array();
			
	
					for(var p=0; p<uls[n].getElementsByTagName("li")[o].childNodes.length; p++){
	
						if( uls[n].getElementsByTagName("li")[o].childNodes[p].nodeName.toLowerCase() != "#text"){
							menus[menus.length-1][cur_spot][menus[menus.length-1][cur_spot].length] = uls[n].getElementsByTagName("li")[o].childNodes[p];
						}
					}
					
					cur_spot++;
				} else {
				}
			}
		}
	}

	showHide("all", "all");

}

function setupLinks(){
	var links = document.getElementsByTagName("div");
	
	for(var i=0; i < links.length; i++){
		if(links[i].id.substr(0,1) == "h"){
			links[i].style.display = "none";
		}
	}
	
	links = document.getElementById("main");
	subdivs = document.getElementsByTagName("div");
	var head = '';
	var html = '';
	var found = 0;
	
	for(var i = 0; i < subdivs.length; i++){
		if(subdivs[i].parentNode == links){
			html = subdivs[i].innerHTML.toString();
			found = html.indexOf("<BR>");
			if( found == -1 ){
				found = html.indexOf("<br>");
			}
			if( found > -1 && found == html.indexOf("<")){
				head = html.substring(0,found);
				html = html.substr(found + 4);
			} else {
				head = "&nbsp;";
			}
			head = "<i><b>" + head + "</b></i>";
			subdivs[i].innerHTML = "<div class='title'><img src='images/content_g_l.gif' width='10' height='20' hspace='0' align='left'>" + head + "</div><a style=\"float:left\" href='javascript:closeDiv(\"" + subdivs[i].id + "\")'><img src='images/content_r_close.gif' width='24' height='20' vspace='0' border='0' align='top'></a> " + html + "<img src='images/content_bottom_592.gif' width='582' height='21' align='top' class='negmargin'> ";
			//subdivs[i].innerHTML = subdivs[i].id;
		}
	}
	
	links = document.getElementsByTagName("a");
	
	for(var i = 0; i < links.length; i++){
		if(links[i].href.split("#")[1]){
			if(document.getElementById(links[i].href.split("#")[1])){
				links[i].onclick = function() { 
					return show(this.href.split("#")[1]); 
				}
			}
		}
		
		if(links[i].href.split("closeDiv")[1]){
			closes[closes.length] = links[i];
			links[i].id = "cl" + (closes.length - 1);
			links[i].onmouseover = function(){
				closes[parseInt(this.id.substr(2))].childNodes[0].src = "images/content_r_close_o.gif";
			}
			links[i].onmouseout = function(){
				closes[parseInt(this.id.substr(2))].childNodes[0].src = "images/content_r_close.gif";
			}
		}
	}
 if(self.document.location.hash){
  show(self.document.location.hash.substr(1));
 }
}

function closeDiv(id){
	var divEl = document.getElementById(id);
	divEl.style.display = "none";
}

function show(id){
	var divEl = document.getElementById(id);
	var mainDiv = document.getElementById("main");
	
	if(divEl.tagName == "DIV"){
		if(divEl.style.display == "none" || divEl != mainDiv.childNodes[0]){
			// move it to the top
			var newdiv = document.createElement('div');
			newdiv.innerHTML = divEl.innerHTML;
			newdiv.className = divEl.className;
			mainDiv.removeChild(divEl);
			newdiv.id = id;
			mainDiv.insertBefore(newdiv,mainDiv.childNodes[0]);
			var links = newdiv.childNodes;
			for(var i = 0; i < links.length; i++){
				if(links[i].tagName == "A" && links[i].href.split("closeDiv")[1]){
					closes[closes.length] = links[i];
					links[i].id = "cl" + (closes.length - 1);
					links[i].onmouseover = function(){
						closes[parseInt(this.id.substr(2))].childNodes[0].src = "images/content_r_close_o.gif";
					}
					links[i].onmouseout = function(){
						closes[parseInt(this.id.substr(2))].childNodes[0].src = "images/content_r_close.gif";
					}
				}
			}
	links = newdiv.getElementsByTagName('A');
   for(var i = 0; i < links.length; i++){
    if(links[i].href.split('#')[1]){
     links[i].onclick = function() { 
      return show(this.href.split("#")[1]); 
     }
    }
   }
			
			return false;
		} else {
			// move it to the top
			var newdiv = document.createElement('div');
			newdiv.innerHTML = divEl.innerHTML;
			newdiv.className = divEl.className;
			mainDiv.removeChild(divEl);
			newdiv.id = id;
			newdiv.style.display = "none";
			mainDiv.appendChild(newdiv);
				
			return false;
		}
	}
}

function showHide(menunum, linum){

  if( menunum == "all" || linum == "all" ){	
    for(var n=menus.length -1; n>=0; n--){
      for(var o=0; o<menus[n].length; o++){

        menus[n][o][1].style.display		= ( menus[n][o][1].style.display == "none" ) ? "block" : "none" ;		

        menus[n][o][0].id = "menu"+n+"header"+o;
		
        menus[n][o][0].onclick = function(){

          showHide( parseInt( this.id.split("header")[0].replace(/\D/g,"") ) , parseInt( this.id.split("header")[1] ) );

        }
		
		if(o > 0){		
        	menus[n][o][0].className		= ( menus[n][o][1].style.display == "none" ) ? "closed" : "open" ;	
			menus[n][o][0].onmouseover = function() {
				if( this.className != "open")
				  this.className = "over";
			}
			menus[n][o][0].onmouseout = function() {
				if( this.className != "open")
				  this.className = "closed";
			}
		} else {
	 		menus[n][o][0].className		= ( menus[n][o][1].style.display == "none" ) ? "closed_t" : "open_t" ;		
			menus[n][o][0].onmouseover = function() {
				if( this.className != "open_t")
				  this.className = "over_t";
			}
			  
			menus[n][o][0].onmouseout = function() {
				if( this.className != "open_t")
				  this.className = "closed_t";
			}
		}

      }
    }

  } else {
	if( linum == 0 ){
      menus[menunum][linum][1].style.display	= ( menus[menunum][linum][1].style.display == "none" ) ? "block" : "none" ;
      menus[menunum][linum][0].className		= ( menus[menunum][linum][1].style.display == "none" ) ? "over_t" : "open_t" ;		
	} else {
      menus[menunum][linum][1].style.display	= ( menus[menunum][linum][1].style.display == "none" ) ? "block" : "none" ;
      menus[menunum][linum][0].className		= ( menus[menunum][linum][1].style.display == "none" ) ? "over" : "open" ;		
	}
  }

}

jQuery(sortMenusArray);
jQuery(setupLinks);
