/*====================================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////

 Author : http://www.metaphase.co.jp/
 created: 2010/02/22
 update : 2010/06/17

//////////////////////////////////////////////////////////////////////////////////////////////////////
====================================================================================================*/


var slideScroll = {

	conf : {
		// 速度（割り算 0未満禁止）
		slideEasing : 8,
		// なめらかさ（関数起動間隔 ミリ秒 0未満禁止）
		slideSmooth : 1,
		
		// ここから下は編集不可
		ua_check : "",
		tmpFunctionId : ""
	},

	setHTML : function(){
		slideScroll.conf.ua_check = navigator.userAgent.toLowerCase();
		
		var i, anchorList = document.getElementsByTagName("a");
		
		for(i=0; i<anchorList.length; i++ ){
			if(anchorList[i].hash.charAt(0) == "#" ){
				anchorList[i].onclick = slideScroll.action;
			}
		}
	},

	action : function(){
		clearTimeout(slideScroll.conf.tmpFunctionId);
		slideScroll.scroll(this);
		return false;
	},

	scroll : function( thisObject, before_x, before_y ){
		
		if(typeof before_x == 'undefined' ){
			before_x = NaN;
		}
		if(typeof before_y == 'undefined' ){
			before_y = NaN;
		}
		
		var targetId = thisObject.hash.replace("#","");
		if( targetId == "" ){
			if( thisObject.href == "" ){
				return false;
			}
			if( thisObject.target != "" ) {
				window.open(thisObject.href, thisObject.target);
			} else {
				location.href = thisObject.href;
			}
			return true;
		}
		var targetObject = document.getElementById(targetId);

		var target_x = 0; 
		if(slideScroll.conf.ua_check.indexOf("msie") != -1){
			tmp_element = targetObject;
			do { 
			target_x += tmp_element.offsetLeft || 0; 
			tmp_element = tmp_element.offsetParent; 
			} while (tmp_element);
		} else {
			target_x = targetObject.offsetLeft;
		}
		
		var target_y = 0; 
		if(slideScroll.conf.ua_check.indexOf("msie") != -1){
			tmp_element = targetObject;
			do { 
			target_y += tmp_element.offsetTop || 0; 
			tmp_element = tmp_element.offsetParent; 
			} while (tmp_element);
		} else {
			target_y = targetObject.offsetTop;
		}
		
		if( target_x > document.body.clientWidth - (document.documentElement.clientWidth || window.innerWidth) + targetObject.offsetWidth ){
			target_x = document.body.clientWidth - (document.documentElement.clientWidth || window.innerWidth) + targetObject.offsetWidth;
		}
		if( target_y > document.body.clientHeight - (document.documentElement.clientHeight || window.innerHeight) + targetObject.offsetHeight ){
			target_y = document.body.clientHeight - (document.documentElement.clientHeight || window.innerHeight) + targetObject.offsetHeight;
		}

		
		
		if(document.all){
			var now_x = document.documentElement.scrollLeft || document.body.scrollLeft;
			var now_y = document.documentElement.scrollTop  || document.body.scrollTop;
		}else if(document.layers || document.getElementById){
			var now_x = pageXOffset;
			var now_y = pageYOffset;
		}
		
		var move_x = (target_x - now_x)/slideScroll.conf.slideEasing;
		if( move_x > 0 ){
			move_x = Math.ceil(move_x);
		} else {
			move_x = Math.floor(move_x);
		}
		
		var move_y = (target_y - now_y)/slideScroll.conf.slideEasing;
		if( move_y > 0 ){
			move_y = Math.ceil(move_y);
		} else {
			move_y = Math.floor(move_y);
		}
		
		
		window.scrollBy( move_x, move_y );
		
		if( (target_x != now_x && before_x != now_x ) || (target_y != now_y && before_y != now_y ) ){
			slideScroll.conf.tmpFunctionId = setTimeout(function(){slideScroll.scroll( thisObject, now_x, now_y )}, slideScroll.conf.slideSmooth);
		}
	},

	addEvent : function(){
		if(window.addEventListener) {
			window.addEventListener("load", this.setHTML, false);
		}
		else if(window.attachEvent) {
			window.attachEvent("onload", this.setHTML);
		}
	}
}


slideScroll.addEvent();




