﻿$.fn.accordianScroller = function (options) {

    var scrollbar = $(this).slider({
        orientation: "vertical",
        min: 0,
        max: 100,
        value: 100,
        slide: function (event, ui) {
            $(this).attr('data-ui-val', ui.value);
            scrollContent.css({ 'position': 'relative', 'top': ((scrollContent.height() - scrollPane.height()) * (.01 * (ui.value - 100))) + 'px' });
        }
    });

    var scrollPane = scrollbar.parent('.PanelContent').find('.inner');
    var scrollContent = scrollbar.parent('.PanelContent').find('.innerWrap');
    var scrollHelper = scrollbar.find('.ui-slider-handle').wrap('<div class="ui-handle-helper-parent"></div>').parent();

    function sizeScrollbar($active) {
        var handleSize = $active.find('.ui-slider-handle').height();
        $active.find('.ui-handle-helper-parent').height('100%')
        .height($active.parent('.PanelContent').find('.inner').height() - handleSize)
        .css({ top: Math.floor(handleSize / 2), position: 'relative' });
    }
    scrollbar.bind('scroll:activate', function (e) {
        $this = $(this);
        if ($this.parent('.PanelContent').find('.innerWrap').height() <= $this.parent('.PanelContent').find('.inner').height()) {
            if ($this.is(":visible")) {
                $this.parent('.PanelContent').find('.inner').width(function (index, width) { return width + 10; });
                $this.hide();
            }
        } else {
            if (!$this.is(":visible")) {
                $this.parent('.PanelContent').find('.inner').width(function (index, width) { return width - 10; });
                $this.show();
                sizeScrollbar($this);
            }
        }
    });
    scrollbar.bind('scroll:adjust', function (e) {
        $this = $(this);
        if ($this.parent('.PanelContent').find('.innerWrap').height() <= $this.parent('.PanelContent').find('.inner').height()) {
            if ($this.is(":visible")) {
                $this.parent('.PanelContent').find('.inner').width(function (index, width) { return width + 10; });
                $this.hide();
            }
        } else {
            if (!$this.is(":visible")) {
                $this.parent('.PanelContent').find('.inner').width(function (index, width) { return width - 10; });
                $this.show();
                sizeScrollbar($this);
            }
        }
        scrollContent.css({ 'position': 'relative', 'top': ((scrollContent.height() - scrollPane.height()) * (.01 * ($(this).attr('data-ui-val') - 100))) + 'px' });
    });

};
