﻿/*-------------------------------------------------------------------------------------
>	Define the pretty scroll plugin function.
-------------------------------------------------------------------------------------*/
(function($) {
    $.fn.prettyScroll = function(config) {
        var methods = {
            track: function() {
                var delta = this.diff;
                this.diff = 0;
                var scrollbar = this.find(".scroll-bar");
                var value = scrollbar.slider("option", "value") + (delta * this.settings.pace);

                if (value > 100) { value = 100; }
                else if (value < 0) { value = 0; }

                scrollbar.slider("option", "value", value);
            }
        };

        return this.each(function() {
            var element = $(this);
            var timer;
            element.diff = 0; // Used to accumulate deltas when capturing mousewheel scroll to prevent excessive scrolling.

            element.settings = {
                scrollbarHeight: "300px", // the height of the scrollbar
                scrollbarSize: "6px", // the size of the scrollbar
                positionX: "5px", // the distance from the right of the container
                positionY: "15px", // the distance from the top of the container
                easing: "easeOutQuart", // the named easing effect from jQuery UI
                pace: 20 // affects the mousewheel increment
            };

            if (config) {
                $.extend(element.settings, config);
            }

            // Add the scrollbar container
            element.css("position", "relative").append("<div class='scroll-bar' style='position:absolute; height:"
							+ element.settings.scrollbarHeight
							+ "; font-size:" + element.settings.scrollbarSize
							+ "; top:" + element.settings.positionY
							+ "; right:" + element.settings.positionX + ";'></div>");

            // Hook up the mousewheel
            element.mousewheel(function(event, delta) {
                clearTimeout(timer);
                element.diff += delta;
                timer = setTimeout(jQuery.proxy(methods.track, element), 100);
            });

            // Initialize the slider
            element.find(".scroll-bar").slider({
                orientation: "vertical",
                range: "min",
                min: 0,
                max: 100,
                value: 100,
                change: function(event, ui) {
                    // Calculate and set the scrolling position.
                    var scroll = element.find(".scrollable");
                    var contentHeight = scroll.attr("scrollHeight");
                    var visibleHeight = scroll.attr("clientHeight");
                    var sliderValue = ui.value;

                    var target = Math.ceil((100 - sliderValue) * (contentHeight - visibleHeight) / 100);

                    scroll.animate(
									{ "scrollTop": target },
									'fast',
									element.settings.easing
								);
                }
            });
        });
    }
})(jQuery);

/*-------------------------------------------------------------------------------------
>	(2) Call code here
-------------------------------------------------------------------------------------*/
$(function() {
    /*
    Settings and defaults:

					scrollbarHeight: "300px", // the height of the scrollbar
    scrollbarSize: "6px", // the size of the scrollbar
    positionX: "5px", // the distance from the right of the container
    positionY: "15px", // the distance from the top of the container
    easing: "easeOutQuart", // the named easing effect from jQuery UI
    pace: 20 // affects the mousewheel increment
    */
    //$("#content, #content3, .full-row").prettyScroll();
    //$("#content2").prettyScroll({ "scrollbarHeight": "400px" });
$(".scroll-text, .scroll-row, .charter-scroll-text").prettyScroll();
    // Call with multiple:
    //$("#content, #content2").prettyScroll();
});
