// JavaScript Document
function setupSubMenus() {
	// add click behaivours to show submenus 
	$$('#mainmenu > ul > li > a').each( function(m) {	
										  			if (m.next('ul')) {
														// link followed by un ordered list
														Event.observe(m, 'click', toggleSubMenu);
													}
										 		 }
						 		);

}

function toggleSubMenu(e) {
	// toggle visiblity of submenu on click
	var elm = Event.element(e); //get element
	// find matching URL in slider
	if (elm) {
		var  subMenu = elm.next('ul');
		if (subMenu) {
			if (!subMenu.visible()) {
				// not currently visible so hide others and show submenu
				hideSubMenus();
				new Effect.BlindDown(subMenu, {duration: 0.3});
			} else {
				// hide it again
				hideSubMenus();
			}
		}
		Event.stop(e); // stop event to disable link being followed		
	}
}

function hideSubMenus() {
	// hide all visible sub-menus
	$$('#mainmenu > ul > li > a').each( function(m) {	var subMenu = m.next('ul');
										  			if (subMenu && subMenu.visible()) {
														// link followed by un ordered list
														new Effect.BlindUp(subMenu, {duration: 0.3});
													}
										 		 }
						 		);
}

document.observe("dom:loaded", function () { setupSubMenus();});
