var ss_STEPS, ss_DELTA;

function smoothScrollInpageLinks( steps, delta )
{
  ss_STEPS = steps || ss_STEPS || 150;
  ss_DELTA = delta || ss_DELTA || 10;
  for( var i=0; i<document.links.length; i++ )
  {
    var link = document.links[i];
    if( link.hash && isSameDocument( link ) )
      addEvent( link, 'click', smoothScroll );
  }
}

function makeScrollTo( x, y )
{
  return function(){ window.scrollTo( x, y ); };
}

function makeGoto( anchor )
{
  return function() { location.hash = anchor; };
}

function smoothScroll( e )
{
  var target = window.event ? window.event.srcElement : e && e.target;
  if( !target ) return true;
  var anchor = target.hash.substr(1);
  if( !(target = document.getElementById( anchor ) ||
         document.anchors.namedItem( anchor )) ) return true;
  var src = getScreenCoordinates();
  var dst = getScreenCoordinates( target );
  if( src.y == dst.y ) return true;

  for( var i=1; i<ss_STEPS; i++ )
  {
    var percent = i / ss_STEPS;
    var smooth = (1-Math.cos( Math.PI * percent )) / 2;
    var x = parseInt( src.x + (     0-src.x) * smooth );
    var y = parseInt( src.y + ( dst.y-src.y) * smooth );
    setTimeout( makeScrollTo( x, y ), ss_DELTA * i );
  }
  setTimeout( makeGoto( anchor ), ss_DELTA * ss_STEPS );


  if( window.event )
  {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if( e && e.preventDefault && e.stopPropagation )
  {
    e.preventDefault();
    e.stopPropagation();
  }
  return true;
}

function isSameDocument( a, b )
{
  return true;
}

function addEvent( node, eventType, callback, useCapture )
{
  if( node.addEventListener )
    return !node.addEventListener( eventType, callback, useCapture ) || true;
  if( node.attachEvent )
    return node.attachEvent( 'on'+ eventType, callback );
}

function getScreenCoordinates( node )
{
  var x, y;
  if( !node )
  {
    y = (document.body && document.body.scrollTop) || // ie5(|.5)
      (document.documentElement && document.documentElement.scrollTop) ||// ie6
      window.pageYOffset || 0; // others
    x = (document.body && document.body.scrollLeft) ||
      (document.documentElement && document.documentElement.scrollLeft) ||
      window.pageXOffset || 0;
  }
  else
  {
    x = node.offsetLeft;
    y = node.offsetTop;
    while( (node = node.offsetParent) && (node != document.body) )
    {
      x += node.offsetLeft;
      y += node.offsetTop;
    }
  }
  return { x:x, y:y };
}