/**
 * Initialize an faq section on an element.
 *
 * @param p_options
 *          Array (optional) : i18n associative array (default : { showAllText : "Show All", hideAllText : "Hide All" })
 *
 * Element of the form :<br />
 * <code>
 *  <dl class="myfaq">
 *		<div>
 *			<dt>Question1?</dt>
 *			<dd>Answer</dd>
 *		</div>
 *		<div>
 *			<dt>Question2?</dt>
 *			<dd>Answer</dd>
 *		</div>
 *	</dl>
 * </code>
 *
 * Classes :
 * <ol>
 * <li>Faq main node :
 * <ol>
 * <li>myfaq [Required] : </li>
 * <li>noLinks [Optional] : keep from showing +/x links</li>
 * <li>imgLinks [Optional] : add open, close images</li>
 * </ol>
 * </li>
 * <li>Faq question+answer container node :
 * <ol>
 * <li>oo [Optional] : Container opened at load time</li>
 * </ol>
 * </li>
 * </ol>
 *
 * Files : jQuery.faq.js, faq.css, open.gif, close.gif
 */
(function($){
	$.fn.makefaq= function(p_options) {
			var options = {
				showAllText : "Show All",
				hideAllText : "Hide All"
			};
			$.extend(options, p_options);

			// ---- SET UP FUNCTIONS ----
			// base show hide function
			var jQueryshowhide = function() {
				var theclass = $(this).attr('class');
				var upclass = $(this).parents('.faq').attr('class');
				$(this).siblings(".aa").slideToggle(220);
				$(this).siblings("a").toggle();
				$(this).parent(".faq").toggleClass('xx').toggleClass('oo');
				if ((theclass == 'aaShow') || (theclass == 'aaHide')) {
					$(this).hide();
				}
				if (upclass.match('xx')) {
					$(this).parents().siblings('a.allHide').show();
				} else {
					if ($(this).parent(".faq").siblings(".oo").length == 0)
						$(this).parents().siblings('a.allHide').hide();
					$(this).parents().siblings('a.allShow').show();
				}
				return false;
			};
			// show-hide-all function
			var jQueryshowhideall = function() {
				var fw = $(this).siblings('.faq');
				var shaclass = $(this).attr('class');
				if (shaclass == 'allShow') {
					$(fw).children('.aa').slideDown(225);
					$(this).siblings('.xx').children('a').toggle();
					$(fw).removeClass('xx').addClass('oo');
				} else {
					$(fw).children('.aa').slideUp(225);
					$(this).siblings('.oo').children('a').toggle();
					$(fw).removeClass('oo').addClass('xx');
				}
				$(this).hide();
				$(this).siblings('a').show();
				return false;
			};

			// ---- START PROCESSING ----
			// assign class of 'faqList' to our targeted container
			$(this).addClass('faqList')
					.prepend($('<a href="#">' + options.hideAllText + '</a>').addClass('allHide')).prepend(
							$('<a href="#">' + options.showAllText + '</a>').addClass('allShow')).prepend(
							$('<br style="clear:both" />'));

			// loop the elements with a class of 'faqList'
			$('.faqList').each(function() {

				// add the faq class to all first level child elements
					// TODO : For JQuery 1.3 : Replace the two not with only one not(a,br)
					var faqch = $(this).children('*:not(a):not(br)');
					$(faqch).addClass('faq');

					$('.faq > *:first-child').addClass('qq');

					$('.faq > *:not(.qq)').addClass('aa');

					// insert links into our markup
					var fqq = $(this).children().children('.qq');
					var fcl = $(this).attr('class');
					if (!(fcl.match('noLinks'))) {
						$(sh).insertBefore(fqq);
						// $(sh).prepend($(this).children($('.faqList:not(.noLinks)
						// > .qq')));
					}
				});

			// assign class of xx to all faq without class oo in markup
			$('.faq:not(.oo)').addClass('xx');

			// close the xx content sectionswith javascript - no js, all answers load
			// open

			$('.xx > .aa').hide();

			// show the correct buttons for each div - no js, all links/buttons hidden
			// with css
			$('.xx > .aaShow').show();
			$('.oo > .aaHide').show();
			$('a.allShow').show();

			// ------ START CLICK EVENTS -----
			// aaShow shows the sibling content
			$('a.aaShow').click(jQueryshowhide);
			// aaHide hides the sibling content
			$('a.aaHide').click(jQueryshowhide);
			// q is clickable, and toggles the content
			$(".qq").click(jQueryshowhide);
			// 'show all' link shows content in all siblings('aa')
			$('a.allShow').click(jQueryshowhideall);
			// 'hide all' link hides content in all siblings('aa')
			$('a.allHide').click(jQueryshowhideall);
		};
})(jQuery);