/*
    FAQ v1.1 Plugin
    
    A dynamic FAQ builder using the power of jQuery.

	Updated December 3rd, 2008 
		- Changed the span to a div - Symantically correct
		- Added an incrementor in case of a duplicate header name
		- Changed the regular expression to anything other than alphanumeric
		- Corrected the syntax to accept the faqHeader variable

    Example HTML Syntax:
    --------------------
    <div id="faq">
        <h2>Title 1</h2>
        <div>Some content</div>
        <h2>Title 2</h2>
        <div>Some content</div>
    </div>

    Example Script Syntax:
    --------------------
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.faq.js"></script>    
	<script type="text/javascript" >
	    jQuery().ready(function() {
			jQuery('#faq').makeFAQ({
				indexTitle: "My Index",
				displayIndex: true,
				faqHeader: "h2"
			});
	    });
	</script>

    Visit http://www.dscoduc.com for questions, comments, issues.
    Creative Commons Attribution-Share Alike
*/

(function(jQuery) { 
    jQuery.fn.makeFAQ = function(options) {
        var defaults = {
            indexTitle: "Index",    // Change to whatever you want to be displayed
            faqHeader: ":header",   // default grabs any header element - h1,h2, etc...
            displayIndex: false,      // Display Index
            theme:"_Simple"
        };
        var options = jQuery.extend(defaults, options);

        return this.each(function() {
            // load the parent object only once
            var jQueryobj = jQuery(this);

            // wrap parent in faqRoot div
            jQueryobj.wrap("<div id='faqRoot"+options.theme+"'></div>");
            
            // Add index div
            if(options.displayIndex) {
                jQueryobj.before("<div id='faqindex"+options.theme+"'><h2>" + options.indexTitle + "</h2><ul></ul></div>");
            };

            // Get header children using the obj ID
            var jQueryfaqEntries = jQueryobj.children(options.faqHeader);
			// counting integer - ensures unique id names
			var i = 0;
            // enumerate through each entry and perform several tasks
            jQueryfaqEntries.each(function () {
                // load object only once
                var jQueryentry = jQuery(this);

                // Get entry name
                var entryName = jQueryentry.text();
                // strip whitespaces and special characters
                var entryNameSafe = entryName.replace(/\W/g,"") + i;
				// Increment counter
                i++;

                // build index line for entry
                var itemHTML = "<li><a id='" + entryNameSafe.toString() + "Index' href='#" + entryNameSafe.toString() + "' >" + entryName + "</a></li>";
                // append the index line to the index
                jQuery('#faqindex'+options.theme+' ul').append(itemHTML);

                // add click event for index entry
                if(options.displayIndex) {
                    jQuery('#' + entryNameSafe.toString() + 'Index').click( function(){ 
                        // slide down the selected index before jumping to the bookmark    
                        jQuery('#' + entryNameSafe.toString()).next('span').slideDown('fast');
                        // make sure it gets the faqopened class
                        if(options.theme!='_SkinDefault')
                        {
                            jQuery('#' + entryNameSafe.toString()).addClass('faqopened'+options.theme);
                        }
                        else
                        {
                            jQuery('#' + entryNameSafe.toString()).addClass('subhead');
                            jQuery('#' + entryNameSafe.toString()).addClass('faqopened'+options.theme);
                        }
                     });
                };

                // add class to faq entry content
                if(options.theme!='_SkinDefault')
                {
                    jQueryentry.next("div").addClass('faqcontent'+options.theme);
                }
                else
                {
                    jQueryentry.next("div").addClass('normal');
                    jQueryentry.next("div").addClass('faqcontent'+options.theme);
                }

                if(options.theme!='_SkinDefault')
                {
                    // add title, name and id to entry
                    jQueryentry.attr({
                        title: "Click to expand/collapse",
                        name: entryNameSafe,
                        id: entryNameSafe                    
                        })
                        // add class
                        .addClass("faqclosed"+options.theme)
                        
                        // Add click event to entry
                        .click( function() {
                            jQueryentry.next('div').slideToggle('fast');
                            jQueryentry.toggleClass('faqopened'+options.theme);
                            })
                            // Collapse the span tag of the entry
                            .next('div').css({ 
                                display: "none"
                            });
                 }
                 else
                 {
                    // add title, name and id to entry
                    jQueryentry.attr({
                        title: "Click to expand/collapse",
                        name: entryNameSafe,
                        id: entryNameSafe                    
                        })
                        // add class
                        .addClass("subhead")
                        .addClass("faqclosed"+options.theme)
                        // Add click event to entry
                        .click( function() {
                            jQueryentry.next('div').slideToggle('fast');
                            jQueryentry.addClass('subhead');
                            jQueryentry.toggleClass('faqopened'+options.theme);
                            })
                            // Collapse the span tag of the entry
                            .next('div').css({ 
                                display: "none"
                            });
                 }
            }); // end enumeration of each faq entry

        }); // end this each
    }; // end function
})(jQuery);