/*
 * jQuery ExpandCollapse Plugin
 * 
 * @ Version: 0.1
 * 
 * @ Author: Kyle Florence (kyle.florence@gmail.com)
 * @ Update: August 5, 2009
 *
 */

(function($)
{
    // Default settings
    var settings = {
        expandText          : "more",
        collapseText        : "less",
        expandClass         : "expand",
        collapseClass       : "collapse",
        toggleElement       : "<span/>",
        expandDuration      : 0,
        collapseDuration    : 0,
        expandAnimation     : {height : "show"},
        collapseAnimation   : {height : "hide"},
        autoCollapse        : false
    };

    // Attach expand/collapse functionality to specified jQuery object(s)
    jQuery.fn.expandCollapse = function(options)
    {
        // Default settings
        var options = jQuery.extend(settings, options);

        // Handle expand/collapse clicks
        doClick = function()
        {
            // Get our elements
            var parent = $(this).parent();
            var target = parent.prev();

            // If collapsed, expand
            if (target.is(":hidden"))
            {
                // Expand target
                target.animate(options.expandAnimation, 
                        options.expandDuration);

                // Change toggle text
                $(this).text(options.collapseText);

                // Update toggle class
                parent.removeClass(options.expandClass).addClass(
                        options.collapseClass);
            }

            // If expanded, collapse
            else if (target.not(":hidden"))
            {
                // Collapse target
                target.animate(options.collapseAnimation,
                        options.collapseDuration);

                // Change toggle text
                $(this).text(options.expandText);

                // Update toggle class
                parent.removeClass(options.collapseClass).addClass(
                        options.expandClass);
            }

            // Make sure we don't go anywhere
            return false;
        };

        // Included in the case of multiple jQuery objects
        return this.each(function()
        {
            // Automatically hide content if autoCollapse is on
                if (options.autoCollapse && $(this).not(":hidden"))
                {
                    $(this).animate(options.collapseAnimation).hide();
                }
                
                if (!$(this).hasClass('is-processed-expanded')) {

                    // The element is collapsed
                    if ($(this).is(":hidden"))
                    {
                        $(this).after(
                            $(options.toggleElement).addClass(options.expandClass)
                            .append($("<a/>").attr("href", "#").text(options.expandText)
                            .click(doClick))
                        );
                    }

                    // The element is expanded
                    else if ($(this).not(":hidden"))
                    {
                        $(this).after(
                            $(options.toggleElement).addClass(options.collapseClass)
                            .append($("<a/>").attr("href", "#").text(options.collapseText)
                            .click(doClick))
                        );
                    }
                    
                    $(this).addClass('is-processed-expanded');
                }
            });
    };
})(jQuery);