$(document).ready(function() {
	$('.MenuItem').hover(function() {
		$('.sub_menu').css('display','none');
		var sub_menu = $(this).attr('id').replace('page_id_','parent_id_');
		$('#'+sub_menu).css('display','block');
	}, function() {
		
	});
	
	$('.sub_menu').hover(function() {
		
	}, function() {
		$('.sub_menu').css('display','none');
	});
	
	$('li.gallery_selector').click(function() {
		current_id = $('img.active_portfolio').attr('id');
		next_id = $(this).attr('id').replace('selector_','portfolio_');
		$('img#'+current_id).fadeOut(600,function() {
			$('img#'+next_id).addClass('active_portfolio').fadeIn(600);
		}).removeClass('active_portfolio');
	});
	
	$('.fotografie_images').click(function() {
		var image = $(this).attr('src').replace('_thumb','');
		$('.Banner').css('display','none');
		$('div#photo_placer').html('<img src="'+image+'" style="margin-left:100px;margin-top:10px;margin-bottom:20px;" />');
		$('div#normal_box').css('display','none');
		$('div#photo_box').css('display','block');
	});
	
	$('img#submit_search_query').click(function() {
		$.ajax({
			type : 'GET',
			url : '/templates/search_ajax.php',
			async : true,
			data : 'search_query='+$('input#search_query').val(),
			success : function(responseText, statusText) {
				$('div#container').html(responseText);
			}
		});
	});
	
	$('img#offerte_aanvraag_versturen').click(function() {
		var submit = true;
		var error_message = '<p style="color:red;">Controleer de velden:<br />';
		var pattern = /^[a-zA-Z- ]{3,50}$/;
		if (!pattern.test($('#offerte_naam').val())) {
			submit = false;
			error_message = error_message+'- naam<br />';
		}
		var pattern = /^[a-zA-Z- ]{3,50}$/;
		if (!pattern.test($('#offerte_organisatie').val())) {
			submit = false;
			error_message = error_message+'- organisatie<br />';
		}
		var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (!pattern.test($('#offerte_email').val())) {
			submit = false;
			error_message = error_message+'- emailadres<br />';
		}
		var pattern = /^[0-9\-\+() ]{10,20}$/;
		if (!pattern.test($('#offerte_telefoon').val())) {
			submit = false;
			error_message = error_message+'- telefoonnummer<br />';
		}		
		var checked = false;
		$('.veld_optie0').each(function() {
			if ($(this).is(':checked')) {
				checked = true;
			}
		});
		if (checked == false) {
			submit = false;
			error_message = error_message+'- budget<br />';
		}
		var checked = false;
		$('.veld_optie1').each(function() {
			if ($(this).is(':checked')) {
				checked = true;
			}
		});
		if (checked == false) {
			submit = false;
			error_message = error_message+'- gewenste oplossing<br />';
		}
		var checked = false;
		$('.veld_optie2').each(function() {
			if ($(this).is(':checked')) {
				checked = true;
			}
		});
		if (checked == false) {
			submit = false;
			error_message = error_message+'- benodigde diensten<br />';
		}
		error_message = error_message+'</p>';
		
		if (submit == false) {
			$('div#form_offerte_errors').html(error_message);
		} else {
			var value_option0 = '';
			$('.veld_optie0').each(function() {
				if ($(this).is(':checked')) {
					value_option0 = value_option0+$(this).val();
				}
			});
			var value_option1 = '';
			$('.veld_optie1').each(function() {
				if ($(this).is(':checked')) {
					value_option1 = value_option1+$(this).val()+", ";
				}
			});
			var value_option2 = '';
			$('.veld_optie2').each(function() {
			if ($(this).is(':checked')) {
					value_option2 = value_option2+$(this).val()+", ";
				}
			});
			if ($('#offerte_file').val() == '') {
				$.ajax({
					type : 'GET',
					url : '/templates/offerte_aanvraag.php',
					async : true,
					data : 'offerte_naam='+$('#offerte_naam').val()+'&offerte_organisatie='+$('#offerte_naam').val()+'&offerte_organisatie='+$('#offerte_naam').val()+'&offerte_email='+$('#offerte_email').val()+'&offerte_telefoon='+$('#offerte_telefoon').val()+'&offerte_budget='+value_option0+'&offerte_option1='+value_option1+'&offerte_option2='+value_option2+'&offerte_info='+$('#offerte_info').val(),
					success : function(responseText, statusText) {
						switch (responseText) {
							case 'succes' : 
								$('div#offerte_aanvraag').fadeOut(600,function() {
									$('div#offerte_aanvraag').html('<p style="font-weight:bold;color:red;">Uw offerte aanvraag is verstuurd.</p>');
									$('div#offerte_aanvraag').fadeIn(600);
								});
							break;
							case 'error' : 
								alert('Uw offerte aanvraag kon niet verstuurd worden.');
							break;
							case 'no_data' : 
								alert('Ongeldige invoer.');
							break;
						}
					}
				});
			} else {
				$('form#form_offerte').submit();
			}
		}
	});
});