﻿/**
* Scroller - lightweight vertical scroller
* By Luca Pivato
* Version 1.0
* www.recoup.com
**/

(function ($) {

    $.fn.scroller = function (options) {
        var defaults = { height: 70, gap: 5, up: '#scroller-up', down: '#scroller-down' };
        var options = $.extend({}, defaults, options);
        var $this = $(this);

        $(options.up).mousedown(function () {
            if ($this.queue().length > 0)
                return;

            var container = $this.parent();
            var position = $this.position().top + (options.height * 3) + (options.gap * 3);
            position = Math.min(position, 0);
            $this.animate({ top: position }, 'fast');
        });

        $(options.down).mousedown(function () {
            if ($this.queue().length > 0)
                return;

            var container = $this.parent();
            var position = $this.position().top - (options.height * 3) - (options.gap * 3);
            var min = container.height() - $this.height() + options.gap;
            position = Math.max(position, min);
            $this.animate({ top: position }, 'fast');
        });

        $(options.up + ',' + options.down).each(
            function (index, item) {
                var $item = $(item);
                $item.hover(
                    function () {
                        $(this).css('background-position', 'center -20px');
                    },
                    function () {
                        $(this).css('background-position', 'center 0px');
                    });
            }
        );

        /*
        $(options.up).mousedown(function () {
        var position = $this.position().top;
        $this.animate({ top: '0' }, Math.max(position * -1, 400));
        });
        $(options.down).mousedown(function () {
        var container = $this.parent();
        var position = container.height() - $this.height() + options.gap;
        $this.animate({ top: position }, Math.max(position * -1, 400));
        });

        $(options.up).mouseup(function () {
        $this.stop();
        var size = options.height + options.gap;
        var position = Math.ceil($this.position().top / size) * size;
        $this.animate({ top: position });
        });
        $(options.down).mouseup(function () {
        $this.stop();
        var size = options.height + options.gap;
        var position = Math.floor($this.position().top / size) * size;
        $this.animate({ top: position });
        });
        */
        $(options.up).dblclick(function () {
            $this.stop();
            $this.animate({ top: '0' }, 'fast');
        });

        return this;
    };
})(jQuery);

