$(document).ready(function() {

	$("a[rel=fancy_group]").fancybox();

	$.validator.messages.required = "*";
	$('#registrationForm').submit(function() {
		return validateAndSubmitForm('registrationForm');
	});
	
	$('#contactForm').submit(function() {
		return validateAndSubmitForm('contactForm');
	});
	
	$('#customerForm').submit(function() {
		return validateAndSubmitForm('customerForm');
	});

	$('#basketForm1').submit(function() {
		return validateAndSubmitForm('basketForm1');
	});

	$("input[placeholder]").placeHeld();
	
	$("#finderDetails").click(function(){
		if ($("#productType").attr("size") == 4) {
			$("#productType").attr("size",1);
			$("#productgroup").attr("size",1);
			$("#productsubgroup").attr("size",1);
			$("#finderlbl").html($("#lbl_prod_meer_details").val());
			$("#showFinderDetail").val("N");
		}
		else {
			$("#productType").attr("size",4);
			$("#productgroup").attr("size",4);
			$("#productsubgroup").attr("size",4);
			$("#finderlbl").html($("#lbl_prod_minder_details").val());
			$("#showFinderDetail").val("Y");
		}
		return false;
	});
	
	$(".filterDetails").click(function(){
		if($("#filterSelections").css("display") == "none"){
			$("#filterSelections").show();
			$("#filterLevel").hide();
			$(".filterlbl").html($("#lbl_prod_verberg_filter").val());
			$("#showFilterDetail").val("Y");
		}
		else {
			$("#filterSelections").hide();
			$("#filterLevel").show();
			$(".filterlbl").html($("#lbl_prod_toon_filter").val());
			$("#showFilterDetail").val("N");
		}
		return false;
	});
	
	$("#prodfil1").change(function(){ fillfilterLevel(); });
	$("#prodfil2").change(function(){ fillfilterLevel(); });
	$("#prodfil3").change(function(){ fillfilterLevel(); });
	$("#prodfil4").change(function(){ fillfilterLevel(); });
	$("#prodfil3sel").change(function(){ fillfilterLevel(); });
	$("#prodfil4sel").change(function(){ fillfilterLevel(); });
	
	$("#productType").change(function(){
		$('#productsubgroup')
			.find('option')
			.remove()
			.end()
			.append('<option value=""></option>')
			.val('')
		;
	 	$.ajax({
		  url: "/ajax/getProductGroup.dhtml?groep1=" + $(this).val(),
		  success: function(data) {
				$('#productgroup')
					.find('option')
					.remove()
					.end()
					.append('<option value=""></option>')
					.val('')
				;
				var arr = data.split("\n");
				for (i = 0;  i<arr.length;i++) {
					if (arr[i] != "") {
						var arr2 = arr[i].split("|");
						$('#productgroup').
						  append($("<option></option>").
						  attr("value",arr2[0]).
						  text(arr2[1])); 
					}
				}
		  }
		});
	});
	
	$("#productgroup").change(function(){
		$.ajax({
		  url: "/ajax/getProductSubgroup.dhtml?groep2=" + $(this).val() + ",groep1=" + $("#productType").val(),
		  success: function(data) {
				$('#productsubgroup')
					.find('option')
					.remove()
					.end()
					.append('<option value=""></option>')
					.val('')
				;
				var arr = data.split("\n");
				for (i = 0;  i<arr.length;i++) {
					if (arr[i] != "") {
						var arr2 = arr[i].split("|");
						$('#productsubgroup').
						  append($("<option></option>").
						  attr("value",arr2[0]).
						  text(arr2[1])); 
					}
				}
		  }
		});
	});
	
	$("#ahl1").click(function(){
		$.ajax({
		  url: "/ajax/homeDetail.dhtml?s_item=" + homeid2 + ",s_preview=" + homepreview2 + ",s_color=" + homecolor2,
		  cache: false,
		  success: function(data) {
				$("#introWrapper").html(data);
		  }
		});
		$("#hl1").removeClass();
		$("#hl1").addClass("highlight");
		$("#hl1").addClass(homecolor1);
		$.ajax({
		  url: "/ajax/homeDetailSmall.dhtml?s_item=" + homeid1 + ",s_preview=" + homepreview1,
		  success: function(data) {
				$("#ahl1").html(data);
		  }
		});
		temp_id = homeid1;
		temp_preview = homepreview1;
		temp_color = homecolor1;
		homeid1 = homeid2;
		homepreview1 = homepreview2;
		homecolor1 = homecolor2;
		homeid2 = temp_id;
		homepreview2 = temp_preview;
		homecolor2 = temp_color;
		return false;
	});
	
	$("#ahl2").click(function(){
		$.ajax({
		  url: "/ajax/homeDetail.dhtml?s_item=" + homeid3 + ",s_preview=" + homepreview3 + ",s_color=" + homecolor3,
		  cache: false,
		  success: function(data) {
				$("#introWrapper").html(data);
		  }
		});
		$("#hl2").removeClass();
		$("#hl2").addClass("highlight");
		$("#hl2").addClass(homecolor1);
		$.ajax({
		  url: "/ajax/homeDetailSmall.dhtml?s_item=" + homeid1 + ",s_preview=" + homepreview1,
		  success: function(data) {
				$("#ahl2").html(data);
		  }
		});
		temp_id = homeid1;
		temp_preview = homepreview1;
		temp_color = homecolor1;
		homeid1 = homeid3;
		homepreview1 = homepreview3;
		homecolor1 = homecolor3;
		homeid3 = temp_id;
		homepreview3 = temp_preview;
		homecolor3 = temp_color;
		return false;
	});
	
	$(".prodlink").click(function(){
		var this_id = this.id.replace("prodlink","");
		if ($("#quantity"+this_id).val() > 0) {
			$.ajax({
			  url: "/ajax/addToBasket.dhtml?pItem=" + this_id + ",pQuantity=" + $("#quantity"+this_id).val(),
			  success: function(data) {
					$("#totalShop").html("("+data+")");
			  }
			});
			$("#quantity"+this_id).val(0);
			$("#prodWinkelOk"+this_id).show();
		}	
		return false;
	});
	
	$(".quantityLink").click(function(){
		var this_id;
		var type;
		var curr_val;
		if (this.id.indexOf("addprod") == -1) {
			this_id = this.id.replace("subtractprod","");
			type = "subtract";
		}
		else {
			this_id = this.id.replace("addprod","");
			type = "add";
		}
		curr_val = parseInt($("#quantity"+this_id).val());
		if (isNaN(curr_val)) { curr_val = 0; }
		if (type == "subtract") { curr_val--; } else { curr_val++; }
		if (curr_val < 0) { curr_val = 0; }
		$("#quantity"+this_id).val(curr_val);
		$("#prodWinkelOk"+this_id).hide();
		doCounting(this_id);
		return false;
	});
	
	$(".quantity").keyup(function(){
		var this_id = this.id.replace("quantity","");
		if (isNaN($("#quantity"+this_id).val()) || $("#quantity"+this_id).val() < 0) {
			$("#quantity"+this_id).val(0);
		}
		$("#prodWinkelOk"+this_id).hide();
		doCounting(this_id);
	});
	
	$(".asslink").click(function(){
		var this_img = $(this).children('img');
		this_id = this.id.replace("agroep","");
		if(this_img.attr("src").indexOf("closed") > 0) {
			this_img.attr("src","/images/img_selection_open.png");
			$("#ulgroep"+this_id).removeClass("hide");
		}
		else {
			this_img.attr("src","/images/img_selection_closed.png");
			$("#ulgroep"+this_id).addClass("hide");
		}
		return false;
	});
	
	$(".asschecks").click(function(){
		var bool = this.checked;
		$(this).parent().find("input").attr('checked', bool);
	});
	
	$("#basketForm1 :input:text").keyup(function(){
	 	var this_id = this.id.replace("numb","");
	 	var price = $("#price"+this_id).val();
	 	var numb = this.value;
	 	if (isNaN(numb) || numb < 1) { 
	 		numb = 0; 
	 		$(this).val("");
	 	}
	 	$.ajax({
		  url: "/ajax/updateBasket.dhtml?pItem=" + this_id + ",pQuantity=" + numb,
			  success: function(data) {
					$("#totalShop").html("("+data+")");
			  }
		});
	 	var btw = $("#tax"+this_id).val();
	 	var tot_excl = price * numb;
	 	tot_excl = tot_excl.toFixed(2);
	 	var tot_incl = parseFloat(tot_excl) + parseFloat(tot_excl * btw / 100);
	 	tot_incl = tot_incl.toFixed(2);
	 	$("#tdpriceexcl"+this_id).html("€ "+tot_excl);
	 	$("#tdpriceincl"+this_id).html("€ "+tot_incl);
	 	$("#priceexcl"+this_id).val(tot_excl);
	 	$("#priceincl"+this_id).val(tot_incl);
	 	var tot_price_excl = 0;
	 	var tot_price_incl = 0;
	 	$("#basketForm1 :input:hidden").each(function(index) {
			var obj = $(this);
			if (obj.attr("name").indexOf("priceexcl") == 0) {
				tot_price_excl = tot_price_excl + parseFloat(obj.val());
			}
			if (obj.attr("name").indexOf("priceincl") == 0) {
				tot_price_incl = tot_price_incl + parseFloat(obj.val());
			}
		});
		$("#totpriceexcl").html("€ " + tot_price_excl.toFixed(2));
		$("#totpriceincl").html("€ " + tot_price_incl.toFixed(2));
	});
	
	$("#recipeShopForm :input:text").keyup(function(){
	 	var numb = this.value;
	 	if (isNaN(numb) || numb < 1) { 
	 		$(this).val("");
	 	}
	 	$("#recipeAddToBasketOk").hide();
	});
	
	$("#recipeAddToBasketLink").click(function() {
		var counter = 0;
		$("#recipeShopForm :input:text").each(function(index) {
			var obj = $(this);
			var this_id = obj.attr("id").replace("prod","");
			if (obj.val() != "") {
				counter++;
				$.ajax({
				  url: "/ajax/addToBasket.dhtml?pItem=" + this_id + ",pQuantity=" + obj.val(),
					  success: function(data) {
							$("#totalShop").html("("+data+")");
					  }
				});
			}
		});
		if (counter > 0) {
			$("#recipeAddToBasketOk").show();
		}	
		return false;
	});
	
	$("#category").change(function() {
		if(this.value == "Anders") {
			$("#category_others").show();
			$("#category_others").addClass("required");
		}
		else {
			$("#category_others").hide();
			$("#category_others").removeClass();
		}
	});
	
	$(".tabs").tabs();
	
	$("#assortmentsForm .button").click(function() {
		$(".pdf_being_created").show();
		//stupid workaround to make the animated gif work on internet explorer
		$(".pdf_being_created").html($(".pdf_being_created").html());
	});
	
});

function doCounting(this_id) {
	var val = parseFloat($("#quantity"+this_id).val());
	if (isNaN(val)) { val = 0; }
	var price = parseFloat($("#price"+this_id).val());
	price = price * val;
	$("#prodtot"+this_id).html(price.toFixed(2) + " €");
}

function validateAndSubmitForm(theForm) {
	$('#' + theForm).validate();
	return $('#' + theForm).valid();
}

function fillfilterLevel() {
	var str = "";
	var prodfil1 = "N";
	var prodfil2 = "N";
	var prodfil3 = "N";
	var prodfil4 = "N";
	if ($('#prodfil1').attr('checked')) { prodfil1 = "Y"; }
	if ($('#prodfil2').attr('checked')) { prodfil2 = "Y"; }
	if ($('#prodfil3').attr('checked')) { prodfil3 = "Y"; }
	if ($('#prodfil4').attr('checked')) { prodfil4 = "Y"; }
	if (prodfil1 =="N" && prodfil2 == "N" && prodfil3 == "N" && prodfil4 == "N") {
		str = $("#lbl_prod_alle_producten").val();
	}
	else {
		if (prodfil1 == "Y" || prodfil2 == "Y" || prodfil3 == "Y") {
			str = $("#lbl_prod_toon_producten").val() + " ";
			count = 0;
			if (prodfil1 == "Y") { 
				str = str + $("#lbl_prod_vegetariers").val(); 
				count = 1; 
			}
			if (prodfil2 == "Y") {
				if (count == 1) {
					str = str + ", ";
				} 
				str = str + $("#lbl_prod_veganisten").val(); 
				count = 1; 
			}
			if (prodfil3 == "Y") {
				if (count == 1) {
					str = str + ", ";
				} 
				str = str + $("#lbl_prod_allergie_voor").val() + " " +  $("#prodfil3sel").val(); 
			}
			str = str + "<br>";
		}
		if (prodfil4 == "Y") {
			str = str + $("#lbl_prod_toon_merk").val() + " " + $("#prodfil4sel").val();
		}
	}	
	$("#filterLevel").html(str);
}
