function bindTopMenuElements(){
	
	$('#main-menu').find('> ul > li').each(function(){
	
		//Fetch submenuId from rel attribute on anchor
		menuToShow = $(this).find('a').attr('rel');
		
		if($('#menu_child_' + menuToShow).length > 0){

			//$(this).find('a').attr('href','javascript:void(0);');
			$(this).find('a').bind('click',function(e){
				return false;}
			);
			
			$(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
				$('#main-menu').find('> ul > li').each(function(){
					$(this).find('a').removeClass('page-active');		
				});
				
				//Juiste op active plaatsen
				$(this).toggleClass('page-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(){
	$('#main-menu').find('#sub-menu').find('ul').each(function(){
		$(this).hide();
	});	
}




function hideInactiveMenus(){
	
	//Set all classes active on a in main menu to normal
	$('#main-menu').find('> ul > li').each(function(){
		$(this).find('a.page-active').toggleClass('page-active');
	});
	
	//Show active menu (if any)
	$('#main-menu').find('#sub-menu').find('ul').each(function(){
		//$(this).show();
		if($(this).hasClass('active'))
			$(this).show();
		else
			$(this).hide();
	});
	
}

$(document).ready(function(){

	//Every link with rel=external gets target blank
	//$('a[rel=external]').attr("target","_blank");

	//Validation search form
	$("#search").bind('submit',function(e){
		if($("#keyword").attr("value") != "" && $("#keyword").attr("value") == $("#keyword").attr("defaultValue")){
			return false;
		}
		return true;
	});

	//Validation newsletter form
	$("#newsletter").bind('submit',function(e){
		if($("#email").attr("value") != "" && $("#email").attr("value") == $("#email").attr("defaultValue")){
			return false;
		}
		return true;
	});


	//Every element that needs to clear on focus
	$('.clearOnFocusField').bind('focus blur', function (e) {
		if (e.type == 'blur' && this.value == ''){
			this.value = this.defaultValue;
		}
		else if (e.type == 'focus' && this.value == this.defaultValue){
			this.value = '';
		}
	});

	bindTopMenuElements();


});