(function($){
    var imageHeight = 0;
    var self;
    var timer;
    var draggingScrollBar = false;
    var currentImageIndex = 0;
    function move(){
        scrollTop = $(this).scrollTop();
        if(draggingScrollBar){
            $(this).find('img').each(function(i){
                position = $(this).position();
                if(scrollTop>position.top+imageHeight/2||scrollTop+imageHeight<position.top+imageHeight/2){
                    $(this).stop().animate({'opacity':'.5'},'fast');
                }else{
                    $(this).stop().animate({'opacity':'1'},'fast');
                    currentImageIndex=i;
                }
            });
        }else{
            if(scrollTop % imageHeight != 0){
                if(scrollTop>(currentImageIndex*imageHeight)){
                    currentImageIndex++;
                }else{
                    currentImageIndex--;
                }
            }else{
                currentImageIndex = scrollTop / imageHeight;
            }
        }
    }
    function snap(){
        self = $(this);
        self.unbind('scroll.tick').animate({scrollTop:currentImageIndex*imageHeight},'fast','linear',function(){
            if(draggingScrollBar){
                self.find('img').css('opacity','1');
                draggingScrollBar=false;
            }
            self.bind('scroll.tick.move',move).bind('scroll.tick.snap',snap);
        });
    }
    $.fn.extend({
        nativeUnitScrollBind: function(){
            self = $(this);
            imageHeight = self.find('img:first').height();
            self.
            bind('scroll.tick.move',move).
            bind('scroll.tick.snap',snap).
            mousedown(function(){
                timer = setInterval(function(){
                    draggingScrollBar=true;
                    self.unbind('scroll',snap);
                },10);
            }).
            mouseup(function(){
                clearInterval(timer);
                if(draggingScrollBar){
                    self.bind('scroll.tick.snap',snap).trigger('scroll.tick.snap');
                }
            });
        },
        nativeUnitScroll: function(){
            return this.each(function(){
                self = $(this);
                if(self.find('img:last').attr('complete')){
                    self.nativeUnitScrollBind();
                }else{
                    self.find('img:last').load(function(){
                        self.nativeUnitScrollBind();
                    });
                }
            });
        }
    });
})(jQuery);