currentsubmenu = null;
function showsubmenu(el){
	if(el.length > 0){
		clearTimeout(el.attr('to'));
		el.removeAttr('to');
		currentsubmenu = el;
		if(!el.attr('hoogte')){
			el.attr('hoogte',el.height());
			el.css({top:'-'+el.attr('hoogte')+'px',display:'block',left:($("#menu a[href="+el.attr('rel')+"]").offset().left - $('#container').offset().left) + 'px'})
		}			
		el.stop().animate({top:'0px'},500);
		$("#follow").stop().animate({height:el.attr('hoogte')+'px'},500);
	}
}

function hidesubmenu(el){
	if(el.length > 0){
		currentsubmenu = null;
		el.attr('to',setTimeout(function(){ hidesubmenu2(el); },50));
	}
}

function hidesubmenu2(el){
	el.stop().animate({top:'-'+el.attr('hoogte')+'px'},500);
	if(currentsubmenu == null){
		$("#follow").stop().animate({height:'0px'},500);
	}
	
}
$(document).ready(function(){
	$('a[href=#top]').bind('click',function(event){
		$('html, body').animate({scrollTop:0}, 200);
		event.preventDefault();
	});
	$("#menu a").hover(
		function(){	showsubmenu($('.submenu[rel='+$(this).attr('href')+']')); },
		function(){	hidesubmenu($('.submenu[rel='+$(this).attr('href')+']')); }
	);	   
	$(".submenu").hover(
		function(){	showsubmenu($(this));},
		function(){	hidesubmenu($(this));}
	);

 	$('#groepsreserveringen').groepsmenu({
		minimumAmountPersons: 		13,
		maximumAmountPersons: 		60,
		minimumDifferentDishes: 	2,
		maximumDifferentDishes: 	4,
		minimumNeededPercentage:	0.6,
		chooseAmountFromPersons: 	26
	});
	
	$(".pics a").fancybox({overlayOpacity:0.9,overlayColor:'#291c14'});
	$(".pics").cycle({timeout:3000});
	$("#pics").cycle({timeout:3000});
	
	
	$(".ajaxformulier").each(function(i){
		$(this).unbind('submit').bind('submit',function(e){
			submitAjaxForm($(this));
			e.preventDefault();
		});
	});
	
	$("a#route_berekenen").click(function(ev){
		ev.preventDefault();
		if(($("#rb_adres").attr("value")!="")){
			$("#rb_adres").css("border","1px solid #fff");
			if(($("#rb_plaats").attr("value")!="")){
				$("#rb_plaats").css("border","1px solid #fff");
				getDirections($("#rb_adres").attr("value")+", "+$("#rb_plaats").attr("value"));
			} else {
				$("#rb_plaats").css("border","1px solid red");
			}
		} else {
			$("#rb_adres").css("border","1px solid red");
		}
	});
});



function submitAjaxForm(form){
	var form = form;
	var data = form.serialize();
	var buttonlabel = form.find('input[type=submit]').val();
	form.find('input, textarea, checkbox, radio, select').each(function(){
		if($(this).attr('title')){
			ar = $(this).attr('name').split('[]');
			naam = ar[0];
			data += '&veldnamen['+encodeURIComponent(naam)+']=' + encodeURIComponent($(this).attr('title'))
		}
	});
	$.ajax({
		type: 	form.attr('method'),
		url: 	form.attr('action'),
		data: 	data,
		success: function(msg){
			if(form.attr('target')){
				form.find('input[type=submit]').attr('disabled',false).val(buttonlabel);
				$("#"+form.attr('target')).html(msg);
			}
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
			alert(thrownError);
		}
	});
	
	form.find('input[type=submit]').attr('disabled',true).val(form.find('input[type=hidden]').val());
	return false;	
}


var directionDisplay;
var map;

function getDirections(address) {
  var directionsService = new google.maps.DirectionsService();
  directionsService.route({origin:address, destination:'Diepstraat 44, Eijsden', provideRouteAlternatives:false, travelMode:google.maps.DirectionsTravelMode.DRIVING, unitSystem:google.maps.DirectionsUnitSystem.METRIC}, function(result, status) {
    if(status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(result);
      var geocoder = new google.maps.Geocoder();
      if(geocoder) {
        geocoder.geocode({'address':address}, function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) {
            var gLat = results[0].geometry.location.lat();
            var gLng = results[0].geometry.location.lng();
          }
          var latlng2 = new google.maps.LatLng(gLat, gLng);
          new google.maps.Marker({position:latlng2, map:map});
        });
      }
    } else {
      alert($("#hidden_foutmelding_routebeschrijving").attr("value"));
    }
  });
}

function doGoogleMaps() {
  directionsDisplay = new google.maps.DirectionsRenderer({draggable:true, suppressMarkers:true});
  var geocoder = new google.maps.Geocoder();
  if(geocoder) {
    geocoder.geocode({'address':'Diepstraat 44, Eijsden'}, function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
        var gLat = results[0].geometry.location.lat();
        var gLng = results[0].geometry.location.lng();
      }
      var latlng = new google.maps.LatLng(gLat, gLng);
      var myOptions = {zoom:15, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP};
      map = new google.maps.Map(document.getElementById('google_maps'), myOptions);
      new google.maps.Marker({position:latlng, map:map});
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById("directions"));
    });
  }
}

