function resizeViewport() {

	//calculate available height
	var availableSpace = $("body").height()-290; // 290 = ending 10 px above footer
	var scrollPaneHeight = 0;
	var desiredHeight = 308;
	var minimumHeight = 100;
//	console.log(availableSpace);
//	console.log($("#scrollPane").height());
	// get actual height depending on contents and compare it to the available
	if($("#scrollPane").height() > availableSpace) {
		// content height more than available set available
		scrollPaneHeight = availableSpace;
		// but assure that minimumHeight is the real minimum
		if (scrollPaneHeight < minimumHeight) {
			scrollPaneHeight = minimumHeight;
		}
	} else {
		// if not set to needed height (no scrolling necessary)
		scrollPaneHeight = $("#scrollPane").height();
		// content may need less than desiredHeight (or floating) but desiredHeight
		// should always be the minimum height of our content element
		if (scrollPaneHeight < desiredHeight) {
			// available height might not be desiredHeight
			if (availableSpace >= desiredHeight) {
				scrollPaneHeight = desiredHeight;
			} else {
				scrollPaneHeight = availableSpace;
				// but assure that minimumHeight is the real minimum
				if (scrollPaneHeight < minimumHeight) {
					scrollPaneHeight = minimumHeight;
				}
			}
		}
	}

	// apply calculated measures
	// viewport around scrollPane has to be enlarged as well
	$("#scrollPane").css("height",scrollPaneHeight+"px");
	$("#viewport").css("height",scrollPaneHeight+11+"px");

	// initialise scrollbar which adds containers around contents
	// using our calculated height
	$("#scrollPane").jScrollPane({
		showArrows:true,
		reinitialiseOnImageLoad: true,
		dragMaxHeight: 56,
		scrollbarWidth: 16,
		scrollbarMargin: 0
	});

	// assure that drag pane is always 56px after jScrollPane calcs
	$(".jScrollPaneDrag").css("height","56px");

	if(typeof loadGallery == 'function') {
		loadGallery();
	}


}

google.setOnLoadCallback(function() {
	$(document).ready(function() {
		$("#scrollPane img").show();
		$("#mainnav li, #mainnav ul li").hover(function() {
			$(this).addClass("active");
		},function(){
			$(this).removeClass("active");
		});

		var leftSpace = 0;
		var itemSize = 160;
		$("#mainnav > li").each(function (){
			$(this).find("ul").css("left",leftSpace+"px");
			leftSpace += itemSize;
			var submenuWidth = ($(this).find("ul").children().length)*itemSize;
			$(this).find("ul").css("width",submenuWidth+"px");
		});
		$("#mainnav > li:last-child ul").css("left","320px");
		$("#mainnav > li:last-child").prev().find("ul").css("left","320px");
		$("#mainnav li ul").show();
		if(jQuery.validator) {
			jQuery.validator.addMethod("selection", function(value, element) {
				return this.optional(element) || value != "0";
			}, jQuery.format("Select one of the options!"));
			jQuery.validator.addMethod("checkbox", function(value, element) {
				return this.optional(element) || value == "1";
			}, jQuery.format("You have to accept the privacy policy!"));
			jQuery.validator.messages.required = "";

			$("#passwordForm").validate({
				submitHandler: function(form) {
					form.submit();
				}
			});
		}

		$(".tx-rssecuredownload-pi1-codeform form input:last").addClass("submit");
		if ($(".tx-rssecuredownload-pi1-error1").length > 0) {
			$(".tx-rssecuredownload-pi1-codeform form input:last").prev().css("border","1px solid red");
		}
		$(".collection").click(function() {
			document.location.href = baseUrl+$(this).attr("id");

		});


		$(".collection").hover(function () {
			   $(this).find("div").show();
		   },
		   function () {
			   $(this).find("div").hide();
		   });

		resizeViewport();
		initializeValidation();
	//	comes with some problems as contents have to be rerendered:
	//
	//	$(window).resize(function() {
	//		content = $("#scrollPane").clone().html();
	//		$("#viewport").empty().append("<div id=\"scrollPane\"></div>")
	//		$("#scrollPane").append(content);
	//		resizeViewport();
	//	});

	});
});

function initializeValidation () {
	for(var i = 1; i<16; i++) {
		jQuery('#quantity'+i).bind("blur", function () {
			validateProduct();
		});
		jQuery('#color'+i).bind("blur", function () {
			validateProduct();
		});
		jQuery('#article'+i).bind("blur", function () {
			validateProduct();
		});

	}

}

function validateProduct(i) {
	for(var i = 1; i<16; i++) {
		if(jQuery('#quantity'+i).val()!="") {
			jQuery('#color'+i).addClass("required");
			jQuery('#article'+i).addClass("required");
		} else if (jQuery('#article'+i).val()!="") {
			jQuery('#color'+i).addClass("required");
			jQuery('#quantity'+i).addClass("required");
		} else if (jQuery('#color'+i).val()!="") {
			jQuery('#article'+i).addClass("required");
			jQuery('#quantity'+i).addClass("required");
		}	else {
				jQuery('#article'+i).removeClass("required");
				jQuery('#quantity'+i).removeClass("required");
				jQuery('#color'+i).removeClass("required");
			}
	}
	$("#passwordForm").validate();
}