// 名前空間を定義。
if (typeof NS == 'undefined' || !NS) {
  window.NS = {};
}

NS.util = function() {
  return {
    isMSIE : /*@cc_on!@*/false, 
    
    // ieとその他ブラウザの仕様差異を吸収する処理
    addListener : function (el, type, fn, flg) {
      if(!el) { return false; }
      if(el.addEventListener) {
        el.addEventListener(type, fn, flg);
      }
      else if(el.attachEvent) {
        el.attachEvent('on' + type, fn);
      }
      else {
        return false;
      }
    }
  }
}();

// スムーススクロール
NS.pageTop = function() {

  // アニメーション
  var scrollUp = function() {
    var position = getPosition();
    // アニメーション動作の微調整
    window.scrollTo(Math.max(Math.floor(position.x / 2), 0), Math.max(Math.floor(position.y - (position.y / 5)), 0));
    if (position.x > 0 || position.y > 0) {
      window.setTimeout(scrollUp, 30);
    }
  }
  var getPosition = function() {
    var position = {};
    position.x = document.body.scrollLeft || document.documentElement.scrollLeft;
    position.y = document.body.scrollTop  || document.documentElement.scrollTop;
    return position;
  }
  var getAnchors = function() {
    var anchors = document.getElementsByTagName('a');
    return anchors;
  }

  var main = function() {
    var anchors = getAnchors();
    for (var i = 0; i < anchors.length; i++) {
      if (anchors[i].className.match(/ *scroll */)) {
        NS.util.addListener(anchors[i], 'click', scrollUp, false);
        if (anchors[i].getAttribute('href')) {
          anchors[i].setAttribute('href', 'javascript:void(0);');
        }
      }
    }
  }();
}

// ロードイベントに追加
NS.util.addListener(window, 'load', NS.pageTop , false);


