
/*
 * iBLOG 2005
 *
 * Scroller javascript helper functions
 *
 * This script is (c) 2004-2005 Chubzz Technologies. Any reproducing or using is allowed as long as these credentials
 * are mentioned somewhere in the script.
 *
 * The usage is very simple: make iframe with no scrollbars
 * and add onload tag to the body element: <body onload="scroll_initialize(direction,ratio);">
 * where direction is either horizontal (true) or vertical (false) and ratio is the smoothing
 * ratio.
 *
 */


var scroll_page_size = 0;
var scroll_wnd_size = 0;
var scroll_timer = 0;
var scroll_horiz = false;
var scroll_ratio = 200;

// initialize horizontal scrolling
function scroll_initH() {
  if (document.getElementById) {
    obj = document.getElementById('jsScrollId');
    
    if (obj&&obj.offsetWidth)
      scroll_page_size = obj.offsetWidth;
    else if (document.body.scrollWidth>document.body.offsetWidth)
      scroll_page_size = document.body.scrollWidth;
    else
      scroll_page_size = document.body.offsetWidth;

    if (self.innerWidth)
      scroll_wnd_size = self.innerWidth;
    else if (document.documentElement&&document.documentElement.clientWidth)
      scroll_wnd_size = document.documentElement.clientWidth;
    else if (document.body)
      scroll_wnd_size = document.body.clientWidth;

    if (scroll_page_size>0) {
      if (document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove = scroll_mouseoverH;
      document.onmouseout = scroll_mouseout;
    }
  }
}

// initialize vertical scrolling
function scroll_initV() {
  if (document.getElementById) {
    obj = document.getElementById('jsScrollId');

    if (obj&&obj.offsetHeight)
      scroll_page_size = obj.offsetHeight;
    if (document.body.scrollHeight>document.body.offsetHeight)
      scroll_page_size = document.body.scrollHeight;
    else
      scroll_page_size = document.body.offsetHeight;

    if (self.innerHeight)
      scroll_wnd_size = self.innerHeight;
    else if (document.documentElement&&document.documentElement.clientHeight)
      scroll_wnd_size = document.documentElement.clientHeight;
    else if (document.body)
      scroll_wnd_size = document.body.clientHeight;

    if (scroll_page_size>0) {
      if (document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
      document.onmousemove = scroll_mouseoverV;
      document.onmouseout = scroll_mouseout;
    }
  }
}

// loaded upon body.onload
// initializes the event capturing
function scroll_initialize(direction, ratio) {

  scroll_ratio = ratio;

  if (direction==true) scroll_horiz = true;
  else scroll_horiz = false;
  
  if (scroll_horiz) scroll_initH();
  else scroll_initV();
  
}

// horizontal scroll
function scroll_mouseoverH(e) {

  scroll_mouseout(0);

  if (!document.all) X = e.pageX;
  else X = event.clientX + document.body.scrollLeft;

  if (X<0) X = 0;

  scroll_adjust(X, false);

  return true;
}

// vertical scroll
function scroll_mouseoverV(e) {

  scroll_mouseout(0);

  if (!document.all) Y = e.pageY;
  else Y = event.clientY + document.body.scrollTop;

  if (Y<0) Y = 0;

  scroll_adjust(Y, false);

  return true;
}

// stops scrolling
function scroll_mouseout(e) {
  window.clearTimeout(scroll_timer);
  scroll_timer = 0;
}

// this function will adjust the scroll area based on the Y position
// of the mouse
var saved_offset = 0;
var saved_dist = 0;
function scroll_adjust(pos, time_out) {

  scroll_mouseout(0);

  offset = scroll_offset();

  // get the scroll_top position
  scroll_pos = ((scroll_page_size - scroll_wnd_size + scroll_ratio) * ((pos - offset) / scroll_wnd_size));

  // scroll to scroll_pos with smoothing
  if (!time_out) {
    saved_offset = scroll_pos - (scroll_ratio / 2);
    saved_dist = (scroll_pos - offset - (scroll_ratio / 2));
  }

  if (saved_dist<0&&offset-2<saved_offset) return;
  if (saved_dist>0&&offset+2>saved_offset) return;

  dist = (scroll_pos - offset - (scroll_ratio / 2)) / 8;

  if (dist<-6) dist = -6;
  else if (dist>6) dist = 6;

  //dist = Math.round(dist);
  if (saved_dist<0&&dist>-2) dist = -2;
  else if (saved_dist>0&&dist<2) dist = 2;
  
  if (scroll_horiz) window.scrollBy(dist, 0);
  else window.scrollBy(0, dist);

  scroll_timer = window.setTimeout('scroll_adjust('+pos+', true)', 1);

}

// gets the Y scroll offset
function scroll_offset() {

  if (scroll_horiz) {
    if (self.pageXOffset) return self.pageXOffset;
    else if (document.documentElement&&document.documentElement.scrollLeft) return document.documentElement.scrollLeft;
    else if (document.body) return document.body.scrollLeft;
  } else {
    if (self.pageYOffset) return self.pageYOffset;
    else if (document.documentElement&&document.documentElement.scrollTop) return document.documentElement.scrollTop;
    else if (document.body) return document.body.scrollTop;
  }

  // should not happen
  return 0;
}
