/** The Tamar general JavaScript utilities object */

var utils = {
    collapseChildren: function(extraOptions) {
        var options = {
            parentClass: 'collapseParagraphs',
            exemptClass: '.cpNoCollapse',
            hideLinkClass: 'cpHide',
            showLinkClass: 'cpShow',
            hideLinkHTML: '[ less ]',
            showLinkHTML: '[ more ]',
            elementFilter: '',
            startHidden: true,
            showSpeed: 500,
            hideSpeed: 500
        };
        
        jQuery.extend(options,extraOptions);
        
        jQuery(document).bind(
            'ready',
            function(readyEvent) {
                jQuery('.' + options.parentClass).each(
                    function(iter) {
                        // If there are eligible children, set up the collapsing
                        if(getChildren(this).length > 0) {
                            // Add show and hide links
                            jQuery(this).append('<span class="' + options.hideLinkClass + '">' + options.hideLinkHTML + '</span>');
                            jQuery(this).append('<span class="' + options.showLinkClass + '">' + options.showLinkHTML + '</span>');
                                                        
                            // Setup show and hide links
                            jQuery(this).find('.'+options.showLinkClass).bind(
                                'click',
                                function(showEvent) {
                                    showChildren(jQuery(this).parent());
                                }
                            )
                            jQuery(this).find('.'+options.hideLinkClass).bind(
                                'click',
                                function(showEvent) {
                                    hideChildren(jQuery(this).parent());
                                }
                            )
                            
                            // Set up elements' and links' visibility
                            if(options.startHidden) {hideChildren(this, true);}
                            else {showChildren(this, true);}
                        }
                    }
                );
            }
        );
        
        /** Utility functions for collapseChildren */
        
        // Find eligible children (applying both elementFilter and exemptFilter)
        function getChildren(parent) {
            return jQuery(parent).children(options.elementFilter).not('.'+options.exemptClass+', .'+options.hideLinkClass+', .'+options.showLinkClass);
        }
        
        // Show all eligible children, also hide the show link, and show the hide one
        function showChildren(parent, now) {
            if(now) {getChildren(parent).show();}
            else {getChildren(parent).show(options.showSpeed);}
            jQuery(parent).find('.' + options.hideLinkClass).show();
            jQuery(parent).find('.' + options.showLinkClass).hide();
        }
        // Hide all eligible children, also show the show link, and hide the hide one
        function hideChildren(parent, now) {
            if(now) {getChildren(parent).hide();}
            else {getChildren(parent).hide(options.hideSpeed);}
            jQuery(parent).find('.' + options.hideLinkClass).hide();
            jQuery(parent).find('.' + options.showLinkClass).show();
        }
    }
}
