//
// ============================
//  file name : scroll.js
// ============================
//

/////////////////////////////////////////////////////////////////////////////////////////

// マウス横スクロール
function horizontalScroll(event){
	if (!event){ var event = window.event; }
	
	var delta;
	
	if (event.wheelDelta){
		/* for IE,Opera,Safari,Chrome */
		delta = -event.wheelDelta/24;
	} else if (event.detail){
		/* for Mozilla */
		delta = event.detail;
	}

	if (delta <= 0){  
		window.scrollBy(80,0);
	} else {
		window.scrollBy(-80,0); 
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

function startPageScroll(){	
	var easing = 0.08;
	var interval = 30;
	
	var targetElement = document.getElementById("first");
					
	// ターゲット要素のx座標を取得
	var targetX = 0;
	while(targetElement){
	   targetX += targetElement.offsetLeft;
	   targetElement = targetElement.offsetParent;
	}
	
	// windowWidth 取得
	if(window.innerWidth){
		var box = document.createElement('div');
		with(box.style){
			position = 'absolute';
			top = '0';
			left = '0';
			width = '100%';
			height = '100%';
			margin = '0';
			padding = '0';
			border = 'none';
			visibility = 'hidden';
		}
		document.body.appendChild(box);
		var windowWidth = box.offsetWidth;
		document.body.removeChild(box);
	}else{
		var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
	}
	
	var bodyWidth = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth);
	var deltaWidth = bodyWidth - targetX + windowWidth;
	var adjustX = deltaWidth - (bodyWidth - targetX);									
	var scrollLeft = document.body.parentNode.scrollLeft || window.pageXOffset || document.body.scrollLeft;
	var toX = Math.max(targetX - scrollLeft - windowWidth, targetX - scrollLeft - windowWidth + (windowWidth - targetX));
	
	var moveX,myTimer;
	function windowScroll(){
		moveX = Math.floor(toX*easing);	
		
		window.scrollBy(moveX,0);
		toX -= moveX;
		myTimer = setTimeout(windowScroll,interval);
		if(moveX == 0){ clearTimeout(myTimer); };
	}								
	windowScroll();	
	return false;
}

/////////////////////////////////////////////////////////////////////////////////////////

// smartScroll ver.2.2
function smartScroll(){	
	
	//初期設定
	var easing = 0.18;
	var interval = 30;
	var allLinks = document.links;
	
	for(var i=0;i<allLinks.length;i++){
		var lnk = allLinks[i];
		
		if((lnk.href && lnk.href.indexOf('#') != -1) && ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname))){
			var myHash = lnk.hash.replace(/#/g,"");
			if(!(myHash.length == 0)){
				lnk.onclick = function(){
					// ターゲットのid名を取得
					var hash = this.hash;
					var targetId = hash.replace(/#/g,"");
				
					if (!document.getElementById(targetId)){return;};
					
					var element = document.getElementById(targetId);
					
					// ターゲット要素のx座標を取得
					var targetX = 0;
					while(element){
					   targetX += element.offsetLeft;
					   element = element.offsetParent;
					}
					
					// ターゲット要素のy座標を取得
					var targetY = 0;
					while(element){
					   targetY += element.offsetTop;
					   element = element.offsetParent;
					}					
					
					// 現在の表示位置のx座標を取得
					var scrollLeft = document.body.parentNode.scrollLeft || window.pageXOffset || document.body.scrollLeft;					
					
					// 現在の表示位置のy座標を取得
					var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
					
					// windowWidth 取得
					if(window.innerWidth){
						var box = document.createElement('div');
						with(box.style){
							position = 'absolute';
							top = '0';
							left = '0';
							width = '100%';
							height = '100%';
							margin = '0';
							padding = '0';
							border = 'none';
							visibility = 'hidden';
						}
						document.body.appendChild(box);
						var windowWidth = box.offsetWidth;
						document.body.removeChild(box);
					}else{
						var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
					}
					
					// 横スクール量を調整
					var bodyWidth = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth); //bodyの横幅
					var deltaWidth = bodyWidth - targetX + windowWidth;
					var adjustX = deltaWidth - (bodyWidth - targetX);									
					
					// 横移動距離を計算					
					var toX = Math.max(targetX - scrollLeft - windowWidth, targetX - scrollLeft - windowWidth + (windowWidth - targetX));					
				
					// 縦スクール量の調整			
					var windowHeight = window.innerHeight || document.documentElement.clientHeight; //ウィンドウの高さ	
					var bodyHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
					var deltaHeight = bodyHeight - targetY;
					var adjustY = windowHeight - deltaHeight;
					
					// 縦移動量の計算
					if(windowHeight > deltaHeight){				
						var toY = targetY - scrollTop - adjustY;
					} else	{
						var toY = targetY - scrollTop;
					}
					
					var moveX,moveY,myTimer;
					
					function windowScroll(){
						moveX = Math.floor(toX*easing);	
						moveY = Math.floor(toY*easing);	
						
						window.scrollBy(moveX,moveY);
						toX -= moveX;
						toY -= moveY;
						myTimer = setTimeout(windowScroll,interval);
						if(moveY == 0 && moveX == 0){ clearTimeout(myTimer); };
					}								
					windowScroll();	
					return false;					
				};
			}
		}
	}	
}

/////////////////////////////////////////////////////////////////////////////////////////

// ページの読み込みが完了後に実行
$(window).load(function(){
	window.scrollTo(0,0);
	setTimeout(startPageScroll,1200);
});

/////////////////////////////////////////////////////////////////////////////////////////

function init(){	
	// マウス横スクロール
	if (window.addEventListener) window.addEventListener('DOMMouseScroll', horizontalScroll, false);
	window.onmousewheel = document.onmousewheel = horizontalScroll;
	
	smartScroll();
}

// addLoadEvent
function addLoadEvent(func){
	if(typeof window.addEventListener == 'function'){
		window.addEventListener('load', func, false);
		return true;
	} else if (typeof window.attachEvent == 'object'){
		window.attachEvent('onload', func);
		return true;
	}
	
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

addLoadEvent(init);