addLoadEvent(collapseMenu);
addLoadEvent(prepareMenu);


function collapseMenu(node) {
	if (!document.getElementById) return false;
	if (!document.getElementById("menu")) return false;
	if (!node) node = document.getElementById("menu");

	if (node.childNodes.length > 0) {
		for (var i=0; i<node.childNodes.length; i++) 
		{
			var child = node.childNodes[i];
			if (child.nodeName == "UL") 
			{
				child.style.display = "none";
			}
			collapseMenu(child);
		}		
	}

}

function prepareMenu() 
{
	if (!document.getElementById || !document.getElementsByTagName) return false;
	if (!document.getElementById("menu")) return false;

	var links = document.getElementById("menu").getElementsByTagName("a");
	var loc = window.location.pathname;
	var done = 0;
	var loc_array = loc.split('/');
	
	for (var i=0; i<links.length; i++) 
	{
		links[i].onclick = function() 
		{
			
			toggleMenu(this.parentNode.getElementsByTagName("UL")[0], this.href);
			//List of Main Menus
			
			//Extract the Main Menu that was selected
			var word=this.href.split("/");
			var cur = word[word.length-1];
			
			//set all images back to grey, and selected one to blue
			
			
			//and active one to blue
			
			document[cur].src = "img/menu/"+cur+".gif";
			
			var menu = new Array("services","about_us","gallery","news","contact_us");
			for (var i = 0; i < menu.length; i++)
			{
				if (menu[i].toString()!=cur){
					//alert(menu[i].toString()+" != "+cur);
					var value = menu[i].toString();
					//alert(value);
					document[value].src = "img/menu/"+value+".gif";
				}
			}
			return false;
		}
		
		var link_loc = links[i].toString();
  		link_loc = link_loc.replace("http://www.your_url.com/",""); //Change to be the root URL (http://www.your_URL.com)
  		var link_loc_array = link_loc.split('/');
		
		if(loc != "/")
		{
			//This may need to be edited depending on the structure of your site.
			//Comparison to find the link that was clicked on.
			//May help to have alerts displayed for loc_array and link_loc_array to get the correct index of the array to compare.
  			if((loc_array[loc_array.length - 2] == link_loc_array[link_loc_array.length - 1]))
  			{
			
  				if(links[i].href != "undefined" && done != 1)
  				{
  					node = links[i].parentNode.getElementsByTagName("UL")[0];
					
  					if(node)
  					{
  						if (node.childNodes.length > 1)
 						{
  							done=1;
							
  							Effect.BlindDown(node, {duration: 0.2});
  						}
	  				}
  				}
			}
		}
	}
}

function toggleMenu(node, link) 
{
	if (!document.getElementById) return false;
	if (!link) return false;
	if (!node) 
	{
		var link_temp = link.replace("http://dell/hgh/","");
		var loc = window.location.pathname;
		loc = loc.replace(".php","/");
		loc = loc.replace("/404/","/");

		if(link_temp != loc)
		{
			location.href = link;
		}
		return false;
	}

	// Collapse all nodes, and only show clicked node (when clicking top level of menu)
	if (node.parentNode.parentNode.id == "menu") 
	{
		hideTopLevels();
	}
	if (node.style.display == "") 
	{
		
	}
	else 
	{
 		if (node.childNodes.length > 1)
 		{
 			Effect.BlindDown(node, {duration: 0.2});
 		}
		else
		{
			var link_temp = link.replace("http://www.hghadvertising.com.au/",""); //Change to be the root URL (http://www.your_URL.com)
			var loc = window.location.pathname;
			loc = loc.replace(".php","/");

			if(link_temp != loc)
			{
				location.href = link;
			}
			return false;
		}
	}
}

function hideTopLevels() {
	if (!document.getElementById) return false;
	if (!(node = document.getElementById("menu"))) return false;	
	
	if (node.childNodes.length > 0) 
	{
		for (var i=0; i<node.childNodes.length; i++) 
		{
			var child = node.childNodes[i];
			for(var j=0; j<child.childNodes.length; j++) 
			{
				var grandchild = child.childNodes[j];
				if (grandchild.nodeName == "UL") 
				{
					if (grandchild.style.display == '') 
					{
						Effect.BlindUp(grandchild, {duration: 0.2});
					}
				}
			}
		}		
	}
}

function addLoadEvent(func) 
{
	var oldonload = window.onload;
 	if (typeof window.onload != 'function') 
 	{
		window.onload = func;
 	}
 	else 
 	{
		window.onload = function() 
		{
   			oldonload();
   			func();
  		}
 	}
}