/*--------------------------------------------------------------------------
 *  Smooth Scroller Script, version 1.0.1
 *  (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
 *
 *  For details, please check the website : http://dezinerfolio.com/
 *
 *  Edited by Max Shawabkeh:
 *    Converted to 2D
 *    Made the script center on the target
 *    Removed automatic activation
 *
/*--------------------------------------------------------------------------*/


Scroller = {
  // control the speed of the scroller.
  speed: 20,

  // returns the X position of a div
  gx: function(d) {
    gx = d.offsetLeft;
    if (d.offsetParent) {
      dd = d;
      while (dd = dd.offsetParent) {
        gx += dd.offsetLeft;
      }
    }
    
    if (document.documentElement) {
      gx -= Math.floor(document.documentElement.clientWidth/2);
      gx += Math.floor(d.offsetWidth/2);
    } else {
      gx -= Math.floor(document.body.clientWidth/2);
      gx += Math.floor(d.offsetWidth/2);
    }
    
    return gx;
  },
  
  // returns the Y position of a div
  gy: function(d) {
    gy = d.offsetTop;
    if (d.offsetParent) {
      dd = d;
      while (dd = dd.offsetParent) {
        gx += dd.offsetTop;
      }
    }

    if (document.documentElement && document.documentElement.clientHeight) {
      gy -= Math.floor(document.documentElement.clientHeight/2);
      gy += Math.floor(d.offsetHeight/2);
    } else {
      gy -= Math.floor(document.body.clientHeight/2);
      gy += Math.floor(d.offsetHeight/2);
    }
      
    return gy;
  },
  
  // returns the current [top] scroll position
  scrollTop: function() {
    body = document.body;
    d = document.documentElement;
    if (body && body.scrollTop)
      return body.scrollTop;
    if (d && d.scrollTop)
      return d.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },
  
  // returns the current [left] scroll position
  scrollLeft: function() {
    body = document.body;
    d = document.documentElement;
    if (body && body.scrollLeft)
      return body.scrollLeft;
    if (d && d.scrollLeft)
      return d.scrollLeft;
    if (window.pageXOffset)
      return window.pageXOffset;
    return 0;
  },
  
  // move the scroll bar to the particular div.
  scroll: function(d2, d) {
    i = window.innerHeight || document.documentElement.clientHeight;
    i2 = window.innerWidth || document.documentElement.clientWidth;
    h = document.body.scrollHeight;
    h2 = document.body.scrollWidth;
    a = Scroller.scrollTop();
    a2 = Scroller.scrollLeft();
    
    if (d > a) {
      if (h - d > i) {
        a += Math.ceil((d - a) / Scroller.speed);
      } else {
        a += Math.ceil((d - a - (h - d)) / Scroller.speed);
      }
    } else {
      a = a + (d - a) / Scroller.speed;
    }
    
    if (d2 > a2) {
      if (h2 - d2 > i) {
        a2 += Math.ceil((d2 - a2) / Scroller.speed);
      } else {
        a2 += Math.ceil((d2 - a2 - (h2 - d2)) / Scroller.speed);
      }
    } else {
      a2 = a2 + (d2 - a2) / Scroller.speed;
    }
    
    window.scrollTo(a2, a);
    
    if ((a == d && a2 == d2) || (Scroller.offsetTop == a && Scroller.offsetLeft == a2))
      clearInterval(Scroller.interval);

    Scroller.offsetTop = a;
    Scroller.offsetLeft = a2;
  },
  
  scrollTo: function(target) {
    to = document.getElementById(target);
    clearInterval(Scroller.interval);
    Scroller.interval = setInterval('Scroller.scroll(' + Scroller.gx(to) + ',' + Scroller.gy(to) + ')', 10);
  }
  
};