// JavaScript Document//
// inpagelink.js
//
// ページ内リンク移動
//

//
// 移動
//
function scroll_inpage_link(pos, tpos, spd, rdc) {
if (Math.abs(spd) < Math.abs(rdc)) spd += rdc ; 
pos += spd ;

  if ((rdc > 0 && pos >= tpos) || (rdc < 0 && pos <= tpos)) {
    document.documentElement.scrollTop = tpos ;
  }
  else {
    document.documentElement.scrollTop = pos ;
    setTimeout('scroll_inpage_link(' + pos + ',' + tpos + ',' + (spd-rdc) + ',' + rdc + ')', 10) ;
  }
}

//
// 移動指定
//
function move_inpage_link(target) {
  tid = target.hash.substr(1, target.hash.length-1) ;
  tobj = document.getElementById(tid) ;
  if (tobj.tagName == 'A') {
    ua = navigator.userAgent ;
    cheight = ua.indexOf('MSIE') >= 0 ? (
      ua.indexOf('MSIE 6') >= 0 ? document.documentElement.clientHeight : document.body.clientHeight
      ) : window.innerHeight ;
    tpos = document.documentElement.scrollHeight - cheight < tobj.offsetTop ? (document.documentElement.scrollHeight - cheight) : tobj.offsetTop ;

    cpos = document.documentElement.scrollTop ;
    rdc = (tpos - cpos) / 5050 ;
    spd = rdc * 100.0001 ;
    timerId = setTimeout('scroll_inpage_link(' + cpos + ',' + tpos + ',' + spd + ',' + rdc + ')', 0) ;
  }
}

//
// 初期化
//
function init_inpage_link() {
anchors = document.getElementsByTagName('a') ;
for (i = 0; i < anchors.length; ++i) {
if (anchors[i].href && anchors[i].href == (document.URL.replace(/#.*$/,'')+anchors[i].hash)) {
      anchors[i].onclick = function () {move_inpage_link(this) ; return false ; }
    }
  }
}


