function bindTopMenuElements(){
	
	$('#navigation').find('> ul > li').each(function(){
	
		//Fetch submenuId from rel attribute on anchor
		menuToShow = $(this).find('a').attr('rel');
		
		
		/*
		//OffsetLeft vergelijken van hoofdelement met eerste li van submenu. 
		//Als left van submenu groter is dan het verschil zetten > right.
		//Als left kleiner is, niets doen.
		//Enkel maar indien er submenu's zijn.
		parentLeft = $(this).offset().left;
		//console.log("Parent: " + parentLeft);
		
		if($('#menu_child_' + menuToShow).length > 0){
		
			childLeft = $('#menu_child_' + menuToShow + ' li:last-child').offset().left;

			if(childLeft > 0){
				if(childLeft > parentLeft){
					offsetWidth = childLeft - parentLeft;
					//console.log("Move to the left!!:" + offsetWidth);	
									
					$('#menu_child_' + menuToShow).css({'right' : offsetWidth + 'px'});
						
				}
			}
		}
		*/

		if($('#menu_child_' + menuToShow).length > 0){

			$(this).find('a').bind('mouseenter',function(e){
	
				//Fetch submenuId from rel attribute on anchor
				menuToShow = $(this).attr('rel');
	
				//Hide other submenus if there is a submenu for this one
				hideAllMenus();
				
				//Show submenu
				showSubMenu($('#menu_child_' + menuToShow));
				
				
				//Timeout om menu te hiden weer afzetten
				window.clearTimeout(window.menuTimeout);	
				
				//Alles op inactive plaatsen
				$('#navigation').find('> ul > li').each(function(){
					$(this).find('a').removeClass('active');		
				});
				
				//Juiste op active plaatsen
				$(this).toggleClass('active');
			});
			
			$(this).find('a').bind('mouseout',function(e){
			
				//Clear timer
				window.clearTimeout(window.menuTimeout);
				
				//Start timer voor hiden van menu
				window.menuTimeout = window.setTimeout(hideInactiveMenus,2000);
				
			});

		
		}else{
			$(this).find('a').bind('mouseenter',function(e){		
				hideInactiveMenus();
			});
		}
	});
	
}

function showSubMenu(menu){

	menu.show();
	menu.find('li').each(function(){
	
		$(this).find('a').bind('mouseenter',function(e){
			//Timeout om menu te hiden weer afzetten
			window.clearTimeout(window.menuTimeout);	
		});	
	
		$(this).find('a').bind('mouseout',function(e){
		
			//Clear timer
			window.clearTimeout(window.menuTimeout);
			
			//Start timer voor hiden van menu
			window.menuTimeout = window.setTimeout(hideInactiveMenus,2000);
			
		});
	});
}

function hideAllMenus(){
	$('#navigation').find('.sub').find('ul').each(function(){
		$(this).hide();
	});	
}




function hideInactiveMenus(){
	
	//Set all classes active on a in main menu to normal
	$('#navigation').find('> ul > li').each(function(){
		$(this).find('a.active').toggleClass('active');
	});
	
	//Show active menu (if any)
	$('#navigation').find('.sub').find('ul').each(function(){
		//$(this).show();
		if($(this).hasClass('active'))
			$(this).show();
		else
			$(this).hide();
	});
	
}


$(document).ready(function(){
	bindTopMenuElements();
	
	if(isHome){
		setInterval(changeHomePicture,5000);
	}
	
	
});

function changeHomePicture(){

	var randomnumber=Math.floor(Math.random()*6);
	for (i=0;i<pictures.length;i++){
		if($('#main').hasClass(pictures[i])){
			//alert(pictures[i]);
			//alert(pictures[randomnumber]);
			$('#main').removeClass(pictures[i]);
			//Add random class
			$('#main').addClass(pictures[randomnumber]);
		}
			
	}
	
}