/* global $ */
/* @depend TablesorterController.js */

Function.prototype.bind = Function.prototype.bind || function (thisp) {
    var fn = this;
    return function () {
      return fn.apply(thisp, arguments);
    };
};

var QfqNS = QfqNS || {};

$(document).ready( function () {
    (function (n) {

        var tablesorterController = new n.TablesorterController();
        $('.tablesorter').each(function(i) {
            tablesorterController.setup($(this), i);
        }); // end .each()

        $('.tablesorter-filter').addClass('qfq-skip-dirty');

        $('.qfq-auto-grow').each(function() {
            var minHeight = $(this).attr("rows") * 14 + 18;
            var newHeight = $(this).prop('scrollHeight');
            var maxHeight = $(this).data('max-height') || 0;

            if ($(this).val === '' || newHeight < minHeight) {
                return;
            }

            if (newHeight < maxHeight || maxHeight === 0) {
                $(this).height(newHeight);
            } else {
                $(this).height(maxHeight);
            }
        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            $('.qfq-auto-grow').each(function () {
                var minHeight = $(this).attr("rows") * 14 + 18;
                var newHeight = $(this).prop('scrollHeight');
                var maxHeight = $(this).data('max-height') || 0;

                if ($(this).val === '' || newHeight < minHeight) {
                    return;
                }

                if (newHeight < maxHeight || maxHeight === 0) {
                    $(this).height(newHeight);
                } else {
                    $(this).height(maxHeight);
                }
            });
        });

        $('.qfq-auto-grow').on('input paste', function() {
            var newHeight = $(this).prop('scrollHeight');
            var maxHeight = $(this).data('max-height') || 0;
            if ($(this).outerHeight() < newHeight) {
                if(newHeight < maxHeight || maxHeight === 0) {
                    $(this).height(newHeight);
                }
            }
        });

        $('.qfq-clear-me').each(function() {
            var myInput = $(this);
            if(!myInput.is("input,textarea")) {
                return;
            }
            var closeButton = $("<span>", {
                class: "qfq-clear-me-button",
                html: "&times;"
            });
            if (myInput.val() == '' || myInput.is('[disabled=disabled]')) {
               closeButton.addClass("hidden");
            }
            closeButton.on("click", function(e) {
                myInput.val('');
                closeButton.addClass("hidden");
            });
            $(this).after(closeButton);
            $(this).on("input", function() {
                if(myInput.val() != '' && !myInput.is('[disabled=disabled]')) {
                    closeButton.removeClass("hidden");
                } else {
                    closeButton.addClass("hidden");
                }
            });
            
        });
        n.Helper.calendar();

    })(QfqNS);
});