Event.observe(window, 'load', function() {
	// First level (vertical)
	$$('#menu > ul > li > a').each(function(element) {
		if(element.next() && element.next().hasClassName('menu')) {
			element.observe('click', function() {
				showMenu(element.next());
			});
			element.observe('mouseover', function() {
				showMenu(element.next());
				element.setStyle({
					color: '#e2001a',
					cursor: 'pointer'
				});
				
			});
			element.observe('mouseout', function() {
				hideMenuCheck();
				element.setStyle({
					color: '#002c57',
					cursor: 'default'
				});
			});
			if(element.hasAttribute('href')) {
				element.removeAttribute('href');
			}
		}
		else if(element.hasAttribute('href')) {
			var href = element.readAttribute('href');
			if(!element.hasAttribute('target')) {
				element.observe('click', function() {
					location.href = href;
				});
				element.removeAttribute('href');
			}
			
			element.observe('mouseover', function() {
				element.setStyle({
					color: '#e2001a',
					cursor: 'pointer'
				});
			});
			element.observe('mouseout', function() {
				element.setStyle({
					color: '#002c57',
					cursor: 'default'
				});
			});
		}
	});
	// Second level (horizontal)
	$$('#menu div.menu ul > li > a').each(function(element) {
		if(element.next() && element.next().hasClassName('submenu')) {
			element.up().addClassName('submenu'); // just to format this list item
			element.observe('mouseover', function() {
				showSubmenu(element.next());
			});
			element.observe('click', function() {
				showSubmenu(element.next());
			});
		}
	});
	
	// add 'last' class to all last entries
	$$('div.menu, div.submenu').each(function(menu) {
		menu.down('ul').childElements().last().addClassName('last');
	});
});

function showMenu(menu) {
	menu = $(menu);
	if(!menu.visible()) {
		Effect.Queues.get('menu').invoke('cancel');
		new Effect.Appear(menu, {
			delay: 0.2,
			duration: Prototype.Browser.IE ? 0.0 : 0.1,
			queue: { scope: 'menu', position: 'end' },
			beforeStart: function() {
				hideMenu();
				if(Prototype.Browser.IE) {
					hidediv('calendar');
				}
				if(Prototype.Browser.IE6) $$('.dropdown').invoke('hide');
			}
		});
	}
}

function hideMenu() {
	$$('div.submenu', 'div.menu').select(function(e){return e.visible()}).each(function(element) {
		new Effect.Fade(element, { duration: Prototype.Browser.IE ? 0.0 : 0.2 });
	});
	$$('div.menu > ul > li').each(function(element) {
		normalStyle(element);
	});
	if(Prototype.Browser.IE6) $$('.dropdown').invoke('show');
}

function hideMenuCheck() {
	var queue = Effect.Queues.get('menu');
	if(queue.size() > 0) {
		queue.invoke('cancel');
		hideMenu();
	}
}

function showSubmenu(submenu) {
	submenu = $(submenu);
	if(!submenu.visible()) {
		Effect.Queues.get('submenu').invoke('cancel');
		new Effect.Appear(submenu, {
			delay: 0.2,
			duration: Prototype.Browser.IE ? 0.0 : 0.1,
			queue: { scope: 'submenu', position: 'end' },
			beforeStart: function() {
				$$('div.submenu').select(function(e){return e.visible()}).each(function(element) {
					new Effect.Fade(element, { duration: Prototype.Browser.IE ? 0.0 : 0.2 });
					normalStyle(element.up('li'));
				});
			},
			afterSetup: function() {
				highlight(submenu.up('li'));				
			}
		});
	}
}

function highlight(entry) {
	entry.setStyle({
		background: 'url(/images/navigation/pfeil.gif) no-repeat right center'
	});
}

function normalStyle(entry) {
	entry.setStyle({
		background: 'none'
	});
}