﻿/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, undef:true, browser:true, devel:true, jquery:true, maxerr:50 */
/*global BL:true */
// todo: use bare DOM functions where possible.
// todo: implement truncate-by-height
// todo: customize easing/animation
// todo: "summarize" needs an "append" for height
// todo: make faster with singleton jquery wrap in iteration
// todo: code cleanup
// done: use legitimate DOM manipulation
// done: minimally-dom-invasive (to preserve links) i.e. http://james.padolsey.com/javascript/replacing-text-in-the-dom-its-not-that-simple/
// done: truncate html, not text
// done: summarize directly modify dom element internally
// done: refactor on top of truncation plugins
// done: make summaryMethods public for extension

(function ($) {
    $.fn.summarize = function (settings) {
        var config = {
            summaryMethod: "smart",
            summaryHeight: 50,
            summaryChars: 100
        };

        if (settings) $.extend(config, settings);

        var jq = $([0]);

        function doSummarize() {
            if ($.fn.summarize.methods[config.summaryMethod]) {
                jq[0] = this;
                $.fn.summarize.methods[config.summaryMethod](jq, config);
            }
        }

        return this.each(doSummarize);

    };

    $.fn.summarize.methods = {};

    $.fn.summarize.methods.substring = function ($el, config) { $el.html(BL.String.truncate($el.html(), config.summaryChars, false)); };
    $.fn.summarize.methods.smart = function ($el, config) { $el.truncateToLength(config.summaryChars); };
    $.fn.summarize.methods.height = function ($el, config) { };

    $.fn.summaryToggle = function (settings) {
        var config = {
            expandText: "Expand",
            collapseText: "Collapse",
            ellipsesText: "...",
            expandLinkClass: "expandLink",
            collapseLinkClass: "collapseLink",
            linkClass: "toggleLink",
            showDuration: 0,
            hideDuration: 0,
            summaryArgs: { summaryMethod: "smart", summaryChars: 100 }
        };

        if (settings) $.extend(config, settings);

        function expand() {
            $(this).parent().parent().swapWithPrev();
            return false;
        }

        function collapse() {
            $(this).parent().parent().swapWithNext();
            return false;
        }

        var jq = $([0]);

        function buildSummary() {
            jq[0] = this;
            var $this = jq;
            var $summary = $this.clone();

            $summary.summarize(config.summaryArgs);

            if ($summary.html() !== $this.html()) {
                $summary.insertAfter($this.hide());

                var $spanExpand = $('<span />')
			        .html(config.ellipsesText);

                var $spanCollapse = $('<span />')
			        .appendTo($this);

                $('<a />', {
                    'href': '#',
                    'html': config.expandText,
                    'click': expand
                })
                    .addClass(config.expandLinkClass)
                    .addClass(config.linkClass)
			        .appendTo($spanExpand);

                $('<a />', {
                    'href': '#',
                    'html': config.collapseText,
                    'click': collapse
                })
                    .addClass(config.collapseLinkClass)
                    .addClass(config.linkClass)
			        .appendTo($spanCollapse);


                $summary
                    .append($spanExpand)
                    .show();
            }
        }

        return this.each(buildSummary);
    };
})(jQuery);
