jQuery(document).ready(function($) {
	
		$("#linkExpandDescriptionShow").click(function(){
			$("#longDescription").fadeIn(0).css('display','inline');
			$("#linkExpandDescriptionShowWrap").hide();
		});
		
		$("#linkExpandDescriptionHide").click(function(){
			$("#longDescription").fadeOut(0);
			$("#linkExpandDescriptionShowWrap").show();
		});	
		
		if ( $(".sammelbestellung").length ) {
			
			$('.sammelbestellung_content').accordion({
			      autoHeight: false,
			      header: ".sammelbestellung_content_headlineitem"
			    });
			
			$(".itemInput").submit(function(){
				
			});
		
			$(".sammelbestellung_content_item_qty_input").keyup(function(){
				if(isNaN($(this).val())) {
					$(this).attr("value","");		
				}				
				if($(this).val().length < 1){
					var attrNr = $(this).attr("id");
					attrNr = attrNr.replace("sammelbestellung_content_item_qty_input_", "");
					$("#sammelbestellung_content_item_total_" + attrNr).html('<span style="display: none;"></span>');
					
					var totals = 0;
					$(".sammelbestellung_content_item_total span").each(function(i){
							if($(this).text() != '') {
									var totalsNew = totals + parseFloat($(this).text());
									totals = totalsNew;
								}
						});
					var countItems = 0;
					$(".sammelbestellung_content_item_qty input").each(function(i){
						if($(this).val() != '') {
								var countItemsNew = countItems + parseFloat($(this).val());
								countItems = countItemsNew;
							}
					});
					$(".sammelbestellung_footer_count").text(countItems);
					if ( $(".sammelbestellung_minimum_order_quantity_wrap").length ) {
						if(countItems == 0) {
							$(".sammelbestellung_minimum_order_quantity_wrap").hide();
							$("form#cart_quantity").attr("onsubmit", "");
							$(".sammelbestellung_minimum_order_quantity_counter").text("");
						} else if(countItems < parseFloat($("#sammelbestellung_minimum_order_quantity").text())) {
							//$(".sammelbestellung_minimum_order_quantity_wrap").show();
							$(".sammelbestellung_minimum_order_quantity_counter").text("1");
						} else {
							$(".sammelbestellung_minimum_order_quantity_wrap").hide();
							$("form#cart_quantity").attr("onsubmit", "");
							$(".sammelbestellung_minimum_order_quantity_counter").text("");
						}
					}
					$(".sammelbestellung_footer_totals").text((Math.round(totals*100)/100).toFixed(2).toString().replace(/\./g, ","));
					
					
					if ( $("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem").length ) {
						$("#sammelbestellung_content_item_" + attrNr).parent();
						var subtotals = 0;
						$("#sammelbestellung_content_item_" + attrNr).parent().find(".sammelbestellung_content_item_total span").each(function(i){
								if($(this).text() != '') {
										var subtotalsNew = subtotals + parseFloat($(this).text());
										subtotals = subtotalsNew;
									}
							});
						var subcountItems = 0;
						$("#sammelbestellung_content_item_" + attrNr).parent().find(".sammelbestellung_content_item_qty input").each(function(i){
							if($(this).val() != '') {
									var subcountItemsNew = subcountItems + parseFloat($(this).val());
									subcountItems = subcountItemsNew;
								}
						});
						
						if(subtotals == 0) {
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count_label").html("");
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count").html("");
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals").html("");
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals_label").html("");
						} else {
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count_label").html("Summe:");
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count").html(subcountItems);
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals").html((Math.round(subtotals*100)/100).toFixed(2).toString().replace(/\./g, ","));
							$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals_label").html("EUR");
						}
					}
					
					if ( $(".sammelbestellung_set").length ) {
						var itemsCount = new Array();
						var countBlock = 0;
						$(".sammelbestellung_content_headlineitem_wrap").each(function(i){
							var countBlockItem = 0;
							itemsCount[countBlock] = 0;
							$(this).find(".sammelbestellung_content_item_qty input").each(function(i2){
								if($(this).val() != '') {
										var subcountItemsNew = itemsCount[countBlock] + parseFloat($(this).val());
										itemsCount[countBlock] = subcountItemsNew;
									}
								countBlockItem ++;
							});
							countBlock ++;
						});
						//alert(dump(itemsCount));
						
						
						for(var i = 0; i < itemsCount.length; i ++) {
							if(itemsCount[i] < parseFloat($("#sammelbestellung_minimum_order_quantity").text())) {
								$("#sammelbestellung_msg_" + i).show();
								var error = true;
							} else {
								$("#sammelbestellung_msg_" + i).hide();
							}
						}
						
						for(var i = 0; i < itemsCount.length; i ++) {
							if(i == 0) {
								var checkAllItems = itemsCount[i];
							} else {
								if(itemsCount[i] != checkAllItems) {
									var checkAllItemsError = true;
								}
							}
						}
						
						if(checkAllItemsError === true) {
							$("#sammelbestellung_msg_all").show();
							var error = true;
						} else {
							$("#sammelbestellung_msg_all").hide();
						}
						
						if(totals == 0) {
							$(".sammelbestellung_msg_counter").text("");
							$(".sammelbestellung_msg_wrap").hide();
							$(".sammelbestellung_content_item_submit_wrap input").attr("onsubmit", "");
						} else if(error === true) {
							$(".sammelbestellung_msg_counter").text("1");
							$(".sammelbestellung_content_item_submit_wrap input").attr("onsubmit", "submitAddToCart(); return false;");
						} else {
							$(".sammelbestellung_msg_counter").text("");
							$(".sammelbestellung_msg_wrap").hide();
							$(".sammelbestellung_content_item_submit_wrap input").attr("onsubmit", "");
						}
					}
					
					//----------------------------------------------------
					
					
					
				} else {
					var attrNr = $(this).attr("id");
					attrNr = attrNr.replace("sammelbestellung_content_item_qty_input_", "");
					
					var price = $("#sammelbestellung_content_item_qty_input_" + attrNr).attr("value") * $("#itemPrice" + attrNr).text().replace(/\,/g, ".");
					if(price != 0 && attrNr != '') {
							$("#sammelbestellung_content_item_total_" + attrNr).html((Math.round(price*100)/100).toFixed(2).toString().replace(/\./g, ",") + ' EUR<span style="display: none;">' + (Math.round(price*100)/100).toFixed(2).toString() + '</span>');
							
							var totals = 0;
							$(".sammelbestellung_content_item_total span").each(function(i){
									if($(this).text() != '') {
											var totalsNew = totals + parseFloat($(this).text());
											totals = totalsNew;
										}
								});
							var countItems = 0;
							$(".sammelbestellung_content_item_qty input").each(function(i){
								if($(this).val() != '') {
										var countItemsNew = countItems + parseFloat($(this).val());
										countItems = countItemsNew;
									}
							});
							$(".sammelbestellung_footer_count").text(countItems);
							if ( $(".sammelbestellung_minimum_order_quantity_wrap").length ) {
								if(countItems < parseFloat($("#sammelbestellung_minimum_order_quantity").text())) {
									//$(".sammelbestellung_minimum_order_quantity_wrap").show();
									$(".sammelbestellung_minimum_order_quantity_counter").text("1");
									$(".sammelbestellung_content_item_submit_wrap input").attr("onsubmit", "submitAddToCart(); return false;");
								} else {
									$(".sammelbestellung_minimum_order_quantity_wrap").hide();
									$(".sammelbestellung_minimum_order_quantity_counter").text("");
									$(".sammelbestellung_msg_wrap").hide();
									$(".sammelbestellung_content_item_submit_wrap input").attr("onsubmit", "");
								}
							}
							$(".sammelbestellung_footer_totals").text((Math.round(totals*100)/100).toFixed(2).toString().replace(/\./g, ","));
							
							
							if ( $("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem").length ) {
								$("#sammelbestellung_content_item_" + attrNr).parent();
								var subtotals = 0;
								$("#sammelbestellung_content_item_" + attrNr).parent().find(".sammelbestellung_content_item_total span").each(function(i){
										if($(this).text() != '') {
												var subtotalsNew = subtotals + parseFloat($(this).text());
												subtotals = subtotalsNew;
											}
									});
								var subcountItems = 0;
								$("#sammelbestellung_content_item_" + attrNr).parent().find(".sammelbestellung_content_item_qty input").each(function(i){
									if($(this).val() != '') {
											var subcountItemsNew = subcountItems + parseFloat($(this).val());
											subcountItems = subcountItemsNew;
										}
								});
								
								if(subtotals == 0) {
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count_label").html("");
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count").html("");
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals").html("");
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals_label").html("");
								} else {
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count_label").html("Summe:");
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_count").html(subcountItems);
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals").html((Math.round(subtotals*100)/100).toFixed(2).toString().replace(/\./g, ","));
									$("#sammelbestellung_content_item_" + attrNr).parent().parent().parent().find(".sammelbestellung_content_headlineitem_totals_label").html("EUR");
								}
							}
							
							
							
							
						} else {
							$("#sammelbestellung_content_item_total_" + attrNr).text('');
						}
					
					
					
					if ( $(".sammelbestellung_set").length ) {
						var itemsCount = new Array();
						var countBlock = 0;
						$(".sammelbestellung_content_headlineitem_wrap").each(function(i){
							var countBlockItem = 0;
							itemsCount[countBlock] = 0;
							$(this).find(".sammelbestellung_content_item_qty input").each(function(i2){
								if($(this).val() != '') {
										var subcountItemsNew = itemsCount[countBlock] + parseFloat($(this).val());
										itemsCount[countBlock] = subcountItemsNew;
									}
								countBlockItem ++;
							});
							countBlock ++;
						});
						//alert(dump(itemsCount));
						
						
						for(var i = 0; i < itemsCount.length; i ++) {
							if(itemsCount[i] < parseFloat($("#sammelbestellung_minimum_order_quantity").text())) {
								$("#sammelbestellung_msg_" + i).show();
								var error = true;
							} else {
								$("#sammelbestellung_msg_" + i).hide();
							}
						}
						
						for(var i = 0; i < itemsCount.length; i ++) {
							if(i == 0) {
								var checkAllItems = itemsCount[i];
							} else {
								if(itemsCount[i] != checkAllItems) {
									var checkAllItemsError = true;
								}
							}
						}
						
						if(checkAllItemsError === true) {
							$("#sammelbestellung_msg_all").show();
							var error = true;
						} else {
							$("#sammelbestellung_msg_all").hide();
						}
						
						if(error === true) {
							$(".sammelbestellung_msg_counter").text("1");
						} else {
							$(".sammelbestellung_msg_counter").text("");
							$(".sammelbestellung_msg_wrap").hide();
						}
					}
					
					
				}
			});
		
		}
		
		
		$(".sammelbestellung_content_item_qty_input").bind("keydown", null, onKeydown);

		function onKeydown(event) {
		  if (event.keyCode == 13)
		    event.preventDefault();
		}

		$("#cart_quantity").submit(function(){
			if($(".sammelbestellung_msg_counter").text() == "1") {
				$(".sammelbestellung_msg_wrap").show();
				return false;
			} else {
				if($(".sammelbestellung_minimum_order_quantity_counter").text() == "1") {
					$(".sammelbestellung_minimum_order_quantity_wrap").show();
					return false;
				} else {
				
				$(".sammelbestellung_msg_wrap").hide();
				$(".sammelbestellung_minimum_order_quantity_wrap").hide();
				}
			}
		});
		
	});


function submitQty(attrNr) {
	var price = $("#sammelbestellung_content_item_qty_input_" + attrNr).attr("value") * $("#itemPrice" + attrNr).text().replace(/\,/g, ".");
	if(price != 0 && attrNr != '') {
		$("#sammelbestellung_content_item_total_" + attrNr).text((Math.round(price*100)/100).toFixed(2).toString().replace(/\./g, ","));
		//$("#sammelbestellung_content_item_total_" + attrNr).text((Math.round(price*100)/100));
		
		var totals = 0;
		$(".sammelbestellung_content_item_total").each(function(i){
			if($(this).text() != '') {
				var totalsNew = totals + parseFloat($(this).text());
				totals = totalsNew;
			}
		});
		$(".sammelbestellung_footer").text((Math.round(totals*100)/100).toFixed(2).toString().replace(/\./g, ","));
	} else {
		$("#sammelbestellung_content_item_total_" + attrNr).text('');
	}
}

function submitAddToCart() {
	
	if($(".sammelbestellung_msg_counter").text() == "1") {
		$(".sammelbestellung_msg_wrap").show();
	} else {
		$(".sammelbestellung_msg_wrap").hide();
	}
	
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


