/**
 * File: jquery.back-to-top.js
 * Created: 17.09.2014
 */
(function ($, window, document, undefined) {
    'use strict';
    /**
     * Back to top plugin.
     *     Shows selected element when scroll down if it is outside of viewport before scroll start.
     * @param {object} optons User set options.
     */
    $.fn.backToTop = function(options) {

        // Default options.
        var containerJQ = this,
            defaults = {
                showDist: 100, // Px to scroll down before showing link.
                fadeTime: 1000, // Millisec for fade animation.
                scrollTime: 500, // Millisec for scroll to top animation.
                fadeOut: false // Wether to fadeOut again. Set to true if the link has position fixed.
            };

        // User options.
        options = $.extend(defaults, options);

        // Hide from start. (Hide with css to prevent flash. This is just a fallback if css is missing.)
        containerJQ.hide();

        // Show after scrolling 100px.
        $(window).scroll(function() {
            if ($(window).scrollTop() > options.showDist) {
                containerJQ.fadeIn(options.fadeTime);
            } else if (options.fadeOut) {
                containerJQ.fadeOut(options.fadeTime);
            }
        });

        // Back to top on click.
        containerJQ.click(function(e) {
            e.preventDefault();
            $('body').animate({
                scrollTop: 0
            }, options.scrollTime);
        });
    };

})(jQuery, window, document);