(function($) {

    $.widget("qusion.fillHeight", {
        options: {
            selector: '.fillh',
            win: window,
            customScroll: true,
            scrollBottom: function () { }
        },
        _create: function() {
            var self = this, options = self.options;
            var timeout;
            $(options.win)
                .on("load", function() {
                    self.refresh();
                })
                .on("resize", function() {
                    clearTimeout(timeout);

                    timeout = setTimeout(function() {
                        self.refresh();
                    }, 100);
                });

            setTimeout(function() {
                self.refresh();
            }, 20);
        },
        
        refresh: function() {
            var self = this, options = self.options;
            $(options.selector).each(function() {
                var that = $(this);
                var t = that.offset().top;
                var fill = $(options.win).height() - t;
                that.height(fill);
            });

            // if custom scroll is enabled
            if ($.fn.jScrollPane && options.customScroll === true) {
                setTimeout(function() {
                    $(options.selector).jScrollPane({ autoReinitialise: true })
                        .bind(
                            'jsp-initialised',
                            function (event, isScrollable) {
                               
                            }
                        );
                    $(options.selector).each(function () {
                        $(this).scroll(function (e) {
                            var scrollable = $(this);
                            var api = scrollable.data("jsp");
                            if (scrollable.outerHeight() + api.getContentPositionY() >= api.getContentHeight() - 1) {
                    						self._trigger("scrollBottom", e, scrollable);
                    				}
                    		});
                    });
                }, 20);
            }
        }
    });
})(jQuery);