﻿(function ($)
{
    $.fn.ellipsis = function (enableUpdating)
    {
        var s = document.documentElement.style;
        if (!('textOverflow' in s || 'OTextOverflow' in s))
        {
            return this.each(function ()
            {
                var el = $(this);
                if (el.css("overflow") == "hidden")
                {
                    var originalText = el.html();
                    var w = el.width();

                    var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit'
                    });
                    el.after(t);

                    var text = originalText;
                    while (text.length > 0 && t.width() > el.width())
                    {
                        text = text.substr(0, text.length - 1);
                        t.html(text + "...");
                    }
                    el.html(t.html());

                    t.remove();

                    if (enableUpdating == true)
                    {
                        var oldW = el.width();
                        setInterval(function ()
                        {
                            if (el.width() != oldW)
                            {
                                oldW = el.width();
                                el.html(originalText);
                                el.ellipsis();
                            }
                        }, 200);
                    }
                }
            });
        } else return this;
    };
})(jQuery);


(function ($)
{
    $.fn.textLimiter = function (limit, settings)
    {
        var config = { truncate: true, limitColor: '#FFCC11' };
        if (settings) $.extend(config, settings);

        function limiter(inputE)
        {
            if ((inputE.val().length > limit) && config.truncate == true)
            {
                inputE.val(inputE.val().substr(0, limit));
            }
            if ((inputE.val().length >= limit))
            {
                inputE.css('border', '1px solid ' + config.limitColor + '');
            }
            else
            {
                inputE.css('border', inputE.data('originalBorder'));
            }
            inputE.next('div').html(+inputE.val().length + '/' + limit);
        }

        this.each(function ()
        {
            var textArea = $(this);
            textArea.data('originalBorder', textArea.css('border'));
            textArea.after('<div class="textLimiter">' + textArea.val().length + '/' + limit + '.</div>');
            //On keyup check
            textArea.keyup(function ()
            {
                limiter(textArea);
            });
            //On paste check
            textArea.bind('paste', function (e)
            {
                setTimeout(function () { limiter(textArea) }, 75);
            });
        });
        return this;
    };
})(jQuery);
