$(document).ready(function()
{
	// In order to show categories opened when printed (medium=print), the hiding process
	// is a little more complicated than a simple .hide(). We use classes and need
	// to make sure the element style attribute is removed every time.

	// If JS enabled, hide all questions within categories and all answers below the questions
	// All that don't have class "faq-category-open" or "faq-question-open" that is.
	$(".faq-category").parent().next().not(".faq-category-open").addClass("hidden");
	$(".faq-category-open .faq-question").next().not(".faq-question-open").addClass("hidden");
	
	//ensure correct link names
	$(".faq-question-open").parent().find('.faq-question-entry-preview').hide();
	$(".faq-question-open").parent().find('.faq-question').html('hide');
	$(".faq-question-open").parent().find('.faq-question').addClass('faq-question-hide');
		
	// Also, if param to hide category headings was set, hide the headings aswell.
	$(".faq-category-hidden").addClass("hidden");

	// On click on a category header
	$(".faq-category").click(function(){
		// Turn arrow of all non-clicked categories to point right.
		// Hide all categories and questions that are not within the clicked category.
		$(this).parent().parent().find('.faq-category').not(this).removeClass('expanded').parent().next().slideUp("slow", function(){ $(this).addClass("hidden").removeAttr("style"); }).find('.faq-question').next().slideUp("slow", function(){ $(this).addClass("hidden").removeAttr("style"); });

		// Turn arrow of clicked category to point down.
		// Show the clicked category content.
		$(this).blur().addClass('expanded').parent().next().css("display", "none").removeClass("hidden").slideDown();

	});

	$('.faq-question-entry-title').click(function(){
		$(this).parent().find(".faq-question").click();
	});
	
	// On click of a question
	$(".faq-question").click(function(){
	
		var wasDisplayed = !$(this).blur().next().css("display", "none").hasClass("hidden");
		
		//reset all section links
		$(this).parent().parent().find('.faq-question').html('show');
		$(this).parent().parent().find('.faq-question-entry-preview').show();
		$(this).parent().parent().find('.faq-question').removeClass('faq-question-hide');
		
		//If the section we activate gets opened up, we change the link
	
        if (!wasDisplayed) {
			$(this).parent().find('.faq-question-entry-preview').hide();
			$(this).html('hide');
			$(this).addClass('faq-question-hide');
        }
		
		// Hide all answers of questions that were not clicked now.
		// The callback function in slideUp() is in place to make the style switch synchroneous.
		// This part must be called AFTER the slideUp().
		
		//we now close all sections - except the one we clicked on
		
        $(this).parent().parent().find('.faq-question').not(this).next().slideUp("slow", function(){ $(this).addClass("hidden").removeAttr("style"); });
		
		//now we handle the section that was clicked on
		
		if (!wasDisplayed) {
			//if the section was not visible yet, display it
			$(this).blur().next().css("display", "none").removeClass("hidden").slideDown();
		} else {
			//we hide the active section
			$(this).blur().next().slideUp("slow", function(){ $(this).addClass("hidden").removeAttr("style"); });
		}
		
	});

});
