$(document).ready(function(){

	$(".product-list-wrapper").tsort({attr:"rel"});
	
	//product popup
	$(".show-desc").mouseenter(function() {
		//remove class and css
		$(this).parent().find(".product-desc").removeClass('desc_top');
		$(this).parent().find(".product-desc").css("top","0px");
		
		$(this).parent().find(".product-desc").css("position","absolute");
		$(this).parent().find(".product-desc").css("z-index","100");
		$(this).parent().find(".show-desc").css("position","relative")
		
		//show
		$(this).parent().find(".product-desc").stop(true, true).show();
		
		//get some values
		var document_height = $(document).height();
		var window_height = $(window).height();
		var element_offset = $(this).parent().find(".product-desc").offset();
		var scroll_top = $(window).scrollTop();
		var element_height = $(this).parent().find(".product-desc").height();
		
		//get the scroll bottom value
		var scroll_top_bar = window_height + scroll_top;
		var scroll_bottom = document_height - scroll_top_bar;
		
		//get the bottom offset
		var top_height = element_offset.top + element_height;
		var bottom_offset = document_height - top_height;
		
		//calculate the empty space at bottom
		var acalc = bottom_offset - scroll_bottom;
		var bcalc = acalc + element_height;
		var ccalc = bcalc - 90;
		
		if (ccalc > element_height) {
			$(this).parent().find(".product-desc").css("top","90px");
		}
		else {
			$(this).parent().find(".product-desc").css("top","-"+element_height+"px");
			$(this).parent().find(".product-desc").addClass('desc_top');
		}
	});

	$(".show-desc").mouseleave(function() {
		$(this).parent().find(".product-desc").stop(true, true).hide();	
		$(this).parent().find(".product-desc").removeClass('desc_top');
		$(this).parent().find(".product-desc").css("top","0px");
		$(this).parent().find(".product-desc").css("position","static");
		$(this).parent().find(".show-desc").css("position","static")
	});
	
	//product type fade in/out
	//show all
	$("#pr_type_all").click(function() { 
		$(".type_glutenfrei").show();
		$(".type_laktosefrei").show();
		$(".type_zuckerfrei").show();
		$(".no-type").show();
	});
	
	//glutenfrei
	$("#pr_type_glutenfrei").click(function() { 
		$(".type_glutenfrei").show();
		$(".type_laktosefrei").hide();
		$(".type_zuckerfrei").hide();
		$(".type_zuckerfrei.type_glutenfrei").show();
		$(".type_laktosefrei.type_glutenfrei").show();
		$(".no-type").hide();
	});
	
	//laktosefrei
	$("#pr_type_laktosefrei").click(function() { 
		$(".type_laktosefrei").show();
		$(".type_glutenfrei").hide();
		$(".type_zuckerfrei").hide();
		$(".type_glutenfrei.type_laktosefrei").show();
		$(".type_zuckerfrei.type_laktosefrei").show();
		$(".no-type").hide();
	});
	
	//zuckerfrei
	$("#pr_type_zuckerfrei").click(function() { 
		$(".type_laktosefrei").hide();
		$(".type_glutenfrei").hide();
		$(".type_zuckerfrei").show();
		$(".type_laktosefrei.type_zuckerfrei").show();
		$(".type_glutenfrei.type_zuckerfrei").show();
		$(".no-type").hide();
	});
	
	//related products carousel
	jQuery('#related-items').jcarousel({
		wrap: 'circular'
	});
	
	//food-info slide
	$("#food-info-heading").click(function () {
		if ($("#food-info").is(":hidden")) {
			$("#food-info").slideDown("slow");
		} 
		else {
			$("#food-info").slideUp("slow");
		}
	});
	
	//ingredients slide
	$("#ingredients h3").click(function () {
		if ($("#ingredients-content").is(":hidden")) {
			$("#ingredients-content").slideDown("slow");
		} 
		else {
			$("#ingredients-content").slideUp("slow");
		}
	});
});
