wp_faq = {
	
	init : function() {
		var $header = $('ol.faq h3');
		$('ol.faq .answer').hide();
		
		$header.click(
				function() {
					var $anchor = this.getAttribute("rel");
					if($anchor)document.location.href = $anchor;
					wp_faq.toggle({target:this});
				}
		);
		
		$header.hover(
			function() {
				wp_faq.highlight({target:this, value:true});
			},
			function() {
				wp_faq.highlight({target:this, value:false});
			}
		);		
	},
	
	toggle : function(evt) {
		var $header = $(evt.target);
		
		/*
		// enable only one window to open
		if(wp_faq.active && wp_faq.active.hasClass('active')){ 
			wp_faq.active.siblings('.answer').slideUp("medium");
			wp_faq.active.toggleClass('active');
		};
		*/
		
		var $answer = $header.siblings('.answer');
		if(!$header.hasClass('active')){
			$answer.slideDown("medium");
			wp_faq.active = $header;
		}else{
			$answer.slideUp("medium");
		}
		$header.toggleClass('active');
	},
	
	highlight : function(evt) {
		var $header = $(evt.target);
		
		if(evt.value == true)
		{
			// only show highlight when header is not active
			if(!$header.hasClass('active')){
				$header.addClass('highlight');
			}
		}else{
			$header.removeClass('highlight');
		}
	}
}

$(function() {
	var $href = document.location.toString();
	
	// only run script on FAQ pages
	if($href.indexOf("faq")!=-1)
	{
		wp_faq.init();
	
		// find the anchor value
		var $anchor = $href.split("#")[1];
		if($anchor)
		{
			// get the anchor element
			var $a = document.getElementById($anchor);
			
			// toggle the header and show the answer
			wp_faq.toggle({target:$($a).find('h3')});
		}
	}
});

