var thumb;
var items;
var mouseDownThumbLocation = -1;
var mouseDownPosition;
var oldFill;

function setupScrollbar(sender) {
    items = sender.findName("items"); 
    
    thumb = sender.findName("scrollThumb");
    thumb.mouseEnter = "javascript:thumbMouseEnter";
    thumb.mouseLeave = "javascript:thumbMouseLeave";
    thumb.mouseLeftButtonDown = "javascript:thumbMouseDown";
    thumb.mouseLeftButtonUp = "javascript:thumbMouseUp";
    thumb.mouseMove = "javascript:thumbMouseMove";
}

function thumbMouseDown(sender, args) {
    thumb.captureMouse();
    mouseDownThumbLocation = thumb["canvas.top"];
    mouseDownPosition = args.Y;
    
    var thumbBackground = sender.findName("thumbBackground");
    thumbBackground.fill = "dimgray";
}

function thumbMouseUp(sender, args) {
    thumb.releaseMouseCapture();
    mouseDownThumbLocation = -1;
    
    var thumbBackground = sender.findName("thumbBackground");
    thumbBackground.fill = "gray";
}

function thumbMouseMove(sender, args) {
    var thumbBackground = sender.findName("thumbBackground");
    if (mouseDownThumbLocation != -1) {
        var newValue = mouseDownThumbLocation
        + (args.Y - mouseDownPosition);
        
        if (newValue > 145 + trackHeight - thumbBackground.height) {
            newValue = 145 + trackHeight - thumbBackground.height;
        }
        if (newValue < 145) {
            newValue = 145;
        }
        
        var itemScroll = ((newValue - 145) / thumbScale) / (trackHeight / listBoxHeight);
        
        thumb["canvas.top"] = newValue;        
        itemsContainer["canvas.top"] = -itemScroll;
    }
}

function thumbMouseEnter(sender, args) {
    if (mouseDownThumbLocation == -1) {
        var thumbBackground = sender.findName("thumbBackground");
        oldFill = thumbBackground.fill;
        thumbBackground.fill = "gray";
     }
}

function thumbMouseLeave(sender, args) {
    var thumbBackground = sender.findName("thumbBackground");
    thumbBackground.fill = oldFill;
}
