/*
 * elevator.js Ver.0.3
 * 2008.3.3 @Kaitoo
 * 
 */

new function (){
	

	/*
	 *イベント追加用
	  -------------------------------------------------*/
	function addEvent(elm,listener,fn){
		try{ // IE
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent(
				"on"+listener
				,function(){
					fn.apply(elm,arguments)					
				}
			);
		}
	}


	function SmoothScroll(a){

		// イベントが発生したオブジェクトを取得
		if (window.event) {
		  target = window.event.srcElement;
		} else if (a) {
		  target = a.target;
		} else return;


		// アンカーをIDにセットしている目的地のオブジェクトを格納
		destinationLink = document.getElementById(a.rel.replace(/.*\#/,""));
		
		if(destinationLink)
		{

			var destx = parseInt(destinationLink.offsetLeft); 
			var desty = parseInt(destinationLink.offsetTop);
			var thisNode = destinationLink;
			while (thisNode.offsetParent && 
				  (thisNode.offsetParent != document.body)) {
			  thisNode = thisNode.offsetParent;
			  destx += thisNode.offsetLeft;
			  desty += thisNode.offsetTop;
			}		
			
			var flag = null;
			
			// 一番下 
			var max = window.innerHeight ? 
			  window.height - window.innerHeight :
			  document.body.scrollHeight - 
				(document.documentElement.clientHeight ? 
				  document.documentElement.clientHeight : document.body.clientHeight);


			if(desty < getCurrentYPos())
			{		
				/******  UPの場合の設定  ******/
				flag = "up";
				midpoint = desty + ((getCurrentYPos() - desty) / 2);
				var next_up_point = (getCurrentYPos() - desty)/30; // 初速度
			}
			else if(desty > getCurrentYPos())
			{							
				/******  DOWN の場合の設定  ******/	
				
				if(desty > max){
					desty = max;
				}
				
				flag = "down";
				var midpoint = parseInt((desty-getCurrentYPos()) / 2);
				var next_down_point = ( getCurrentYPos() + ((desty - getCurrentYPos())/ 30) ) ;		
			}





			/** 共通設定(変更しちゃダメよ) **/		
			var now_position = null; /* 現在地(初期値NULL) */
			var pass_flag = false;   /* 中間点通過フラグ */
			var i=0;
	
			interval = setInterval ( 
						
				function()
				{
					if(flag=='down'){ /******  DOWN  ******/
														
							
						// 現在の位置が設定されていなかった場合、現在地を取得
						if(!now_position){ 	now_position = getCurrentYPos(); }								
						
						// 中間点通過した場合
						if(pass_flag == true ){
							
							// 通常に進んだ場合 
							tmp_now_position = now_position + next_down_point; 
							
							// 目的地と次の地点の差 
							diff = desty - getCurrentYPos();
						
							if(diff <= 10)
							{									
								now_position = now_position + 1 ;
							}
							else
							{										
								//now_position = ( getCurrentYPos() + ((desty - getCurrentYPos())/ 5) ) ;
								
								diff = desty - getCurrentYPos(); // 残り距離
								now_position = now_position + (diff * 0.2);
							}
							
							if(i > 50) {
								/* 50回以上処理を繰返したら終了 */
								now_position = desty;
								clearInterval(interval);
							}
							else if(now_position >= desty){
								/* 設定した値が目標を超える場合、終了 */									
								now_position = desty;
								clearInterval(interval);
							}
							
							if(now_position > tmp_now_position)
							{
								now_position = tmp_now_position;
							}
							
						}else if(desty == getCurrentYPos()){
							/* 現在の位置が目標だった場合、終了 */
							now_position = desty;
							clearInterval(interval);
						
						}else if(desty < now_position + next_down_point){		
							/* 次の位置が目標を超える場合、終了 */
							pass_flag = true;
						
						}else if(midpoint < now_position + next_down_point){
							// 次の位置が中間点を越す場合、中間点に移動
							now_position = midpoint; 
							// 中間点通過フラグをTRUEに変更
							pass_flag = true;
						
						}else{	
							/* 通常の場合、一定の距離でDOWN */
							now_position = now_position + next_down_point; 
						}
						
						if(now_position >= midpoint) {
							/* 中間点を越す場合、中間点通過フラグをTRUEに変更 */
							pass_flag = true;
						}
						
					}
					else /******  UP  ******/
					{
						// 現在の位置が設定されていなかった場合、初速度(UP)をプラス
						if( !now_position ) { now_position = getCurrentYPos() - next_up_point; }
						
						/* 中間点通過した場合 */
						if(pass_flag == true ){
								
								// 通常に進んだ場合 
								tmp_now_position = now_position - next_up_point;
								
								/* 注意：ここら辺の式は結構適当です。 */
								
								diff = now_position - desty; // 残り距離
								
								if(diff < 10){	
									now_position = getCurrentYPos() - 1;
								}else{ 
									diff = now_position - desty; // 残り距離
									now_position = now_position - (diff * 0.1);
								}
								
								now_position = now_position - (diff * 0.1);
								
								if(now_position < tmp_now_position)
								{
									now_position = tmp_now_position;
								}
								
								
								if(i > 50) {
									/* 50回以上処理を繰返したら終了 */
									now_position = desty;
									clearInterval(interval);
								}
								else if(now_position <= desty){											
									now_position = desty;
									clearInterval(interval);
								}
						
						}else if(desty == getCurrentYPos()){
								/* 現在の位置が目標だった場合、終了 */
								now_position = desty;
								clearInterval(interval);
						
						}else if(midpoint > (now_position - next_up_point)){
								/* 次の位置が中間点を越す場合、中間点に移動 */ 
								now_position = midpoint;
								/* 中間点通過フラグをTRUEに変更 */ 
								pass_flag = true;
								
						}else{
								/* 通常の場合、一定の距離でUP */
								now_position = now_position - next_up_point;
						}
	
						if(now_position <= midpoint) {
								pass_flag = true;
						}						
					}
					window.scrollTo(parseInt(destx), parseInt(now_position));
					i++;
				}
				,10);
			
			pass_flag = false;
		}
	}

	function getCurrentYPos() {
		if (document.body && document.body.scrollTop)
		  return document.body.scrollTop;
		if (document.documentElement && document.documentElement.scrollTop)
		  return document.documentElement.scrollTop;
		if (window.pageYOffset)
		  return window.pageYOffset;
		return 0;
	}



	addEvent(window,"load",function(){
		// Aタグ全取得
		var a_tag = document.getElementsByTagName('a');

		for (var i=0;i<a_tag.length;i++) {
			var lnk = a_tag[i];
			
			// -------- 条件 ---------
			// 1. Aタグのリンクがある
			// 2. Aタグのリンクの先頭が「#」で始まる
			// 3. Aタグのリンクが現在のウィンドウのパスと同じ 
			// 4. 条件3、もしくはスラッシュ以下のパスと同じ
			// 5. URL以下の「?」パラメータ以降が同じ
			// 6. Aタグのリンクが「#」以降に文字が続く(英数文字列) 
			
			if ((lnk.href && lnk.href.indexOf('#') != -1) && 
			   ((lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname)) && 
			   (lnk.search == location.search) && 
			   (lnk.href.match(/.*\#\w/) )){
					lnk.rel = lnk.href;                            // Aタグ「rel」属性変更 
					lnk.href = "javascript:void(0)";               // Aタグ「href」属性変更
					lnk.onclick=function(){SmoothScroll(this)}; // Aタグ「onClick」属性変更
			}
		}
	});

}




	function whereMe(){
		alert(getCurrentYPosZ());
	}

	function getCurrentYPosZ() {
		if (document.body && document.body.scrollTop)
		  return document.body.scrollTop;
		if (document.documentElement && document.documentElement.scrollTop)
		  return document.documentElement.scrollTop;
		if (window.pageYOffset)
		  return window.pageYOffset;
		return 0;
	}


