$.fn.disableSelection = function() {
    $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function() { 
               this.onselectstart = function() { return false; };
            });
};

var formto = null;
var formtoform = null
function setFormSubmit(){
	$("#favorieten form input[type=text], #favorieten2 form input[type=text], #favorieten2 form textarea").bind("keyup",function(){
		formtoform = $(this).parents('form:eq(0)');
		clearTimeout(formto);
		formto = setTimeout(function(){
			formtoform.trigger('submit');
		}, 300);					 
	});
}
function setLinks(){
	
	$('a.favo:not(.set), #favorieten2 a.delete:not(.set), #favorieten a.delete:not(.set)').addClass('set').click(function(event){
		event.preventDefault();
		url = $(this).attr('href');
		if($('#favorieten2').length > 0){
			url += '&type=2';
		}
		$('#favorieten, #favorieten2').load(url, function(){							 
			setLinks();
			if($(this).attr('id') == 'favorieten2'){
				setFormSubmit();
				$("html, body").animate({scrollTop: $('#favorieten2').offset().top-60 }, 300);
			}
		});
		
		if(url.indexOf('?add=') > -1){
			url2 = url.replace('?add=','?del=');
			$('a.favo[rel='+$(this).attr('rel')+']').addClass('active').attr('href',url2); //.html('Plant verwijderen uit favorieten')
		}else{
			url2 = url.replace('?del=','?add=');
			$('a.favo[rel='+$(this).attr('rel')+']').removeClass('active').attr('href',url2); //.html('Plant toevoegen aan favorieten')
		}
		$('a.favo[rel='+$(this).attr('rel')+']').each(function(){
			$old = $(this).html();
			$(this).html($(this).attr('alt')).attr('alt',$old);
		});
	});
	
	$submitTO = null;
	
	$("#favorieten form:not(.jQueried), #favorieten2 form:not(.jQueried)").addClass('jQueried').unbind('submit').submit(function(){
		clearTimeout($submitTO);
		$("#favorieten form input[type=submit], #favorieten2 form input[type=submit]").stop().attr('disabled','disabled').val('Wachten..Bezig met opslaan..');
		var form = $(this);
		var data = $(this).serialize();
		
		if($('#favorieten2').length > 0){
			form.attr('action', form.attr('action')+'?type=2');
		}
		$.ajax({
			type: 	$(this).attr('method'),
			url: 	$(this).attr('action'),
			data: 	data,
			success: function(msg){
				$submitTO = setTimeout(function(){
					$("#favorieten form input[type=submit], #favorieten2 form input[type=submit]").attr('disabled','').val('Opmerkingen, Maten en Aantallen opslaan');
				}, 400);
			},
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			}
		});
		return false;
	});
}

$(document).ready(function(){
	setLinks();   
				setFormSubmit();
	$('.route:not(.jQueried)').addClass('jQueried').maps();
	$('#slideshow').cycle({ 
		delay:  2000, 
		speed:  500
	}); 
	
	$('#menu  li').each(function(){
		$(this).prepend('<span></span>');
	}).hover(function(){
		if(!$(this).hasClass('active')){
			$(this).find('span').stop().animate({height:'20px'}, 400);
			$(this).find('a').stop().animate({paddingTop:'15px',paddingBottom:'20px'}, 400);
		}
	},function(){		
		if(!$(this).hasClass('active')){
			$(this).find('span').stop().animate({height:'0px'}, 400);
			$(this).find('a').stop().animate({paddingTop:'35px',paddingBottom:'0px'}, 400);
		}
	});
	
/*
	$('#sidemenu li:not(.active) a').hover(function(){
		$(this).stop().animate({paddingBottom:'25px'}, 200);								
	}, 
	function(){
		$(this).animate({paddingBottom:'0px'}, 200);
	})

	var timeoutreverse = null;
	
	$('#menu > ul > li').hover(function(){
		$(this).addClass('hover');
		if(!$(this).find('.submenu').hasClass('active') || $(this).find('.submenu').length == 0){
			clearTimeout(timeoutreverse);
			$('.submenu.active').stop(true,true).hide('fast');
			$(this).find('.submenu').stop(true,true).show('fast');
		}
	},function(){		
		$(this).removeClass('hover');
		if(!$(this).find('.submenu').hasClass('active') || $(this).find('.submenu').length == 0){
			$(this).find('.submenu').stop(true,true).hide('fast'); 
			clearTimeout(timeoutreverse);
			timeoutreverse = setTimeout(function(){ $('.submenu.active').stop(true,true).show('fast') }, 100);
		}
	});
	
	*/
	$('a.meel').each(function(i) {
        var text = $(this).text();
        var address = text.replace(" op ", "@");
        var address = address.replace(" punt ", ".");
        $(this).attr('href', 'mailto:' + address);
		$(this).text(address);
	});
	
	
	$("#ajaxformulier:not(.jQueried)").addClass('jQueried').unbind('submit').submit(function(){
		var form = $(this);
		var data = $(this).serialize();
		$(this).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: 	$(this).attr('method'),
			url: 	$(this).attr('action'),
			data: 	data,
			success: function(msg){
				if(form.attr('target')){
					$("#"+form.attr('target')).html(msg);
				}
			},
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			}
		});
		return false;
	});
});
