function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function'){
    window.onload = func;
  } else {
    window.onload = function(){
      oldonload();
      func();
    }
  }
}

function getAbsolutePos(el) {
  var SL = 0, ST = 0;
  var is_div = /^div$/i.test(el.tagName);
  if (is_div && el.scrollLeft)
    SL = el.scrollLeft;
  if (is_div && el.scrollTop)
    ST = el.scrollTop;
  var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
  if (el.offsetParent) {
    var tmp = this.getAbsolutePos(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

function scroll_div_x() {
  newLeft = Scroll_Element.scrollLeft+DX*Direction;
  Scroll_Element.scrollLeft = newLeft;
//  document.getElementById('info').innerHTML = Scroll_Element.scrollLeft;
  scroll_i_x = setTimeout('scroll_div_x()', DTime);
}

function scroll_div_y() {
  newTop = Scroll_Element.scrollTop+DY*Direction;
  Scroll_Element.scrollTop = Scroll_Element.scrollTop + Direction*DY;
//  document.getElementById('info').innerHTML = Scroll_Element.scrollTop + ' ' + Direction*DY;
  scroll_i_y = setTimeout('scroll_div_y()', DTime);
}

var DX = 4, DY = 2, Direction = 0, DTime=100, Scroll_V, Scroll_Element, scroll_i_x=false, scroll_i_y=false;
function scroll_init() {
//  preloadImages('img/download_hover.gif');
//  preloadImages('img/play_hover.gif');
  var alldivs = document.getElementsByTagName('div');
  for (i=0; i<alldivs.length; i++) {
    if (alldivs[i].className && (alldivs[i].className=='scroll' || (alldivs[i].className.substring(0,5)=='scroll'))) {
      alldivs[i].style.position = 'relative';
      alldivs[i].style.left = '0px';
      alldivs[i].onclick = function(e) {
          Direction = 0;
      }
      alldivs[i].onmousemove = function(e) {
        Scroll_Element = this;
        if (!e)
          e = window.event;
        x = e.clientX-getAbsolutePos(this.parentNode).x;
        Scroll_V = (x * 100 / this.parentNode.offsetWidth).toFixed();
//        document.getElementById('info').innerHTML = ''+x+' '+Scroll_V;
        if ((70 < Scroll_V)) {
          Direction = 1;
        } else if (Scroll_V < 30) {
          Direction = -1;
        } else {
          Direction = 0;
        }
        if (!scroll_i_x)
          scroll_div_x();
      }
    }
    if (alldivs[i].className && (alldivs[i].className=='scrollY' || (alldivs[i].className.substring(0,5)=='scrollY'))) {
      alldivs[i].onmousemove = function(e) {
        Scroll_Element = this;
        if (!e)
          e = window.event;
        y = e.clientY-getAbsolutePos(this.parentNode).y;
        Scroll_V = (y * 100 / this.parentNode.offsetHeight).toFixed();
//        document.getElementById('info').innerHTML = ''+y+' '+Scroll_V;
        if ((70 < Scroll_V)) {
          Direction = 1;
        } else if (Scroll_V < 30) {
          Direction = -1;
        } else {
          Direction = 0;
        }
        if (!scroll_i_y)
          scroll_div_y();
      }
    }
  }
}

addLoadEvent(scroll_init);

