﻿/*
* 
* jQuery Expander plugin
* Version 0.4  (12/09/2008)
* @requires jQuery v1.1.1+
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*
* NOTE**: this is not the original .js file.  I have modified it to better suit our needs for expanding the list of links
* on the home pages. The original file is jquery.expander.js. - JP 05-10-2011
*/


(function ($) {

    $.fn.expander = function (options) {

        var opts = $.extend({}, $.fn.expander.defaults, options);
        var delayedCollapse;
        return this.each(function () {
            var $this = $(this);
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            var lists = $this.find('ul');
            var startText = lists[0].innerHTML;

            var endText = lists[1].innerHTML;
            // create necessary expand/collapse elements if they don't already exist
            if (!$('span.details', this).length) {

                $this.html([
                '<ul>',
     		startText,
            '</ul>',
     		'<span class="read-more">',
     		o.expandPrefix,
       		'<a href="#">',
       		  o.expandText,
       		'</a>',
        '</span>',
     		'<span class="details">',
            '<ul class="expanded">',
     		  endText,
              '</ul>',
     		'</span>'
     		].join('')
     	  );
            }
            var $thisDetails = $('span.details', this),
        $readMore = $('span.read-more', this);
            $thisDetails.hide();
            $readMore.find('a').click(function () {
                $readMore.hide();

                if (o.expandEffect === 'show' && !o.expandSpeed) {
                    o.beforeExpand($this);
                    $thisDetails.show();
                    o.afterExpand($this);
                    delayCollapse(o, $thisDetails);
                } else {
                    o.beforeExpand($this);
                    $thisDetails.show(o.expandEffect, null, o.expandSpeed, function () {
                        $thisDetails.css({ zoom: '' });
                        o.afterExpand($this);
                        delayCollapse(o, $thisDetails);
                    });
                }
                return false;
            });
            if (o.userCollapse) {
                $this
        .find('span.details').append('<span class="re-collapse">' + o.userCollapsePrefix + '<a href="#">' + o.userCollapseText + '</a></span>');
                $this.find('span.re-collapse a').click(function () {

                    clearTimeout(delayedCollapse);
                    var $detailsCollapsed = $(this).parents('span.details');
                    reCollapse($detailsCollapsed);
                    o.onCollapse($this, true);
                    return false;
                });
            }
        });
        function reCollapse(el) {
            el.hide('blind', null, '4000', function () { $('span.read-more').show(); });

        }
        function delayCollapse(option, $collapseEl) {
            if (option.collapseTimer) {
                delayedCollapse = setTimeout(function () {
                    reCollapse($collapseEl);
                    option.onCollapse($collapseEl.parent(), false);
                },
          option.collapseTimer
        );
            }
        }
        function rSlash(rString) {
            return rString.replace(/\//, '');
        }
    };
    // plugin defaults
    $.fn.expander.defaults = {
        slicePoint: 100,  // the number of characters at which the contents will be sliced into two parts.  // this is not being used. - JP 05-10-2011
        // Note: any tag names in the HTML that appear inside the sliced element before 
        // the slicePoint will be counted along with the text characters.
        widow: 4,  // a threshold of sorts for whether to initially hide/collapse part of the element's contents.   // this is not being used. - JP 05-10-2011
        // If after slicing the contents in two there are fewer words in the second part than 
        // the value set by widow, we won't bother hiding/collapsing anything.
        expandText: 'read more', // text displayed in a link instead of the hidden part of the element. 
        // clicking this will expand/show the hidden/collapsed text
        expandPrefix: '&hellip; ',
        collapseTimer: 0, // number of milliseconds after text has been expanded at which to collapse the text again
        expandEffect: 'fadeIn',
        expandSpeed: '',   // speed in milliseconds of the animation effect for expanding the text
        userCollapse: true, // allow the user to re-collapse the expanded text.
        userCollapseText: '[collapse expanded text]',  // text to use for the link to re-collapse the text
        userCollapsePrefix: ' ',
        beforeExpand: function ($thisEl) { },
        afterExpand: function ($thisEl) { },
        onCollapse: function ($thisEl, byUser) { }
    };
})(jQuery);
