/// JavaScript Document
/// by frizbee.be

/// variables

	var timeout = 10;
	var timer = 0;
	var submenu = 0;
	var menuitem = 0;
	
	$(document).ready(function(){
		
		$('li span[rel], li a[rel]').hover(
			function()
			{
				/// hides all submenus
					hideallsubmenus();
					
				/// remove all hover classes
					removelinkhovers();
									
				/// save this menuitem
					menuitem = $(this);
					
				/// give this menuitem the hover class
					menuitem.addClass('hover');
				
				/// get the position of the button where we hover
					var position = $(this).position();				
				
				/// get the id of the submenu for this button
					var idsubmenu = $(this).attr('rel');
				
				/// select the submenu for this button
					submenu = $('#' + idsubmenu );
					
				/// align the submenu with the button
					submenu.css('left', position.left + 'px');
					
				/// cancel the timeout to hide the submenu
					cancelhidesubmenu();
					
				/// show the submenu
					submenu.show();
					
			},
			function()
			{
				/// hide the submenu
					starthidesubmenu();
			}
		);
		
		$('.submenu').hover(cancelhidesubmenu, starthidesubmenu)
		
		function starthidesubmenu()
		{
			/// keep the css hover for the menuitem
				menuitem.addClass('hover');
				
			/// start timeout to hide the submenu
				timer = window.setTimeout(hidesubmenu, timeout);
		}
		
		function hidesubmenu()
		{
			/// hides alle submenus
				hideallsubmenus();
				
			/// remove all hover classes
				removelinkhovers();
		}
		
		function cancelhidesubmenu()
		{
			if(timer)
			{
				window.clearTimeout(timer);
				timer = null;
			}
		}
		
		function removelinkhovers()
		{
			/// hides all hover classes on the navigation links
				$('#navigation ul li span, #navigation ul li a').removeClass('hover');
		}
		
		function hideallsubmenus()
		{
			/// hides alle submenus
				$('.submenu').hide();
		}
				
	});
