/* 使い方：

	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 + ")", 0 );
	}
	
}