/**
 * @author sugimoto
 */

  //global value
 var scrollTimer = '';
 var scrollAbsFlag = false;
 // get User's Browser
 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)
 {
 	//constructor
 	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(targetId);
 }
 Scroll.prototype.scrollDo = function(targetId)
	{
		//var speed = 20;
		/*var is_pos = this.getPositionProperty(document.getElementById(targetId), 'position');
		alert(is_pos);
		if( is_pos == 'relative' || is_pos == 'absolute')
		{
			var targetPosition = new Object();
				targetPosition.y = this.getPositionValue(document.getElementById(targetId),'top');
				targetPosition.x = this.getPositionValue(document.getElementById(targetId),'left');
		}
		else
		{*/
		var targetPosition = new GetPos(document.getElementById(targetId));//document.getElementById(targetId).offsetTop;
		//}
		var scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
		if (targetPosition.y < scrollTop) {
			var scrollValue = scrollTop;
			scrollAbsFlag = true;
		}
		else{
			var scrollValue = 0;
			scrollAbsFlag = false;
		}
		
		scrollTimer = setInterval(function(){
			scrollValue = scrollValue + (targetPosition.y - scrollValue)/ 20;
			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 > (Scroll.bodyheight - Scroll.pageheight - 1) || new_scrollTop > targetPosition.y - 30) {
					clearInterval(scrollTimer);
				}
			}
		},5);
		
	}
	/*
Scroll.prototype.getPositionProperty = function(elm, p)
{
	var elm = elm;
	if(elm.currentStyle)
	{
		return elm.currentStyle[p];
	}
	else if( window.getComputedStyle )
	{
		var nowStyle = window.getComputedStyle(elm, '');
		return nowStyle.getPropertyValue(p);
	}	
}
Scroll.prototype.getPositionValue = function(elm, property)
{
	var elm = elm;
	if(elm.currentStyle)
	{
		return elm.currentStyle[property];
	}
	else if( window.getComputedStyle )
	{
		var nowStyle = window.getComputedStyle(elm, '');
		return nowStyle.getPropertyValue(property);
	}		
}
*/
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;
		// for ie_fix
		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);
		} 
	}
	// fix for Firefox
	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);
	}
}
/*	
}	
function getElementStyle(targetElm,IEStyleProp,CSSStyleProp) {
　var elem = targetElm;
　if (elem.currentStyle) {
　　return elem.currentStyle[IEStyleProp];
　} else if (window.getComputedStyle) {
　　var compStyle = window.getComputedStyle(elem,"");
　　return compStyle.getPropertyValue(CSSStyleProp);
　}
}//---End Function

function getPosition(that) {
　var targetEle = that;　　　//thatは位置を取得したい要素Object
　var pos = new function(){ this.x = 0; this.y = 0; }
　while( targetEle ){
　　pos.x += targetEle.offsetLeft; 
　　pos.y += targetEle.offsetTop; 
　　targetEle = targetEle.offsetParent;
　　　//IEの補正：上記計算で無視されてしまう各親要素のborder幅を加算
　　if ((targetEle) && (is_ie)) {
　　　pos.x += (parseInt(getElementStyle(targetEle,"borderLeftWidth","border-left-width")) || 0);
　　　pos.y += (parseInt(getElementStyle(targetEle,"borderTopWidth","border-top-width")) || 0);
　　}
　}
　　//geckoの補正：カウントしないbody部border幅をマイナスしてしまうので２倍して加算
　if (is_gecko) {
　　　//以下の部分でbody部を取得し、borderの減算を補正する。
　　var bd = document.getElementsByTagName("BODY")[0];　　//body部を取得
　　pos.x += 2*(parseInt(getElementStyle(bd,"borderLeftWidth","border-left-width")) || 0);
　　pos.y += 2*(parseInt(getElementStyle(bd,"borderTopWidth","border-top-width")) || 0);
　}
　return pos;
}//---End Function	
*/