/*
@description FAQ jQuery enhancements

@author	Bryan Gullan
@created 2007-09
*/

$(document).ready(function() {
	//attach js funtionality to dt elements
	$('dl#faq dt').each(function(list) {
		var anchorID = $(this).attr('id');
		var questionText = $(this).text().split('|');
		if(questionText[1] != null) {
			var anchorLink = '<a href="#' + anchorID +'">' + questionText[0] + '</a><br />' + questionText[1];
		}
		else {
			var anchorLink = '<a href="#' + anchorID +'">' + questionText[0] + '</a>';
		}
		$(this).empty().append(anchorLink).removeClass('active').addClass('inactive');		
	});
	
	$('dl#faq dd').each(function(list2) {
		$(this).removeClass('active').addClass('inactive').hide();
	});
	
	//ensure any element specified by an anchor in the URL is shown
	var url = document.location.href;
	var url_array = url.split('#');
	var $bookmark = url_array[1];
	if($bookmark) {
		$('dt#' + $bookmark).removeClass('inactive').addClass('active');
		$('dt#' + $bookmark).next().show();
		$('dt#' + $bookmark).next().removeClass('inactive').addClass('active');
	}
	
	
	$('dt a').click(function() {
		var $oldAnswer = $('dl#faq dd.active');
		
		var $newAnswer = $(this).parent().next();
		
		/*var $newAnswerTop = $(elem).offset().top;
		window.scrollTo($newAnswerTop - 50,0);*/
		
		if (!$oldAnswer.length) { //ensure that oldAnswer has a length so that the new answer gets opened regardless
			$oldAnswer = $('<div></div>');
		}
		/*$oldAnswer.slideUp('medium',function() {// slide up the old answer
			$newAnswer.slideDown('slow'); // then slide down the new one
		});*/
		$oldAnswer.hide(); // hide the old answer
		$newAnswer.show(); // then show down the new one
		
		//sort out the correct classes for the dt and dd elements
		$('dl#faq dt.active').removeClass('active').addClass('inactive');
		$(this).parent().removeClass('inactive').addClass('active');
		$oldAnswer.removeClass('active').addClass('inactive')
		$newAnswer.removeClass('inactive').addClass('active');
	});
});