var topLevel = 8;
var topLevelItems = new Array(topLevel);
topLevelItems[0] = 'what_is_sound_therapy';
topLevelItems[1] = 'history';
topLevelItems[2] = 'for_the_ear';
topLevelItems[3] = 'for_the_brain';
topLevelItems[4] = 'for_children';
//topLevelItems[5] = 'online_shopping';
topLevelItems[5] = 'research_and_media';
topLevelItems[6] = 'testimonials';
topLevelItems[7] = 'for_practitioners';

var bottomLevel = 46;
var bottomLevelItems = new Array(bottomLevel);
bottomLevelItems[0] = 'what_is_sound_therapy_benefits';
bottomLevelItems[1] = 'what_is_sound_therapy_how_it_works';
bottomLevelItems[2] = 'what_is_sound_therapy_how_you_use_it';
bottomLevelItems[3] = 'history_history';
bottomLevelItems[4] = 'history_dr_tomatis';
bottomLevelItems[5] = 'history_our_method';
bottomLevelItems[6] = 'history_the_founders';
bottomLevelItems[7] = 'history_the_company';
bottomLevelItems[8] = 'history_comparisons';
bottomLevelItems[9] = 'history_tomatis_principles';
bottomLevelItems[10] = 'history_we_are_unique';
bottomLevelItems[11] = 'for_the_ear_hearing';
bottomLevelItems[12] = 'for_the_ear_tinnitus';
bottomLevelItems[13] = 'for_the_ear_menieres_vertigo';
bottomLevelItems[14] = 'for_the_ear_background_noise';
bottomLevelItems[15] = 'for_the_ear_blocked_ear_sinus';
bottomLevelItems[16] = 'for_the_ear_sound_sensitivity';
bottomLevelItems[17] = 'for_the_ear_nutrition';
bottomLevelItems[18] = 'for_the_brain_memory_and_learning';
bottomLevelItems[19] = 'for_the_brain_creativity';
bottomLevelItems[20] = 'for_the_brain_stress_and_energy';
bottomLevelItems[21] = 'for_the_brain_sleep';
bottomLevelItems[22] = 'for_the_brain_nutrition';
bottomLevelItems[23] = 'for_children_listening_and_learning';
bottomLevelItems[24] = 'for_children_learning_difficulties';
bottomLevelItems[25] = 'for_children_special_needs';
bottomLevelItems[26] = 'for_children_for_parents';
bottomLevelItems[27] = 'for_children_nutrition';
//bottomLevelItems[28] = 'online_shopping_new_listeners_adults';
//bottomLevelItems[29] = 'online_shopping_new_listeners_children';
//bottomLevelItems[30] = 'online_shopping_experienced_listeners';
//bottomLevelItems[31] = 'online_shopping_practitioners';
bottomLevelItems[28] = 'research_and_media_articles';
bottomLevelItems[29] = 'research_and_media_media_coverage';
bottomLevelItems[30] = 'research_and_media_research_papers';
bottomLevelItems[31] = 'research_and_media_resources';
bottomLevelItems[32] = 'testimonials_hearing';
bottomLevelItems[33] = 'testimonials_tinnitus';
bottomLevelItems[34] = 'testimonials_menieres_vertigo';
bottomLevelItems[35] = 'testimonials_background_noise';
bottomLevelItems[36] = 'testimonials_blocked_ear';
bottomLevelItems[37] = 'testimonials_sound_sensitivity';
bottomLevelItems[38] = 'testimonials_memory_and_learning';
bottomLevelItems[39] = 'testimonials_creativity';
bottomLevelItems[40] = 'testimonials_stress_and_energy';
bottomLevelItems[41] = 'testimonials_sleep';
bottomLevelItems[42] = 'testimonials_children';
bottomLevelItems[43] = 'for_practitioners_training';
bottomLevelItems[44] = 'for_practitioners_your_practice';
bottomLevelItems[45] = 'for_practitioners_distributing';


//this function expands a branch of the menu tree
function expand(id){
	if (document.all){
		document.all[id].style.display = '';	
		expandImage(id);
		return false;
   } else if (document.getElementById){
        document.getElementById(id).style.display = 'block';	
		expandImage(id);
  		return false;
   }
}


//this function collapses a branch of the menu tree
function collapse(id){
   if (document.all){
     document.all[id].style.display = 'none'; 
	 collapseImage(id);
     return false;
   } else if (document.getElementById){
     document.getElementById(id).style.display = 'none'; 
	 collapseImage(id);
     return false;
   }
}


//this function collapses all of the menu items and changes all images to up state
function collapseAll(){
	if (document.all){
	   for(var count = 0; count < topLevel; count++){
			myElement = topLevelItems[count];
			document.all[myElement].style.display = 'none';
			collapseImage(myElement);
		}
		window.scrollTo(0,0);
		return false;
   } else if (document.getElementById){
		for(var count = 0; count < topLevel; count++){
			myElement = topLevelItems[count];
			document.getElementById(myElement).style.display = 'none';
			collapseImage(myElement);
		}
		window.scrollTo(0,0);
		return false;
   }
}


//this function expands all of the menu items and changes all images to open state
function expandAll(){
	if (document.all){
	   for(var count = 0; count < topLevel; count++){
			myElement = topLevelItems[count];
			document.all[myElement].style.display = '';
			expandImage(myElement);
		}
		return false;
   } else if (document.getElementById){
		for(var count = 0; count < topLevel; count++){
			myElement = topLevelItems[count];
			document.getElementById(myElement).style.display = 'block';
			expandImage(myElement);
		}
		return false;
   }
}


//this function will toggle a branch between open and closed. If other branches are open, they will be closed.
function toggle(id){
   if (document.all){
     if(document.all[id].style.display == 'none'){ 
		//collapseAll();								/* menu allows more than one branch to be open */
		expand(id);
     } else {
       collapse(id);
     }
   } else if (document.getElementById){
     if(document.getElementById(id).style.display == 'none'){ 
	 //collapseAll();
       expand(id);
     } else {
       collapse(id);
     }
   }
}
 
 
//this function swaps the image for the open state image
function expandImage(id){
	document.images[id + '_image'].src='images/menu_out_arrow.gif';
}


//this function swaps the image for the closed state image
function collapseImage(id){
	document.images[id + '_image'].src='images/menu_in_arrow.gif';
}


/*
//this function swaps the image for the highlighted state image
function highlightImage(id){
	document.images[id + '_image'].src='images/menu_highlight_point.gif';
}
*/


//this function will exchange the class of a given element with a new class, unless it is already set to the 'menu_highlight' or 'header_highlight' class
function changeClass(id, newClass){
	if (id.className!="menu_highlight" && id.className!="header_highlight"){
		id.className=newClass;
		return true;
	}
}


//this function will highlight and change the image for a menu item
function highlightMenuItem(id){
	if (document.all){
		document.all[id].className = 'menu_highlight'; 
		//highlightImage(id);								/* removed dots on highlighted menu items */
	} else if (document.getElementById){
     document.getElementById(id).className = 'menu_highlight'; 
	 //highlightImage(id);									/* removed dots on highlighted menu items */
	}
}


/*
//this function will highlight a header item			//no longer used as top level items are same style as low level items
function highlightHeaderItem(id){
	if (document.all){
		document.all[id].className = 'header_highlight'; 
	} else if (document.getElementById){
     document.getElementById(id).className = 'header_highlight'; 
	}
}
*/


//this function opens the menu to the correct area when entering a new page
function initialiseMenu(){
	var found = false;
	for(var count=0; count < bottomLevel; count++){
		myElement = bottomLevelItems[count];
		if(checkFileName(myElement)){
			highlightMenuItem(myElement);
			found = true;
		}
	}
	for(var count = 0; count < topLevel; count++){
		myElement = topLevelItems[count];
		if (checkFileName(myElement)){
			expand(myElement);
			if(!found){
				highlightMenuItem(myElement +'_header')
			}
		}
	}
}


//this function checks for the presence of a string withing the filename of the current page
function checkFileName(myFileName){
	var thisPage = new String(window.location);
	if (thisPage.indexOf(myFileName) != -1){
		return true;
	} else {
		return false;
	}
}


function preloadImages() {
  if (document.images) {
    if (typeof(document.menuImages) == 'undefined'){
      document.menuImages = new Object();
    }
    document.menuImages.loadedImages = new Array();
    var argLength = preloadImages.arguments.length;
    for(arg=0;arg<argLength;arg++) {
      // For each arg, create a new image.
      document.menuImages.loadedImages[arg] = new Image();
      // Then set the source of that image to the current argument.
      document.menuImages.loadedImages[arg].src = preloadImages.arguments[arg];
    }
  }
}


function statusBarMessage(){
	window.status='Please click on the arrows to expand or collapse a menu item...';
}
