/*
*   jQuery readMore plugin
*   Copyright 2011, FFVentures
*   
*   Usage example:
*   <script type="text/javascript">
*        jQuery(document).ready(function(){
*            jQuery(".readmore-opener").readMore();
*        });
*    </script>
*    
*    Plugin Options (none are required):
*       openerBody - string indicating the HTML for the opener; default is whatever is contained in the matching element; suggested: '<p><a href="#">Read More</a></p>'
*       closerBody - string indicating the HTML for the closer; default is '<p><a href="#">Collapse ^</a></p>'
*       toggledContentWrapperElement - string indicating what type of element is used to wrap the collapsible content; default is 'div'
*       toggledContentWrapperClass - string indicating the CSS class for the collapsible content wrapper; default is 'readmore-content'
*       isCollapsedClass - string indicating the CSS class to add to the collapsible content when it is in a collapsed state; default is 'readmore-collapsed'
*       toggledContent - a jQuery object representing the toggled class; defaults to an empty string, and the plugin will use a sibling element (toggledContentWrapperElement) that has the toggledContentWrapperClass
*   
*   Version: 0.0.2
*   Date: 8/04/2011
*/
;(function($){
    $.fn.readMore = function(options)
    {
        // grab our opener so we don't have the expense of finding it each time we need it
        var opener = $(this);
        // default settings; allow override by passed options
        var settings = $.extend({
            openerBody:opener.html(),
            closerBody:"<p><a href='#'>Collapse ^</a></p>",
            toggledContentWrapperElement:"div",
            toggledContentWrapperClass:"readmore-content",
            isCollapsedClass:"readmore-collapsed",
            toggledContent:""
        },options);
        
        // the default configuration is for the toggled content to be in a sibling element to the opener
        // if the toggledContent was provided, assume it is a jQuery DOM object and move on
        if ( settings.toggledContent.length == 0 )
        {
            settings.toggledContent = opener.siblings(settings.toggledContentWrapperElement+"."+settings.toggledContentWrapperClass);
        }
        
        // default the opener body; this allows easy override of whatever the actual content is by setting it via the plugin rather than editing the content each time
        // this could also be controlled via CSS, but adding this here doesn't preclude one from using CSS to manage it; it just gives the flexibility to do it when init'ing the plugin
        opener.html(settings.openerBody);
        
        // add a 'collapsed' class we can use for a flag
        settings.toggledContent.addClass("readmore-collapsed");
        
        // first make sure that the readmore-content is collapsed on load; also make sure any <hr />s are not shown
        settings.toggledContent.hide();
        settings.toggledContent.children("hr").addClass("hide");
        
        // add click handler so we can collapse/expand
        opener.click(function(event){
            var isCollapsed = settings.toggledContent.hasClass(settings.isCollapsedClass);
            // prevent following anchor
            event.preventDefault();
            settings.toggledContent.slideToggle("fast");
            if ( isCollapsed )
            {
                opener.html(settings.closerBody);
                settings.toggledContent.removeClass(settings.isCollapsedClass);
            }
            else
            {
                opener.html(settings.openerBody);
                settings.toggledContent.addClass(settings.isCollapsedClass);
            }
        });
    }
})(jQuery);
