$(document).ready(function(){ 
	var animating=false;
	var menu_expanded=false;
	var submenu_expanded=false;

	var selected_menu=0;



	$("#menu").height($(document).height());
	$("#bluebar").height($("#menu").height()-20);

	$("#button_1").click(function(){
		$("#button_1").hide();
		$("#flags").hide();

		animating=true;
		$("#content_container").animate({left:600},0);
		$("#menu").animate({width:160},1000,function(){
			$("#primary_menu").show();
			animating=false;
			menu_expanded=true;
			if(selected_menu>0){
				//animating=true;
				$("#bluebar").animate({width:190},1000,function(){
					animating=false;
					submenu_expanded=true;
					$(".submenu").hide();
					$("#submenu_"+selected_menu).show();
					position=$('#menu_'+selected_menu).position();
					$("#submenu_"+selected_menu).css('margin-top',position.top);
				});
			}
		});
	})

	$("#menu").click(function(){
		$("#button_1").hide();
		$("#flags").hide();
		animating=true;
		$("#content_container").animate({left:600},0);
		$("#menu").animate({width:160},1000,function(){
			$("#primary_menu").show();
			animating=false;
			menu_expanded=true;
			
		});
	})

	$("#home_button").click(function(){
		selected_menu=0;
		$('#maincontainer').css("background-image","url('/images/backgrounds/20100513-_DSC0181.JPG')");
		$("#content_container").animate({left:600},0);
		$('#content_container').removeClass('alpha60');
		$("#content").html('');
		animating=true;
		$(".submenu").hide();
		$("#bluebar").animate({width:40},500,function(){
			animating=false;
			$("#primary_menu").hide();
			menu_expanded=false;
			$("#menu").animate({width:15},500,function(){
				submenu_expanded=false;
				$("#button_1").show();
				$("#flags").show();
				$("#primary_menu").hide();
			});
		
		});
	})

	$(".menu_item").click(function(){
		
		animating=true;
		var id_menu=$(this).attr('id').substr(5);
		var position=$(this).position();
		selected_menu=id_menu;
		if(submenu_expanded){
			$(".submenu").hide();
			$("#submenu_"+id_menu).show();
			$("#submenu_"+id_menu).css('margin-top',position.top);
		}else{
			$("#bluebar").animate({width:190},1000,function(){
				animating=false;
				submenu_expanded=true;
				$(".submenu").hide();
				$("#submenu_"+id_menu).show();
				$("#submenu_"+id_menu).css('margin-top',position.top);
			});
		}
	});

	$(".submenu_item").click(function(){
		var id_menu=$(this).attr('id').substr(5);
		$("#content_container").animate({left:600},0);		
		$("#content").html('');
		$("#menu").css('height',$("#content_container").height());
		$("#bluebar").css('height',$("#content_container").height());
		loadImage(id_menu);
		
		var milliseconds = new Date().getTime(); 

		$.ajax({ url: "/getPage.php?id="+id_menu+'&t='+milliseconds, context: document.body, success: function(data){
			
			$('#content_container').addClass('alpha60');
			$("#content").html(data);
			$("#menu").css('height',$(document).height());
			$("#bluebar").css('height',$(document).height());
			animating=true;
			$(".submenu").hide();
			$("#bluebar").animate({width:40},500,function(){
				$("#content_container").animate({left:0},1000);
				animating=false;
				$("#primary_menu").hide();
				menu_expanded=false;
				$("#menu").animate({width:15},500,function(){
					submenu_expanded=false;
					$("#button_1").show();
					$("#flags").show();
				});
			});
			
		}});
	});

	$(".menu_item_leave").click(function(){
		var id_menu=$(this).attr('id').substr(5);

		$("#content").html('');
		$("#menu").css('height',$("#content_container").height());
		$("#bluebar").css('height',$("#content_container").height());
		loadImage(id_menu);
		var milliseconds = new Date().getTime(); 
		$.ajax({ url: "/getPage.php?id="+id_menu+'&t='+milliseconds, context: document.body, success: function(data){
			$("#content_container").animate({left:0},1000);
			$('#content_container').addClass('alpha60');
			$("#content").html(data);
			$("#menu").css('height',$(document).height());
			$("#bluebar").css('height',$(document).height());		
			animating=true;
			$(".submenu").hide();
			$("#bluebar").animate({width:40},500,function(){
				animating=false;
				$("#primary_menu").hide();
				menu_expanded=false;
				$("#menu").animate({width:15},500,function(){
					submenu_expanded=false;
					$("#button_1").show();
					$("#flags").show();
					$("#primary_menu").hide();
				});
			});
			
		}});
	});
	
	$(".menu_item_2").click(function(){
		var id_menu=$(this).attr('id').substr(5);

		$("#content").html('');
		$("#content_container").animate({left:0},1000);
		$("#menu").css('height',$("#content_container").height());
		$("#bluebar").css('height',$("#content_container").height());
		$("#menu").css('height',$(document).height());
		$("#bluebar").css('height',$(document).height());
		loadImage(id_menu);
		var milliseconds = new Date().getTime(); 
		$.ajax({ url: "/getPage.php?id="+id_menu+'&t='+milliseconds, context: document.body, success: function(data){

			animating=true;
			$('#content_container').addClass('alpha60');
			$("#content").html(data);
			$(".submenu").hide();
			
			$("#bluebar").animate({width:40},500,function(){
				animating=false;
				$("#primary_menu").hide();
				menu_expanded=false;
				$("#menu").animate({width:15},500,function(){
					submenu_expanded=false;
					$("#button_1").show();
					$("#flags").show();
				});
			});
			
		}});
	});

	//$("#maincontainer_image").bind("load", function () { $(this).fadeIn(); });

	function loadImage(id_menu){
		$("#maincontainer_image").hide();
		var url;

		var t=0;
		var rnd=Math.floor(Math.random()*backgrounds.length);
		while($('#maincontainer_image').css("background-image").indexOf(backgrounds[rnd])>=0){
			var rnd=Math.floor(Math.random()*backgrounds.length);
			t++;
			if(t>100){
				alert('error');
				break;
			}
		}
			
		switch(id_menu){
			case '129':		//home
				url="url('/images/backgrounds/20100513-_DSC0181.JPG')";
				break;
			case '185':		//Algemeen
				url="url('/images/backgrounds/20100513-_DSC0181.JPG')";
				break;
			case '199':		//Geografische diversiteit 
				url="url('/images/backgrounds/2010i-29.JPG')";
				break;
			case '198':		// operationele waarden
				url="url('/images/backgrounds/naamloos29072010153202-Edit2.JPG')";
				break;
			case '216':		// Wegtransport en distributie
				url="url('/images/backgrounds/2010i-82.JPG')";
				break;
			case '217':		// Geconditioneerd transport 
				url="url('/images/backgrounds/2010i-29.JPG')";
				break;
			case '186':		// Visie en missie
				url="url('/images/backgrounds/20100513-_DSC0165.JPG')";
				break;
			case '210':		// Fenex
				url="url('/images/backgrounds/20100513-_DSC0165.JPG')";
				break;
			case '211':		// Nedexco en Fenex
				url="url('/images/backgrounds/20100513-_DSC0165.JPG')";
				break;
			case '219':		// PL
				url="url('/images/backgrounds/01092010-34-3-Edit.JPG')";
				break;
			case '218':		// Opslag en Handling
				url="url('/images/backgrounds/2010i-77.JPG')";
				break;
			case '218':		// Opslag en Handling
				url="url('/images/backgrounds/naamloos29072010160858.jpg')";
				break;
			case '221':		// Consultancy & Ontwikkeling
				url="url('/images/backgrounds/naamloos29072010160858.jpg')";
				break;
			case '187':		// Duurzaamheid en Milieu
				url="url('/images/backgrounds/01092010-19-11-Edit.JPG')";
				break;
			case '188':		// Uitgangspunt
				url="url('/images/backgrounds/01092010-19-11-Edit.JPG')";
				break;
			case '189':		// Milieumanagement
				url="url('/images/backgrounds/01092010-19-11-Edit.JPG')";
				break;
			case '195':		// Maatschappelijk Verantwoord Ondernemen
				url="url('/images/backgrounds/01092010-19-11-Edit.JPG')";
				break;

			case '181':		// Contactgegevens
				url="url('/images/backgrounds/2010i-26.JPG')";
				break;
			case '182':		// Contactformulier
				url="url('/images/backgrounds/2010i-26.JPG')";
				break;
			case '208':		// Voor ons werken
				url="url('/images/backgrounds/2010i-24.JPG')";
				break;
			case '209':		// Charter worden?
				url="url('/images/backgrounds/2010i-24.JPG')";
				break;
			default:
				var url="url('/images/backgrounds/20100513-_DSC0205.JPG')";		
		}

		
		
		
		$('#maincontainer_image').css("background-image",url);
		$('#maincontainer_image').fadeIn('slow');
	}
	
	$("#contactform").live('validate',function(){
	
	});



	$("#submit_contactform").live('click',function(){  
		var valid=true;
		if(!$("#email").valid()){
			valid=false;
		}
		if(!$("#bedrijf").valid()){
			valid=false;
		}
		if(!$("#voornaam").valid()){
			valid=false;
		}
		if(!$("#achternaam").valid()){
			valid=false;
		}
		if(!$("#onderwerp").valid()){
			valid=false;
		}
		if(!$("#vraag").valid()){
			valid=false;
		}
		if(valid){

			$.ajax({
				url: "/contact.php",
				global: false,
				type: "POST",
				data: ({
					voornaam : $("#voornaam").val(),
					achternaam : $("#achternaam").val(),					
					bedrijf : $("#bedrijf").val(),
					email : $("#email").val(),
					onderwerp : $("#onderwerp").val(),
					vraag : $("#vraag").val(),
					aanhef : $("input[@name='aanhef']:checked").val()
				}),
				dataType: "html",
				async:false,
				success: function(){
					$("#content").html('Hartelijk dank voor uw bericht,<br>wij zullen zo spoedig mogelijk, maar uiterlijk binnen 2 werkdagen reageren.');
				}

			})
		}
	});
	
	$(".search").mouseover(function(){
		$(this).css('color','#ffcc33');	
	});

	$(".search").mouseout(function(){
		$(this).css('color','#fff');	
	});

	$(".search").click(function(){
		if($(".query").val()=='')
			return;
		$.ajax({ url: "/search.php?query="+$(".query").val(), context: document.body, success: function(data){
			$('#content_container').addClass('alpha60');
			$("#content").html(data);
			$("#menu").css('height',$(document).height());
			$("#bluebar").css('height',$(document).height());
			
			animating=true;
			$(".submenu").hide();
			$("#bluebar").animate({width:40},500,function(){
				animating=false;
				$("#primary_menu").hide();
				menu_expanded=false;
				$("#menu").animate({width:15},500,function(){
					submenu_expanded=false;
					$("#button_1").show();
					$("#flags").show();
					
				});
			});
			
		}});
	});


	$(".search2").mouseover(function(){
		$(this).css('color','#ffcc33');	
	});

	$(".search2").mouseout(function(){
		$(this).css('color','#fff');	
	});


	
	
}); 

function loadArticle(id){
	$.ajax({ url: "/getArticle.php?id="+id, context: document.body, success: function(data){
		$('#content_container').addClass('alpha60');
		$("#content").html(data);
		$("#menu").css('height',$(document).height());
		$("#bluebar").css('height',$(document).height());
		animating=true;
		$(".submenu").hide();
		$("#bluebar").animate({width:40},500,function(){
			animating=false;
			$("#primary_menu").hide();
			menu_expanded=false;
			$("#menu").animate({width:15},500,function(){
				submenu_expanded=false;
				$("#button_1").show();
				$("#flags").show();
			});
		});
		
	}});
}


	function search2(){
		if($(".query2").val()=='')
			return;
		$.ajax({ url: "/search.php?query="+$(".query2").val(), context: document.body, success: function(data){
			$('#content_container').addClass('alpha60');
			$("#content").html(data);
			$("#menu").css('height',$(document).height());
			$("#bluebar").css('height',$(document).height());
			animating=true;
			$(".submenu").hide();
			$("#bluebar").animate({width:40},500,function(){
				animating=false;
				$("#primary_menu").hide();
				menu_expanded=false;
				$("#menu").animate({width:15},500,function(){
					submenu_expanded=false;
					$("#button_1").show();
					$("#flags").show();
				});
			});
			
		}});
	};
