/* IE7でも動作するもの*/

function backToTop() {
  var x1 = x2 = x3 = 0;
  var y1 = y2 = y3 = 0;
  if (document.documentElement) {
      x1 = document.documentElement.scrollLeft || 0;
      y1 = document.documentElement.scrollTop || 0;
  }
  if (document.body) {
      x2 = document.body.scrollLeft || 0;
      y2 = document.body.scrollTop || 0;
  }
  x3 = window.scrollX || 0;
  y3 = window.scrollY || 0;
  var x = Math.max(x1, Math.max(x2, x3));
  var y = Math.max(y1, Math.max(y2, y3));
  window.scrollTo(Math.floor(x / 1.2), Math.floor(y / 1.2));
  if (x > 0 || y > 0) {
      window.setTimeout("backToTop()", 25);
  }
}


/* 使い方：

	easeScroll( 目的x,  目的y, [スピード], [スターティングx], [スターティングy] )
	
	* [] = optional

*/


// find the current X position
function getCurrX()
{
	if (document.all)
	{
		return document.body.scrollLeft;
	}
	else if (window.pageXOffset)
	{
		return window.pageXOffset;
	}
	else
	{
		return 0;
	}
}

// Find the current Y position
function getCurrY()
{ 
	if (document.all)
	{
		return document.body.scrollTop;
	}
	else if (window.pageYOffset)
	{
		return window.pageYOffset;
	}
	else
	{
		return 0;
	}
}



function easeScroll(destX, destY, spd, currX, currY)
{
	// stop any active scrolling
	if (timeOutRef)
	{
		clearTimeout(timeOutRef);
	}
	
	// If speed not given default to 6
	if (!spd) spd = 6;
	
	// default destination to top of page
	if (!destX || destX < 0) destX = 0;
	if (!destY || destY < 0) destY = 0;
	
	// Use current X,Y if coords not given
	if (!currX) currX = getCurrX();
	if (!currY) currY = getCurrY();
	
	currX += ( destX - getCurrX() ) / spd;	
	if (currX < 0) currX = 0;
	currY += ( destY - getCurrY() ) / spd;
	if (currY < 0) currY = 0;
	
	// round out to integers
	var gotoX = Math.round(currX);
	var gotoY = Math.round(currY);
	
	window.scrollTo(gotoX, gotoY);
	
	// Recurse this function until we reach destination
	if (gotoX != destX || gotoY != destY)
	{
		var timeOutRef = setTimeout( "easeScroll(" + destX + "," + destY + "," + spd + "," + currX + "," + currY + ")", 20 );
	}
	
}