﻿

//Scroller functionality

var modScroller = function(scroller, scrollee, up, down, distance, bottomAveragePadding) {
    var moving = null; //flag to indicate whether scrolling is taking place

    //Attach down click event
    $(down).click(function($e) {

        $e.preventDefault();
        if (moving) return;
        moving = true;

        var sTop = $(scrollee).css('top');
        
        sTop = Math.round(parseInt(sTop.replace('px', '')) - distance);

        if (sTop < 0) sTop = -sTop;

        var holderHeight = Math.round(parseInt($(scroller).height()));
        var listHeight = Math.round(parseInt($(scrollee).height()));

        if (listHeight < holderHeight) {

            moving = null;
            return;
        }

        //alert('holderHeight: ' + holderHeight + '\nlistHeight: ' + listHeight);

        if ((sTop) > (listHeight - holderHeight)) sTop = Math.round((listHeight + (distance / bottomAveragePadding)) - holderHeight);

        $(scrollee).animate({
            top: ('-' + sTop + 'px')
        }, 'linear', function() { moving = null; });

    });

    //Attach up click event	
    $(up).click(function($e) {
        $e.preventDefault();

        if (moving) return;
        moving = true;

        var sTop = $(scrollee).css('top');
        sTop = Math.round(parseInt(sTop.replace('px', '')) + distance);

        if (sTop > 0) sTop = 0;

        $(scrollee).animate({
            top: (sTop + 'px')
        }, 'linear', function() { moving = null; });
    });
};



