$(function() {
	$('div.faqQuestion').each(function(index){
		$(this).attr("id", "q" + index );
		$(this).next(".answer").attr("id", "a" + index );
	});
	
	$('div.faqQuestion').click(function(){
		var answerDiv = $(this).next();
		
		if (answerDiv.hasClass("answer")) {
			
			var height = answerDiv.height(),
				H = $(window).height(),
				prevheight = $(this).parent('.faqHolder').prev().children('.answer').height();
				targetOffset = ((H/2) <= $(this).offset().top) ?
									(($(this).offset().top - (H/2)) - prevheight):
										(((H/2) - $(this).offset().top) - prevheight);
			
			$(".answer").removeClass("selected").fadeOut("fast");
			
			answerDiv.fadeIn().addClass("selected");
			
			$('html, body').animate({scrollTop: targetOffset}, 'slow');
		}
	});
});

