//##################################################################################
//
// Prototype | Accordion menu - my first proper prototype project
// Developed by Craig Jardine | Infinite Eye Ltd 2007 | www.infinite-eye.com
// Copy it and use it anywhere you like, really, just dont ask me for help :)
//
//##################################################################################
function initAccMenu() {
	//check we have a menu to do things with
	if($('acc_menu')){
		//HIDE all the sub menus to start with		
		hideAllSubMenus(false);
		//now see if there is an a tag with the class 'active' set
		//and if there is, open the path to it, ie open all the ancestral ul tags
		var active_node = $('acc_menu').getElementsByClassName('active');
		if(active_node.size()){
			active_node.each(function(n) {
				n.ancestors().each(function(s) {
					if(s.match('ul')){
						s.show();
					}	
				});
			});
		}
	}
}

function hideAllSubMenus(animate){
		//HIDE all the sub menus to start with
		var top = $('acc_menu').immediateDescendants(); 
		var num_top = top.size();
		//set the link for each one that contains a nested ul (a sub menu)
		for(x=0; x<num_top; x++){
			var subs = top[x].getElementsBySelector('ul');
			if(subs.size()){
				if(animate){
					if(subs[0].visible()){
						new Effect.BlindUp(subs[0]);
					}
				}
				else{
					subs[0].hide();
				}
				var tag = top[x].immediateDescendants(); 
				//tag[0].setAttribute('onclick', 'showMenu('+x+')');			
				tag[0].setAttribute('href', 'javascript:showMenu('+x+')');			
			}
		}
}

function showMenu(menu){
	var top = $('acc_menu').immediateDescendants(); 
	var num_top = top.size();
	//firstly check that the request menu is not already open
	if(!top[menu].getElementsBySelector('ul')[0].visible()){	
		//HIDE all the other menus first and set all links to be showMenu links
		hideAllSubMenus(true);
		//SHOW the menu we are interested in
		new Effect.BlindDown(top[menu].getElementsBySelector('ul')[0]);
		// 		var opened = getElementBySelector('li')[0];
		// 	
		// 		opened.addClassName('openSubnav');
	}
	//Blur the links so we dont get the ugly dotted box
	top[menu].getElementsBySelector('ul').each(function(n){
		n.getElementsBySelector('a').each(function(x){
			x.blur();
		});
	});
}

