//Comic strip thumbnail list variable.
var C2_vlab_body_list;

//Clipping region for thumbnail list.
var C2_vlab_body_list_clip; // default clip: Rect="0, 0, 626, 253"

//Variable representing the handle of the scroll bar.
var C2_scroll_handle;

//Variable representing the main scroll area.
var C2_scroll_mechanism;

//Variable representing the maximum handle position.
var C2_maxHandlePosition;

//Variable representing the maximum handle top and left.
var C2_maxHandlePoint = {X:0, Y:0};

//Variable indicating whether the scrollbar handle is currently dragging.
var C2_handleDragging = false;

//Constant value representing the bottom of the up arrow.
var C2_SMALLVALUE = 16;

//Variable representing the ratio for resizing the scrollbar.
var C2_scroll_handle_ratio;

//Variable representing the interval when continuous scrolling is active.
var C2_intervalHandle;

//Function that fires once scrollbar is loaded.
function VlabScrollbarLoaded(sender)
{
    C2_scroll_handle = sender.findName("C2_scroll_handle");
    C2_scroll_mechanism = sender.findName("C2_scroll_mechanism");
    C2_maxHandlePosition = C2_scroll_mechanism.Height - C2_SMALLVALUE - C2_scroll_handle.Height;
    C2_vlab_body_list = sender.findName("C2_vlab_body_list");
    C2_vlab_body_list_clip = sender.findName("C2_vlab_body_list_clip");
    
    // Calculate the ratio of content scrolling distance to thumb scrolling distance
    var C2 = sender.findName("C2");
    C2_scroll_handle_ratio = (C2_vlab_body_list.Height - C2.Height + 550) / (C2.Height - 2 * C2_SMALLVALUE - C2_scroll_handle.Height);
}

//Function to capture the mouse when pressing the C2_scroll_handle
function C2Scroll_onThumbMouseLeftButtonDown(sender, mouseEventArgs)
{
    C2_scroll_handle.CaptureMouse();
    C2_maxHandlePoint = mouseEventArgs.GetPosition(null);
    C2_handleDragging = true;
}

//Function to move the C2_scroll_handle along with the mouse
function C2Scroll_onThumbMouseMove(sender, mouseEventArgs)
{
  if (C2_handleDragging)
  {
    var point = mouseEventArgs.GetPosition(null);
    C2_scrollTo(C2_scroll_handle["Canvas.Top"] + point.Y - C2_maxHandlePoint.Y);
    C2_maxHandlePoint = point;
  }
}

//Function to release mouse capture when releasing the C2_scroll_handle
function C2Scroll_onThumbMouseLeftButtonUp(sender, mouseEventArgs)
{
    C2_scroll_handle.ReleaseMouseCapture();
    C2_handleDragging = false;
}

//Function to move the content and cC2_scroll_handle to the specified vertical position
function C2_scrollTo(handlePosition)
{

  // Constrain the position to the bounds of the scrollbar  
  var C2_maxHandlePosition = C2_scroll_mechanism.Height - C2_SMALLVALUE
                        - C2_scroll_handle.Height;
  handlePosition = Math.max(handlePosition, C2_SMALLVALUE);
  handlePosition = Math.min(handlePosition, C2_maxHandlePosition);

  if (C2_scroll_handle["Canvas.Top"] == handlePosition)
  {
    // We're already at the desired position.
    // Just in case this is from a continuous scroll:
    C2_stopContinuousScrolling();
  }
  else
  {
    // Move the C2_scroll_handle to the desired position
    C2_scroll_handle["Canvas.Top"] = handlePosition;
    
    // Move the content to the corresponding position
    C2_vlab_body_list["Canvas.Top"] = (C2_SMALLVALUE - handlePosition) * C2_scroll_handle_ratio + 30;
    var clipTop = parseInt(C2_vlab_body_list["Canvas.Top"]) * (-1) + 30;
    C2_vlab_body_list_clip.Rect = "0, " + clipTop + ", 626, 220";
  }
}

//Function to scroll continuously when pressing the up or down arrow
function C2Scroll_onArrowMouseLeftButtonDown(sender, mouseEventArgs)
{
  C2_startContinuousScrolling(sender.Name == "C2_scroll_arrow_up");
}

//Function to stop scrolling continuously when releasing the up or down arrow
function C2Scroll_onArrowMouseUpOrLeave(sender, mouseEventArgs)
{
  C2_stopContinuousScrolling();
}

//Function to begin continuous scrolling
function C2_startContinuousScrolling(up)
{
  var delta = C2_SMALLVALUE;
  if (up)
    delta *= -1;

  // Call scroll every couple of milliseconds, adding the delta
  //var scrollTo = delegate(this, scrollTo);
  var callback = function() { C2_scrollTo(C2_scroll_handle["Canvas.Top"] + delta); }
  C2_intervalHandle = setInterval(callback, C2_SMALLVALUE);
}

//Function to end the continuous scrolling, if it is happening
function C2_stopContinuousScrolling()
{
  clearInterval(C2_intervalHandle);
}

//Helper function for creating callbacks.
function delegate(target, callback) {
  return function() { callback.apply(target, arguments); };
}