$(document).ready(function() {
	
	countProducts();
	$(".neverEmpty").neverEmpty();
	$(".form_submit").click(function(e){e.preventDefault(); $(".form_produktberater *").removeAttr("disabled"); $(this).parents('form').submit();});
	
	$(".tpl .tpl_collapse .tpl_collapse_headline a.tpl_collapse_toggle, .tpl .tpl_collapse .tpl_collapse_headline_red a.tpl_collapse_toggle, .tpl .tpl_collapse .tpl_collapse_headline_blue a.tpl_collapse_toggle").click(function(e){
		var tpl = $(this).parents(".tpl_collapse");
		tpl.children(".tpl_collapseable").stop(true,true);
		$(this).parents(".tpl").find(".form_standort_suchen").toggleClass("form_standort_suchen_grey");
		tpl.children(".tpl_collapseable").slideToggle("fast", function() {
			$(this).parents(".tpl").find(".tpl_collapse_toggle").toggleClass("tpl_collapse_toggle_plus");
		});
		e.preventDefault();
	});
	
	$(".tpl .tpl_accordion").each(function(item){
		$(this).find("h3").click(function() {
			$(this).parents(".tpl_accordion").find("h3").removeClass("active");
			$(this).parents(".tpl_accordion").find(".tpl_accordion_text").stop(true,true);
			$(this).parents(".tpl_accordion").find(".tpl_accordion_text").slideUp("fast");
			$(this).addClass("active");
			$(this).next(".tpl_accordion_text").slideDown("fast");
			var img = $(this).next(".tpl_accordion_text").children(".tpl_accordion_hidden_image");
			var newimg = $(img.html());
			$(this).parents(".tpl_accordion").find(".c33l img").fadeOut("slow",function(){
				$(this).remove();
			});
			newimg.css("display","none")
			newimg.hide();
			$(this).parents(".tpl_accordion").find(".c33l").append(newimg);
			newimg.fadeIn("slow");
		});
	});
	
	$(".tpl_event_kurse_shown").click(function() {
		var kurs = $(this);
		kurs.next().stop(true,true);
		if(kurs.next().css("display") == "block") {
			kurs.next().slideUp("fast", function(){
				kurs.children("h2").removeClass("tpl_h2_collpased");
			});
		} else {
			kurs.next().slideDown("fast", function() {
				kurs.children("h2").addClass("tpl_h2_collpased");
			});
		}
	});
	
	$('#col2_content .c50l #Map area').mouseover(function(e) {
		var href = $(this).attr("href").replace(/#/,"");
		$('.tpl_switcher_box_content_box_header_red .c50r').children('div').slideUp('slow');
		$('#einzel_standort_'+href).stop(true,true);
		$('#einzel_standort_'+href).slideDown('slow');
		e.preventDefault()
		$(this).click(function(e){
			e.preventDefault();
		});
	});
	
	$('#get_sonothek').change(function(){
		window.location.href = $('#get_sonothek option:selected').val();
	});
	
	$("#produktberater_typart input, .form_produktberater select").change(function(){
		countProducts();
	});
	

	$("#col2_content .yform form").validate({
		errorPlacement: function(e,b) {
		}
	}); 
	
	$("#col2_content .yform form .required").removeAttr("onkeyup");
	
	$(".tpl .tpl_switcher").each(function(item){
		
		var th = $(this);

		th.find(".c33l, .c33r").mouseenter(function(){
			$(this).find(".bild_ueberschrift_box_bestseller").stop(true, true);
			$(this).find(".bild_ueberschrift_box_bestseller").slideToggle("fast");
			$(this).find(".text_box_bestseller").stop(true, true);
			$(this).find(".text_box_bestseller").slideToggle("fast");
		});
		th.find(".c33l, .c33r").mouseleave(function(){
			$(this).find(".bild_ueberschrift_box_bestseller").stop(true, true);
			$(this).find(".bild_ueberschrift_box_bestseller").slideToggle("fast");
			$(this).find(".text_box_bestseller").stop(true, true);
			$(this).find(".text_box_bestseller").slideToggle("fast");
		});
		
		$(".tpl .tpl_switcher .tpl_switcher_box_content_box_header_grey, .tpl .tpl_switcher .tpl_switcher_box_content_box_header_grey .subcolumns, .tpl .tpl_switcher .tpl_switcher_box_content_box_header_grey .subcolumns .c33l, .tpl .tpl_switcher .tpl_switcher_box_content_box_header_grey .subcolumns .c33r").height("200px");
	});
	
	$('.preventDefault, .preventAll').live("click",function(e){
		e.preventDefault();
	});
	
	$('.dropMenu').dropMenu();
	
	if ($('#productBasket:visible').length == 0) {
		$("#productBasket").parents(".tpl").show();
		try {
			var oTop = $("#productBasket").parents(".tpl").offset().top;
		} catch(e) {
			
		}
		$("#productBasket").parents(".tpl").hide();
	} else {
		try {
			var oTop = $("#productBasket").parents(".tpl").offset().top;
		} catch (e) {
		
		} 
	}
	var sTop = 0;
	var diff = 0;
	
	$(window).scroll(function(e){
		sTop = $(this).scrollTop();
		diff = sTop - oTop;
	});
	
	$(window).bind("scrollstop", function(){
		if($('#productBasket:visible').length > 0) {
			scrollBasket();
		}
	}); 
	
	function scrollBasket() {
		$("#col3_content").stop(true,true);
		if(diff > 0) {
			scrollBasketDown();
		} else {
			scrollBasketTop();
		}
	};
	
	function scrollBasketTop() {
		$("#col3_content").animate({
			paddingTop:  0
		},{duration: 400});
	};
	
	function scrollBasketDown() {
		$("#col3_content").animate({
			paddingTop: diff+10
		},{duration: 400});
	};
	
	$.productBasket({
		basketSelector: '.basketAddProduct',
		basketName: 'sonogeraete',
		basketScript: '/products/ajax.php',
		basketContent: $('#productBasket'),
		basketOnAdd: function(item,obj,result){
			if (item.hasClass("preventAll") == false) {
				var brick = $('<div class="productBasket_brick"><a href="' + obj.productData.data + '">' + obj.productData.name + '</a> <a product-basket-id="' + obj.productData.id + '" product-basket-data="{\'id\': \'' + obj.productData.id + '\', \'name\': \'' + obj.productData.name + '\', \'data\': \'' + obj.productData.data + '\'}" class="tpl_collapse_toggle_inline basketAddProduct"></a></div>')
				if ($("#productBasket").find('a[product-basket-id]').length == 0) {
					$("#productBasket").parents(".tpl").fadeIn("slow");
					scrollBasketDown();
				}
				$("#productBasket").prepend(brick.hide().fadeIn("slow"));
				
				$('a[product-basket-id=' + obj.productData.id + ']').each(function(){
					if ($(this).text() != "") {
						$(this).text("bereits ausgewählt").addClass("preventAll");
					}
				});
			}
		},
		basketOnRemove: function(item,obj,result){
			if (item.hasClass("preventAll") == false) {
				$("#productBasket").find('a[product-basket-id=' + obj.productData.id + ']').parent().fadeOut("fast", function(){
					$(this).remove();
					if ($("#productBasket").find('a[product-basket-id]').length == 0) {
						$("#productBasket").parents(".tpl").fadeOut("fast");
						scrollBasketTop()
					}
				});
				$('a[product-basket-id=' + obj.productData.id + ']').each(function(){
					if ($(this).text() != "") {
						$(this).text("Sammelanfrage").removeClass("preventAll");
					}
				});
			}
		},
		basketOnError: function(obj,err){
		}
	});
	
});

function countProducts() {
	$.ajax({
	  url: '/products/ajax.php',
	  type: 'post',
	  dataType: 'json',
	  data: $("#produktberater_verfahren, #produktberater_fachgebiete, #produktberater_typart input").serialize(),
	  success: function(json) {
	  	if (json) {
			$('#produktberater_anzahl').text(json.count);
			$(".form_produktberater .form_submit").click(function(e){e.preventDefault(); $(this).parents('form').submit();}).fadeTo("fast",1);
		}
	  },
	  error: function() {
		$(".form_produktberater .form_submit").click(function(e){e.preventDefault(); $(this).parents('form').submit();}).fadeTo("fast",1);
	  }
	});
	$(".form_produktberater .form_submit").unbind("click").fadeTo("fast",0.5);
}

