// SonicFoundry - Mediasite (written 2012)

(function ($) {
    var defaultOptions = {
        overflowClass: 'ellipsis',  // str, added to element when text is truncated
        container: null,  //[optional]  jQuery selector, used for size reference
        title: false    // bool, adds original text as title to element when text is truncated
    };

    var constants = {
        dataKey: 'textOverflow'
    };

    $.extend($.fn, {
        textOverflow: function () {
            var args = arguments;

            this.each(function (i, elm) {
                var $this = $(this);

                var options = $this.data(constants.dataKey);
                if (!options) {
                    var initOptions = $.isPlainObject(args[0]) ? arguments[0] : null;
                    setup($this, initOptions);
                }


                if (typeof args[0] === "string") {
                    var cmd = args[0].toLowerCase();

                    switch (cmd) {
                        case 'update':
                            var initOptions = args[1];
                            setup($this, initOptions);
                            break;
                        case 'reflow':
                            reflow($this);
                            break;
                    }
                }
            });

            return this;
        }
    });


    var wordSeparator = ' ';
    function setup($this, initOptions) {
        var oldOptions = $this.data(constants.dataKey)
        var options = $.extend({}, defaultOptions, oldOptions, initOptions);
        $this.data(constants.dataKey, options);

        options.text = options.text || $this.text();
        options.words = options.text.split(wordSeparator);

        options.maxHeight = options.container
            ? $(options.container).height()
            : $this.height();

        reflow($this);
    }

    function reflow($this) {
        var options = $this.data(constants.dataKey);

        // Reset element
        $this.text(options.text);
        $this.removeClass(options.overflowClass);

        if (options.title) {
            $this.removeAttr('title');
        }

        if ($this.height() < 1) return; // cancel if hidden

        // Determine optimal length
        var i, len;
        i = len = options.words.length - 1;
        var truncated = false;

        // BUG41396: 	Player - Presentation title not displayed on pip layout when zoomed in 90%

        // +1 added to options.maxHeight is a bit of a hack to remedy the fact that $(options.container).height() (in setup() above) 
        // will likely report the wrong value when the browser is zoomed.  The wrong value is like less than 1 higher or lower than the 
        // real value.  For example, $(options.container).height() normally reports a value of 60, but reports 59.8833 in Firefox when 
        // zoom equals 90%.  The below logic will fail if we do not acommadate for this behavior by adding 1.  

        var elem = document.querySelector('#titleArea');
        var realHeight = Math.max(elem.scrollHeight, elem.offsetHeight, elem.clientHeight);

        while (i > 0 && $this.outerHeight() > (options.maxHeight + 1)) {
            if (!truncated) {
                // text was truncated
                $this.addClass(options.overflowClass);
                if (options.title) {
                    $this.attr('title', options.text);
                }

                truncated = true;
            }

            i--;
            var text = options.words.slice(0, i).join(wordSeparator);
            $this.text(text);
        }

    }
} (jQuery));
