if (!window.jqDCCAJAX)
    jqDCCAJAX = jQuery;
(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",
            effect: "none",
            tooltip: "Click to expand/collapse",
            openonlyone: "false",
            showfirstquesexpanded:"false"
        };

        var options = jQuery.extend(defaults, options);
        var chkExpCollAll = "false";
        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: options.tooltip,
                        name: entryNameSafe,
                        id: entryNameSafe
                    })
                    // add class
					.addClass("faqclosed" + options.theme)
					.attr("opened", "false")
                    // Add click event to entry
					.click(function() {
					    
					    //check openonlyone and close others if needed
					    if (jQuery(this).attr("mode") == undefined && options.openonlyone == "true" && chkExpCollAll == "false") {
					        //close all
					        var strToFind = options.faqHeader + '[opened="true"]';
					        //var thisID = jQuery(this).id;
					        var jQueryfaqAllOpenEntries = jQuery(this).parents('div#faq' + options.theme).children(strToFind).not(this);

					        jQueryfaqAllOpenEntries.each(function() {
                               // if(thisID != jQuery(this).id)
					                jQuery(this).toggleClass('faqopened' + options.theme).attr("opened", "false").next('div').css({display: "none"});
					        });
					    }
					    jQueryentry.next('div').slideToggle('fast');
					    jQueryentry.toggleClass('faqopened' + options.theme);
					    if (jQueryentry.attr("opened") == "false") {
					        jQueryentry.attr("opened", "true");
					        //apply the effects to the answers
					        if (options.effect != "") {
					            if (options.effect == "fadein") {
					                jQueryentry.next('div').animate({ opacity: 0 }, { duration: 0, queue: false }).animate({ opacity: 1 }, { duration: 1000, queue: false }); // for fadein
					            }
					            else {
					                if (options.effect == "scrollin") {
					                    var strFontSize = jQueryentry.next('div').css("font-size");
					                    jQueryentry.next('div').animate({ "fontSize": "7" }, { duration: 0, queue: false }).animate({ "fontSize": strFontSize }, { duration: 500, queue: false });
					                }
					            }
					        }
                            
					    }
					    else  {
					        jQueryentry.attr("opened", "false");
					    }
                       
					})
                    // Collapse the span tag of the entry
					.next('div').css({
					    display: "none"
					});
                  // Show first question expanded on load
                   if(i==1 && options.showfirstquesexpanded=="true")
                     {
                        jQueryentry.next('div').css({
					        display: "block"
                      
					    });
                      jQueryentry.attr("opened", "true"); 
                      jQueryentry.toggleClass('faqopened' + options.theme);
                     }
                }
                else {
                    // add title, name and id to entry
                    jQueryentry.attr({
                        title: options.tooltip,
                        name: entryNameSafe,
                        id: entryNameSafe
                    })
                    // add class
					.addClass("subhead")
					.addClass("faqclosed" + options.theme)
					.attr("opened", "false")
                    // Add click event to entry
					.click(function() {
					    if (jQuery(this).attr("mode") == undefined && options.openonlyone == "true" && chkExpCollAll == "false") {//
					        //close all
					        var strToFind = options.faqHeader + '[opened="true"]';
					        var jQueryfaqAllOpenEntries = jQuery(this).parents('div#faq' + options.theme).children(strToFind);
					        jQueryfaqAllOpenEntries.each(function() {
					            jQuery(this).toggleClass('faqopened' + options.theme).attr("opened", "false").next('div').css({
					                display: "none"
					            });
					        });
					    }
					    jQueryentry.next('div').slideToggle('fast');
					    jQueryentry.addClass('subhead');
					    jQueryentry.toggleClass('faqopened' + options.theme);
					    if (jQueryentry.attr("opened") == "false") {
					        jQueryentry.attr("opened", "true");
					        //apply the effects to the answers
					        if (options.effect != "") {
					            if (options.effect == "fadein") {
					                jQueryentry.next('div').animate({ opacity: 0 }, { duration: 0, queue: false }).animate({ opacity: 1 }, { duration: 1000, queue: false }); // for fadein
					            }
					            else {
					                if (options.effect == "scrollin") {
					                    var strFontSize = jQueryentry.next('div').css("font-size");
					                    jQueryentry.next('div').animate({ "fontSize": "7" }, { duration: 0, queue: false }).animate({ "fontSize": strFontSize }, { duration: 500, queue: false });
					                }
					            }
					        }
					    }
					    else {
					        jQueryentry.attr("opened", "false");
					    }
					})
                    // Collapse the span tag of the entry
					.next('div').css({
					    display: "none"
					});
                     if(i==1 && options.showfirstquesexpanded=="true")
                     {
                        jQueryentry.next('div').css({
					        display: "block"
                      
					    });
                      jQueryentry.attr("opened", "true");
                      jQueryentry.toggleClass('faqopened' + options.theme); 
                     }
                }
            }); // end enumeration of each faq entry
        }); // end this each
    }; // end function
})(jqDCCAJAX);
