/* ========================================================================
 * @license	The MIT License
 * 		http://www.opensource.org/licenses/mit-license.php
 * @author	Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
 * @copyright	Neo,Inc <http://neo-navi.net/>
 * @version    ,v 0.9.0
 * ======================================================================== 
 */
 var scrollTimer;
 var scrollAbsFlag = false;
 var agt = navigator.userAgent.toLowerCase();
 var is_ie　 = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
 var is_gecko = (agt.indexOf('gecko') != -1);
 var is_opera = (agt.indexOf("opera") != -1);
 
 function Scroll(targetId)
 {
 	this.param = {
		speed : (arguments[1]) ? arguments[1].speed : 20,
		offset : (arguments[1]) ? arguments[1].offset : 30
	};
	this.targetElm = document.getElementById(targetId);
 	this.pagewidth = document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth;
	this.pageheight = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
	this.bodyheight = document.body.offsetHeight;
	try{
		clearInterval(scrollTimer);
	}
	catch(e){}
	this.scrollDo(this.targetElm,this.param);
 }
 Scroll.prototype.scrollDo = function(elm, param)
	{
		var targetPosition = new GetPos(elm);
		var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
		if (targetPosition.y < scrollTop) {
			var scrollValue = scrollTop;
			scrollAbsFlag = true;
		}
		else{
			var scrollValue = scrollTop;
			scrollAbsFlag = false;
		}
		var height = this.bodyheight;
		var pageheight = this.pageheight;
		if(!scrollAbsFlag)
		{
			window.scrollTo(0,scrollTop);
		}
		scrollTimer = setInterval(function(){
			scrollValue = scrollValue + (targetPosition.y - scrollValue)/ param.speed;
			window.scrollTo(0,scrollValue);
			var new_scrollTop = document.body.scrollTop  || document.documentElement.scrollTop;
			if (scrollAbsFlag) {
				if (new_scrollTop < 1) {
					window.scrollTo(0, 0);
					clearInterval(scrollTimer);
				}
			}
			else {
				if (new_scrollTop > Math.abs(height - pageheight) - 1 || new_scrollTop > targetPosition.y - param.offset) {
					clearInterval(scrollTimer);
				}
			}
		},5);
	}
function GetPos(elm)
{
	this.targetElm = elm;
	this.position = new function(){ this.x = 0; this.y = 0;	}
	while( this.targetElm )
	{
		this.position.x += this.targetElm.offsetLeft;
		this.position.y += this.targetElm.offsetTop;
		this.targetElm = this.targetElm.offsetParent;
		if( (this.targetElm) && (is_ie) )
		{
			this.position.x += (parseInt(this.getStyle(this.targetElm, 'borderLeftWidth', 'border-left-width') || 0));
			this.position.y += (parseInt(this.getStyle(this.targetElm, 'borderTopWidth', 'border-top-width')) || 0);
		} 
	}
	if (is_gecko)
	{
		var body = document.getElementsByTagName('body')[0];
		this.position.x += 2 * (parseInt(this.getStyle(body, 'borderLeftWidth', 'border-left-width')) || 0);
		this.position.y += 2 * (parseInt(this.getStyle(body, 'borderTopWidth', 'border-top-width')) || 0);
	}
	return this.position;
}

GetPos.prototype.getStyle = function(targetElm, property_IE, property_CSS)
{
	var elm = targetElm;
	if(elm.currentStyle)
	{
		return elm.currentStyle[property_IE];
	}
	else if( window.getComputedStyle )
	{
		var nowStyle = window.getComputedStyle(elm, '');
		return nowStyle.getPropertyValue(property_CSS);
	}
}
