/* ================================================================ 
Copyright (c) 2009 Jürgen Müller-Lütken. All rights reserved.
This script and the associated (x)html may be modified in any 
way to fit your requirements.
=================================================================== */
/* You need the Javascript-Library Mootools for this script to work (http://mootools.net)
 and also the following code snippet from Chris McMichael for a hasEvent() expansion (http://chrismcmichael.com/tags/mootools/:
Native.implement([Element, Window, Document], { hasEvent: function(type) { var events = this.retrieve('events',{});
if (events && events[type]) return true; else return false; }})
*/
// configure the next 2 lines
var fixedMenus = false; // all submenus will hide if the mouse leaves a submenu; set to "true" for leaving them fixed 
var NaviContainerId = 'NaviContainer'; // the id of the div containing all the menus

// the HTML of the navigation needs to be in the same way as you see it in the example-file
// for every level you need a div; the submenus of every level are normal unordered lists
// ATTENTION: the id of every ul has to be the following way; a name you can choose, a underline, the number of the link showing the submenu
// so you go through all levels of submenus you wanted to be shown
// EXAMPLE: Submenu_2_1_6_3; this means: this is a submenu of the fourth level; it will be open after you entered the 2. link of the main-menu, the 1. link of the first submenu, the 6. link of the second submenu and the 3. link of the third submenu

// no changes needed above
var openMenus = new Array();
var brackets = new Array(3);
var navigating = false;
var linkTexts;

function initMenu()
{
	// get all levels of navigation 
	var NaviDivs = $(NaviContainerId).getElements("DIV");
	var ndl = NaviDivs.length;
	linkTexts = new Array(ndl);
	var level = 0;
	// put mouseover events on all menu buttons to show submenus
	while(level < ndl)
	{
		// get all submenus of every level
		var SubUls = NaviDivs[level].getElements('UL');
		for(var j=0; j<SubUls.length; j++)
		{
			// put a moseout event on the submenus to hide all submenus when leaving them with the mouse
			if(!fixedMenus)
			{
				SubUls[j].addEvent('mouseout', hideAll);
				SubUls[j].levelCode = 'x';
			}
			if(level > 0)
				SubUls[j].setStyle('display', 'none');
			var SubIdSplit = SubUls[j].getProperty('id').split('_');
			// get all links of every submenu
			var UlA = SubUls[j].getElements('A');
			for(var k=0; k<UlA.length; k++)
			{
				// put the events on all links of the submenu showing further submenus
				if(level < (ndl - 1))
				{
					// get all the submenus of the next level and compare their ids with the id of the submenu of the calling link
					var SubUlsNext = NaviDivs[level+1].getElements('UL');
					for(var x=0; x<SubUlsNext.length; x++)
					{
						var SubUlsNextIdSplit = SubUlsNext[x].getProperty('id').split('_');
						var equal = 0;
						for(var z=0; z<level; z++)
						{
							if(Number(SubUlsNextIdSplit[z+1]) == Number(SubIdSplit[z+1]))
							{
								equal++;
							}
						}
						if(equal == level && Number(SubUlsNextIdSplit[level+1]) == (k+1))
						{
							UlA[k].addEvents({'mouseover': showMenu,
											'mouseout': hideBracket});
							UlA[k].levelCode = SubUlsNext[x].getProperty('id');
							UlA[k].level = level;
						}
					}
					// all links showing no submenu just have to hide the open submenus
					if(!UlA[k].hasEvent('mouseover'))
					{
						UlA[k].addEvent('mouseover', hideMenu);
						// just to get the right title whwn clicking the Link-Button; very bad hack
						if(level == 0 && k == 5)
							UlA[k].levelCode = 'SubMenu_6';
						else
							UlA[k].levelCode = SubUls[j].getProperty('id');
						UlA[k].level = level;
					}
					UlA[k].addEvent('mouseout', hideBracket);
				}
				else
					// the links of the last submenu level just have to avoid the hiding of their submenu when they get entered
				{
					//if(!fixedMenus)
						UlA[k].addEvents({'mouseover': setNavigating,
										'mouseout': hideBracket});
						UlA[k].levelCode = SubUls[j].getProperty('id');
						UlA[k].level = level;
				}
			}
		}
		level++;
	}
}
// show the submenu
function showMenu()
{
	navigating = true;
	checkMenusToHide(this.levelCode, this);
	openMenus.push(this.levelCode);
	$(this.levelCode).setStyle('display', 'block');
	getLinkText(this.get('text'), this.level);
	showBracket(this);
}
// hide the submenus
function hideMenu()
{
	navigating = true;
	checkMenusToHide(this.levelCode, this);
	getLinkText(this.get('text'), this.level);
	showBracket(this);
}
// just prevent hiding of the submenus when entering the last submenu level
function setNavigating()
{
	navigating = true;
	getLinkText(this.get('text'), this.level);
	showBracket(this);
}
// hide all submenus not needed anymore
function checkMenusToHide(levelid, button)
{
	if(openMenus.length)
	{
		for(var i=openMenus.length-1;i>=0; i--)
		{
			
			if(openMenus[i] && levelid.indexOf(openMenus[i]) == -1)
			{
				$(openMenus[i]).setStyle('display', 'none');
				openMenus[i] = '';
			}
		}
		openMenus.erase('');
	}
}

// hide the submenus when leaving a submenu; to prevent hiding while entering a submenu, do this after a short delay
function hideAll()
{
	navigating = false;
	setTimeout("hideAllSubmenus('"+this.levelCode+"')", 50);
}
// hide all open submenus
function hideAllSubmenus(levelCode)
{
	if(!navigating)
		checkMenusToHide(levelCode);
}
function showBracket(el)
{
	el.setStyle('background-position', '0px 0px');
	brackets[el.level] = el;
}
function hideBracket()
{
	if(!this.hasClass('clicked'))
		this.setStyle('background-position', '-15px 0px');
}
function removeBrackets()
{
	$$('#NaviContainer a').each(function(button)
	{
		if(button.hasClass('clicked'))
		{
			button.removeClass('clicked');
			button.setStyle('background-position', '-15px 0px');
		}
	})
}
function setBrackets(level)
{
	for(var i=0; i<brackets.length; i++)
	{
		if(i <= level)
		{
			brackets[i].addClass('clicked');
			brackets[i].setStyle('background-position', '0px 0px');
		}
	}
}
// collect the text of a hovered link
function getLinkText(txt, level)
{
	linkTexts[level] = txt;
	for(var i=(level+1); i<linkTexts.length; i++)
		linkTexts[i] = '';
}
// all links of the navigation get some more events calling functions;  every link gets an id which will be extracted from the url 
function initButtons()
{
	$$('#NaviContainer a').each(function(button)
	{
		button.no = getPageId(button.getProperty('href'));
		button.addEvents({'mousedown': setNewData,
						'mouseup': getText,
						'click': function(event) { event.stop();}});
	})
}

