 //if( Drupal.jsEnabled){
	$(document).ready(function(){

			$("#tableMiddle").click(function(){
				window.scrollBy(0,300);
				$("#forUserNotes").hide();
			});

            $("#tableChooseGroup").click(function(){
                /*$("#tableChooseGroup").hide();
                if (otherSugarIsChosen()) {
                    $("#forUserNotes").show();
                } else {
                    $("#forUserNotes").hide();
                }
                if (complete()) {
                    if (!otherSugarIsChosen()) {
                        $("#calcButton").show();
                        document.getNotes.notes.value = ""; // get rid of old notes
                    } else {
                        $("#saveButton").show();
                        $("#addToCartButton").show();
                    }
                }*/
            });

			$("#tablePopUp").click(function(){

				$("#tablePopUp").hide();
				if(otherSugarIsChosen()){
					$("#forUserNotes").show();
				}else{
					$("#forUserNotes").hide();
				}

				var terminalM = getTerminalMiddle();
				var terminalT = getTerminalTop();
				var branched = checkForBranch();

				var terminalTArr = getTerminalTopArr();
				var branchedArr = checkAllForBranch();




				if(branched != -1){ // if sugar is branched

					// hide those before branched sugar
					for( sq = 7; sq < branched + 6 ;sq++){
						$("#square" + sq).hide();
						squares[sq].group = -1;
						squares[sq].sub = -1;
						squares[sq].branched = 0;
						squares[sq].src = '';
					}
					// show those after branched sugar
					for( sq = branched + 6; sq < 12 ;sq++){
						$("#square" + sq).show();
					}

					// hide those after last terminal in table top if there is terminal and this terminal is >= than last branched + 6
					if( terminalTArr.length > 0 && terminalTArr[terminalTArr.length - 1] >= (branchedArr[branchedArr.length - 1])){

						for( sq = 12;  sq > terminalTArr[terminalTArr.length - 1]; sq-- ){
							$("#square" + sq).attr("src", pathToImage + "white.png");
							squares[sq].group = -1;
							squares[sq].sub = -1;
							squares[sq].branched = 0;
							squares[sq].src = '';
						}
					}else {//if(empty(2)) // show if no terminal or if branched
						//if(clickedMain > 6){ // important!!
							for(sq = clickedMain + 1; sq <= 12; sq++){
								var src = $("#square" + sq).attr("src");
								if( squares[sq].group == -1){
									$("#square" + sq).attr("src", pathToImage + "default.png");
									squares[sq].group = -1;
									squares[sq].sub = -1;
									squares[sq].branched = 0;
									squares[sq].src = '';
								}
							}
						//}
					}

					// remove squares in between
					for(count = 0; count < terminalTArr.length; count++){
						for( sq = terminalTArr[count] + 1; sq < branchedArr[count + 1]; sq++ ){
							$("#square" + sq).attr("src", pathToImage + "white.png");
							squares[sq].group = -1;
							squares[sq].sub = -1;
							squares[sq].branched = 0;
							squares[sq].src = '';
						}
					}
					$("#tableMiddle").css("margin-top", "0px");
					$("#tableTop").show();


				}else{// if sugar is not branched
					$("#tableTop").hide();
					$("#tableMiddle").css("margin-top", "70px");
				}

				if(terminalM != -1){ // if terminal sugar in middle table is chosen
					for(sq = 6; sq > terminalM; sq--){
						$("#square" + sq).attr("src", pathToImage + "white.png");
						squares[sq].group = -1;
						squares[sq].sub = -1;
						squares[sq].branched = 0;
						squares[sq].src = '';

					}
				}else if( emptyF(1)){ // is no terminal sugar in middle table is chosen and all sugars after clickedMain are empty
					for(sq = 6; sq > clickedMain; sq--){
						$("#square" + sq).attr("src", pathToImage + "default.png");
						squares[sq].group = -1;
						squares[sq].sub = -1;
						squares[sq].branched = 0;
						squares[sq].src = '';
					}
				}

			if(complete()){
				if( !otherSugarIsChosen()){ // if other sugar is chosen price cannot be computed, so calc button is not shown
					$("#calcButton").show();
					document.getNotes.notes.value = ""; // get rid of old notes
				}
				$("#saveButton").show();
				$("#addToCartButton").show();
			}else{
				$("#saveButton").hide();
				$("#addToCartButton").hide();
				$("#calcButton").hide();
			}

			});
	});
 //}