//
// Requires:
//  createElement()         from utils.js
//  localOffsetTop()        from utils.js
//

scrolldelay = 40;
scrollstep  = 16;

function createScrollArrow(direction, scrollable) {
  var f = function(e) {
    // debug(direction + ', event=' + e); 
    arrowmousedown(scrollable, direction, e); 
  };
  var arrow = createElement('a', { 
      'className'  : direction, 
      'href'       : "#"});
  dojo.event.connect(arrow, 'onmousedown', f);
  // debug(direction+': '+arrow.onmousedown);
  return arrow; }

function createCustomScrollBar(container, scrollable) {

  // Create the custom scroll bar:

  // Create the up arrow for the scroll bar.
  var uparrow = createScrollArrow('up', scrollable);

  // Create the down arrow for the scroll bar.
  var downarrow = createScrollArrow('down', scrollable);

  // Create the scroll bar container.  Store references to arrow and
  // indicator elements so they can be accessed when scrolling.
  var scrollbar = createElement('div', {
      'className'   : 'scrollbar',
      'style.height': scrollable.offsetHeight,
      'style.top'   : -scrollable.offsetHeight,
      'uparrow'     : uparrow,
      'downarrow'   : downarrow});

  container.appendChild(scrollbar);
  scrollbar.style.left = -scrollbar.offsetWidth-5;
  container.style.paddingLeft = scrollbar.offsetWidth+5;
  
  // Insert arrows into scroll bar.
  scrollbar.appendChild(uparrow);
  scrollbar.appendChild(downarrow);
  
  // Save scrollbar object in the scrollable object so we can access it
  // when scrolling.
  scrollable['scrollbar'] = scrollbar;

  // Create a black cover to hide the stock scroll bar with.
  var scrollcover = createElement('div', {
      'className'   : 'scrollcover', 
      'style.height': scrollable.offsetHeight,
      'style.top'   : -scrollbar.offsetHeight-scrollable.offsetHeight});
  container.appendChild(scrollcover);
  scrollcover.style.left = scrollable.offsetWidth - scrollcover.offsetWidth;

  updateScrollButtons(scrollable); 
}


scrolltimer = undefined;
active_arrow = false;

function scrollstop(event) {
  debug('scrollstop(' + event + ')');
  if (scrolltimer) scrolltimer = clearInterval(scrolltimer);
  dojo.event.kwDisconnect({srcObj: document    , 
			      srcFunc: 'onmouseup' ,
			      targetFunc: 'scrollstop',
			      once: true});
  if (active_arrow) {
    dojo.event.kwDisconnect({srcObj: active_arrow, 
				srcFunc: 'onmouseout',
				targetFunc: 'scrollstop',
				once: true}); 
    active_arrow = false;
  }
}

function arrowmousedown(el, direction, event) {
  var y = direction == 'up' ? 0 : el.scrollHeight - el.clientHeight;
  debug('direction='+direction+', scrollTowards('+el+', '+y+', '+scrollstep+', true);');
  if (scrolltimer) scrolltimer = clearInterval(scrolltimer);
  scrollTowards(el, y, scrollstep);
  scrolltimer = setInterval(function() { 
			      scrollTowards(el, y, scrollstep); 
			      // scroll follows mouse when reaches pointer
			    }, scrolldelay);
  dojo.event.kwConnect({srcObj: document    , 
			   srcFunc: 'onmouseup' , 
			   targetFunc: 'scrollstop', 
			   once: true});
  active_arrow = event.target;
  dojo.event.kwConnect({srcObj: active_arrow,
			   srcFunc: 'onmouseout',
			   targetFunc: 'scrollstop',
			   once: true});
  event.stopPropagation();
  event.preventDefault();
}


function scrollTo(el, y) {
  debug('scrollTo'+el+', '+y);
  y = Math.min(el.scrollHeight - el.clientHeight, Math.max(0, y));
  var step = Math.abs(y - el.scrollTop) / 10;
  if (scrolltimer) scrolltimer = clearInterval(scrolltimer);
  scrolltimer = setInterval(function() { scrollTowards(el, y, step) || scrollstop(false); }, 
			    scrolldelay);
}

function scrollTowards(scrollable, y, step) {
  // debug('scrollTowards');
  var delta    = y - scrollable.scrollTop;
  if (delta != 0) {
    var absdelta = Math.abs(delta);
    scrollable.scrollTop += delta / absdelta * Math.min(step, absdelta);
    updateScrollButtons(scrollable); 
  }
  return delta;
}

function updateScrollButtons(el) {
  // Hide or show scroll buttons (ids `scrollup` and `scrolldown`)
  // based on the vertical scroll position of the given element.
  // Called whenever a scroll button is clicked, collapsed content is
  // toggled (see collapse.js) or a stylesheet is switched (see
  // stylesheet.js).

  /* var scrollHeight = el.scrollHeight - el.clientHeight;
  var top          = el.scrollTop;
  var scrollbar    =        el['scrollbar'];
  var   uparrow    = scrollbar[  'uparrow'];
  var downarrow    = scrollbar['downarrow'];
  var pos          = Math.min(top / scrollHeight, 1.0); */ }

function onChangeStyleSheet() {
  updateScrollButtons(document.getElementById('content'));
}

// alert('scroll.js done');

