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);
			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://www.spiderdesign.co.uk","");
		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.your_url.com/",""); //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();
  		}
 	}
}



addLoadEvent2(collapseMenu2);
addLoadEvent2(prepareMenu2);


function collapseMenu2(node) {
	if (!document.getElementById) return false;
	if (!document.getElementById("menulogin")) return false;
	if (!node) node = document.getElementById("menulogin");

	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";
			}
			collapseMenu2(child);
		}		
	}

}

function prepareMenu2() 
{
	if (!document.getElementById || !document.getElementsByTagName) return false;
	if (!document.getElementById("menulogin")) return false;

	var links = document.getElementById("menulogin").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() 
		{
			toggleMenu2(this.parentNode.getElementsByTagName("UL")[0], this.href);
			return false;
		}
		
		var link_loc = links[i].toString();
  		link_loc = link_loc.replace("http://www.escuelanacional.cl/",""); //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 toggleMenu2(node, link) 
{
	if (!document.getElementById) return false;
	if (!link) return false;
	if (!node) 
	{
		var link_temp = link.replace("http://www.spiderdesign.co.uk","");
		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 == "menulogin") 
	{
		hideTopLevels2();
	}
	if (node.style.display == "") 
	{
		
	}
	else 
	{
 		if (node.childNodes.length > 1)
 		{
 			Effect.BlindDown(node, {duration: 0.2});
 		}
		else
		{
			var link_temp = link.replace("http://www.escuelaacional.cl/",""); //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 hideTopLevels2() {
	if (!document.getElementById) return false;
	if (!(node = document.getElementById("menulogin"))) 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 addLoadEvent2(func) 
{
	var oldonload = window.onload;
 	if (typeof window.onload != 'function') 
 	{
		window.onload = func;
 	}
 	else 
 	{
		window.onload = function() 
		{
   			oldonload();
   			func();
  		}
 	}
}