// JavaScript Document
(function($) {
	$.fn.groepsmenu = function(options) {
		var opts = $.extend({}, $.fn.groepsmenu.defaults, options);
		return this.each(function() {
			var $this = $(this);
			$this.opts 				= $.extend({}, opts);
			$this.attr('action',$this.attr('action').replace('stap=2','stap=3'));
			$this.find('.nawhider').show();
			$this.find('.naw_overview').hide();
			$this.find('.gerecht [type=text]').bind('keyup focus blur',function(){
				newval = $(this).val().replace(/(^[0]|[^0-9])/,'');
				if(newval <= 0){
					newval = '';	
				}
				if($(this).val() != newval){
					$(this).val(newval)
				}
				clearTimeout($this.opts.checkinterval);
				$this.opts.checkinterval = setTimeout(function(){ $.fn.groepsmenu.checkCourses($this); }, 100);
			});
			
			$this.find('.gerecht [type=checkbox]').bind('change click',function(){
				$.fn.groepsmenu.checkCourses($this);
			});
			
			
			$this.find('[name=aantalpersonen]').bind('keyup blur',function(){
				newval = $(this).val().replace(/[^0-9]/,'');
				if(newval <= 0){
					newval = '';	
				}
				if($(this).val() != newval){
					$(this).val(newval)+'1';
				}
				clearTimeout($this.opts.checkinterval);
				$this.opts.checkinterval = setTimeout(function(){ $.fn.groepsmenu.checkType($this); }, 100);
			});
			
			/*$this.bind('submit', function(e){
				if($this.opts.submitMessageBlocked){
					alert('Er is iets (nog) niet correct in het gekozen menu: ' + $this.opts.submitMessageBlocked);
					e.preventDefault();	
				}else{
					if(!$this.opts.submitMessage){
					//	if(!confirm('U staat op het punt om deze reservering voor '+$this.opts.amountPersons+ ' personen te maken, is alles correct ingevuld?')){
//							e.preventDefault();
//						}
					}else{
						if(!confirm('U gaat nu naar de overzichtspagina, even een snelle vraag: ' + $this.opts.submitMessage)){
							e.preventDefault();
						}
					}
				}
			}); */
			$.fn.groepsmenu.checkType($this);
		});
		
		
		
	};
	
	$.fn.groepsmenu.defaults = {
		minimumAmountPersons: 		10,
		maximumAmountPersons: 		60,
		minimumDifferentDishes: 	2,
		maximumDifferentDishes: 	4,
		minimumNeededPercentage:	0.6,
		chooseAmountFromPersons: 	21
	};
	$.fn.groepsmenu.checkCourses = function($this){
		
		$this.opts.submitMessageBlocked = '';
		$this.opts.submitMessage 		= '';
		$this.find('.gerechtstatus').show();
		if($this.opts.currentType == 'amount'){
			$this.find('.menugang').each(function(){
				totalAmount = maximumDifferentDishes = 0;
				$(this).find('input[type=text]').each(function(){
					if($(this).val()){
						aantal = parseInt($(this).val());
						if(aantal > 0){	
							if(maximumDifferentDishes >= $this.opts.maximumDifferentDishes){
								$(this).val('');
							}else{
								maximumDifferentDishes += 1;
								totalAmount += aantal;
							}
						}
					}
				});
				if(totalAmount > $this.opts.amountPersons){
					$this.opts.submitBlocked = true;
					$this.opts.submitMessageBlocked += '\n\nEr zijn teveel gerechten gekozen bij de '+$(this).attr('rel').toLowerCase() + '. Corrigeer dit aub.';
					$(this).find('.gerechtstatus').html(maximumDifferentDishes + ' van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen voor <span class="error">' + totalAmount + '</span> van de ' + $this.opts.amountPersons +' gasten.');
				}else if(totalAmount == $this.opts.amountPersons){
					$(this).find('.gerechtstatus').html(maximumDifferentDishes + ' van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen voor <span class="good">' + totalAmount + '</span> van de ' + $this.opts.amountPersons +' gasten.');
				}else{
					if(totalAmount < Math.round($this.opts.amountPersons * $this.opts.minimumNeededPercentage)){
						$this.opts.submitMessageBlocked += '\n\nEr zijn te weinig '+$(this).attr('rel').toLowerCase() + ' gekozen.';
						$(this).find('.gerechtstatus').html(''+maximumDifferentDishes + ' van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen voor <span class="error">' + totalAmount + '</span> van de ' + $this.opts.amountPersons +' gasten.');
					}else if(totalAmount < Math.round($this.opts.amountPersons)){
						$this.opts.submitMessage += '\n\nEr zijn minder '+$(this).attr('rel').toLowerCase() + ' gekozen dan het aantal gasten. Is dat correct?';
						$(this).find('.gerechtstatus').html(''+maximumDifferentDishes + '</span> van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen voor <span class="good">' + totalAmount + '</span> van de ' + $this.opts.amountPersons +' gasten.');
					}
				}
				if(maximumDifferentDishes == $this.opts.maximumDifferentDishes){
					$(this).find('input[type=text]:not([value])').fadeTo(100,0.1);
					$(this).find('input[type=text]:not([value])').unbind('focus').bind('focus',function(){$(this).blur(); alert('U heeft al ' + $this.opts.maximumDifferentDishes + ' andere gerechten gekozen, wanneer u dit gerecht wilt kiezen, zet dan een ander gerecht op 0.');})
				}else{
					$(this).find('input[type=text]').unbind('focus').fadeTo(100,1);
//					$(this).find('input[type=text]').parent().unbind('click');
				}
			});
		}else if($this.opts.currentType == 'checkboxes'){
			$this.find('.menugang').each(function(){
				differentDishes = 0;
				$(this).find('input[type=checkbox]').each(function(){
					if($(this).attr('checked')){
						if(differentDishes >= $this.opts.maximumDifferentDishes){
							$(this).removeAttr('checked');
						}else{
							differentDishes += 1;
						}
					}
				});
				
				
				if(differentDishes < $this.opts.minimumDifferentDishes){
					$this.opts.submitMessageBlocked += '\n\nEr zijn te weinig '+$(this).attr('rel').toLowerCase() + ' gekozen. Kies er minimaal '+ $this.opts.minimumDifferentDishes +' aub.';
					$(this).find('.gerechtstatus').html('<span class="error">'+differentDishes + '</span> van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen.');
				}else if(differentDishes < $this.opts.maximumDifferentDishes){
					$this.opts.submitMessage += '\n\nEr zijn minder '+$(this).attr('rel').toLowerCase() + ' gekozen dan het toegestane aantal. Is dat correct?';
					$(this).find('.gerechtstatus').html('<span class="good">'+differentDishes + '</span> van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen.');	
				}else{
					$(this).find('.gerechtstatus').html('<span class="good">'+differentDishes + '</span> van de ' + $this.opts.maximumDifferentDishes + ' gangen gekozen.');					
				}
				if(differentDishes == $this.opts.maximumDifferentDishes){
					$(this).find('input[type=checkbox]:not([checked])').attr('disabled','disabled').fadeTo(100,0.1);
				}else{
					$(this).find('input[type=checkbox]').removeAttr('disabled').fadeTo(100,1);
				}
			});
		}else{
			$this.find('.gerechtstatus').hide();
		}
	}
	
	$.fn.groepsmenu.checkType = function($this){
		$this.find('.gerecht .amount span').hide();
		$this.find('.submitrow').show();
		$this.opts.currentType = '';
		$this.find('.status div').hide();
		$this.find('.menugang').stop().fadeTo(400,0.5);
		if($this.find('[name=aantalpersonen]').val()){
			
			//HOEVEEL PERSONEN HOUDEN WE OVER?
			$this.opts.amountPersons = Math.round($this.find('[name=aantalpersonen]').val());
			
			if($this.opts.amountPersons < $this.opts.minimumAmountPersons){
				$this.find('.status [rel=normal]').show();
			}else if($this.opts.amountPersons >= $this.opts.minimumAmountPersons && $this.opts.amountPersons < $this.opts.chooseAmountFromPersons){
				$this.opts.currentType = 'checkboxes';
				$this.find('.menugang, .submitrow').stop().fadeTo(400,1);
				$this.find('.gerecht span.checkboxes').show();
				$this.find('.status [rel=checkboxes]').show();
			}else if($this.opts.amountPersons >= $this.opts.chooseAmountFromPersons && $this.opts.amountPersons <= $this.opts.maximumAmountPersons){
				$this.opts.currentType = 'amount';				
				$this.find('.menugang, .submitrow').stop().fadeTo(400,1);
				$this.find('.gerecht span.amount').show();
				$this.find('.status [rel=amount]').show();
			}else{
				$this.find('.status [rel=teveel]').show();
				$this.find('.submitrow').hide();
			}
			$.fn.groepsmenu.checkCourses($this);
		}else{
			$this.find('.submitrow').hide();
			$this.find('.status [rel=default]').show();
		}
	}
	
})(jQuery);
