
////////// SCROLLING
/////http://henrik.nyh.se/2006/12/12/scroll-window-when-dragging-scriptaculous-sortables-to-edges/
/////http://wiki.script.aculo.us/scriptaculous/discuss/Sortables#discuss_3347

var itemBeingDragged;
var scrollPoll;

function getWindowScroll() {
  var T, L, W, H;
  var w = window;
  with (w.document) {
    if (w.document.documentElement && documentElement.scrollTop) {
      T = documentElement.scrollTop;
      L = documentElement.scrollLeft;
    } else if (w.document.body) {
      T = body.scrollTop;
      L = body.scrollLeft;
    }
    if (w.innerWidth) {
      W = w.innerWidth;
      H = w.innerHeight;
    } else if (w.document.documentElement && documentElement.clientWidth) {
      W = documentElement.clientWidth;
      H = documentElement.clientHeight;
    } else {
      W = body.offsetWidth;
      H = body.offsetHeight
    }
  }
  return { top: T, left: L, width: W, height: H };
}

function findTopY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function findBottomY(obj) {
  return findTopY(obj) + obj.offsetHeight;
}

function scrollSomeA() {
  var scroller = getWindowScroll();
  var yTop = findTopY(itemBeingDragged);
  var yBottom = findBottomY(itemBeingDragged);

  if (yTop > scroller.top + scroller.height - 150)
    window.scrollTo(0,scroller.top + 250);
    else if (yTop < scroller.top + 100)
    window.scrollTo(0,scroller.top - 250);
}

function scrollSomeB() {
  var scroller = getWindowScroll();
  var yTop = findTopY(itemBeingDragged);
  var yBottom = findBottomY(itemBeingDragged);

  if (yTop > scroller.top + scroller.height - 75)
    window.scrollTo(0,scroller.top + 250);
    else if (yTop < scroller.top + 50)
    window.scrollTo(0,scroller.top - 250);
}


function scrollStart(e) {
  itemBeingDragged = e;
  scrollPollA = setInterval(scrollSomeA, 1000);
  scrollPollB = setInterval(scrollSomeB, 100);
}

function scrollEnd(e) {
  clearInterval(scrollPollA);
  clearInterval(scrollPollB);
}

