﻿d = document;

/*
    Extender (tiny library)
    last modified February 19, 2008
*/

//Functions extオブジェクトなしで使えるファンクションたち
Array.prototype.each = function(fn){
    for(var i=0, el; el=this.nodes[i]; i++){ fn.call(el); };
    return this;
}
String.prototype.removeClass = function( target ){
  arr = this.split(/\s+/);
  for(i = 0; i < arr.length; i++){
    if(arr[i] == target){
      arr.splice(i,1);
    }
  }
  return arr.join(" ");
}
String.prototype.addClass = function(target){
  arr = this.split(/\s+/);
  for(i = 0; i < arr.length; i++){
    if(arr[i] != target){
      arr.push(target);
    }
  }
  return arr.join(" ");
}


//Extender extオブジェクト
var extender = window.extender = function(selector){
  return this instanceof extender ?
    this.init(selector) :
    new extender(selector);
};
window.ext = extender;

extender.fn = extender.prototype = {
  // initialize、ノードを見つける
  init: function(selector){
    this.nodes = [];
    if(typeof(selector)=="Array"){
      this.nodes.push(selector);
      return
    } else if(typeof(selector)!="string"){
      this.nodes.push(selector);
      return
    } else if( selector.match(/^#(\S+)/) ){
      this.nodes = [ d.getElementById(RegExp.$1) ];
      return this;
    } else if( selector.match(/^\.(\S+)/) ){
      if(d.getElementsByClassName){
        this.nodes = d.getElementsByClassName(RegExp.$1);
        return;
      }
      var nodes = d.getElementsByTagName("*");
      for(var i=0, el; el=nodes[i]; i++){
        if(el.className==RegExp.$1){ this.nodes.push(el); };
      };
    } else if( selector.match(/^([^#\.].*)/) ){
      this.nodes = d.getElementsByTagName(RegExp.$1);
    }
  },

  // each iterator
  each: function(fn){
    for(var i=0, el; el=this.nodes[i]; i++){ fn.call(el); };
    return this;
  },

// Plugins here
  // custom image roll function
  imageRoll: function(off,on){
    var off = off || "_o";
    var on = on || "_h";
    return this.each(function(){
      var off_reg = new RegExp(off+"(\.[a-z]+$)","i");
      var on_reg = new RegExp(on+"(\.[a-z]+$)","i");
      if(this.src.match(off_reg)){
        this.pre = new Image(); //this.pre にロール用imgを作成
        this.pre.src = this.src.replace(off_reg,on+"$1"); //preload
        this.onmouseover = function(){
          this.src = this.src.replace(off_reg,on+"$1");
        }
        this.onmouseout = function(){
          this.src = this.src.replace(on_reg,off+"$1");
        }
      }
    });
  },

  // custom navigate
  navigate: function(active,current,parent){
    //ノードが存在し、なおかつfunction-disableが設定されていなかったら実行。
    if ( this.nodes[0] && this.nodes[0].className != "function-disable"){
      var active = active || "active";
      var current = current || "current";
      var parent = parent || "parent";
      if(d.URL.match(/\/$/)){
        url = (d.URL + "index.html").split("/");
      }else{
        url = d.URL.split("/");
      }
      location.pathname.split("/")[2]!="" ? current_dir = location.pathname.split("/")[1] + "/" + location.pathname.split("/")[2] + "/"
                                            : current_dir = "undefined";
      var target = this.nodes[0]
      //var current_dir = location.pathname.split("/")[2]|"";
      return ext(this.nodes[0].getElementsByTagName('dd')[0].getElementsByTagName('ul')[0]).each(function(){
        firstLi = this.getElementsByTagName('li')[0];
        firstLi.className = firstLi.className.addClass('first-child');
        if((typeof this == 'undefined')|this.className == "no-func"){return;}
        //hide other childs
        for(var i=0, _this; _this=this.childNodes[i]; i++){
          if(typeof _this.childNodes[0] != 'undefined' && !_this.childNodes[0].href.match( current_dir )){
            _this.className = _this.className.removeClass(parent);
          }
          if(location.pathname.split("/")[1] == "term" && typeof _this.childNodes[0] != 'undefined' && _this.childNodes[0].href.match( current_dir )){
            _this.className = _this.className.addClass( current )
          }
        };
        for(var i=0, element; element=this.getElementsByTagName('li')[i]; i++){
          element.url = element.getElementsByTagName('a')[0].href.split("/")
          if(element.url.join() == url.join() || (element.url[4] == url[4] && element.url[5] == url[5])){
            element.className = element.className.addClass(current);
          }
        };
      });
    }
  },

  // custom smooth scroll
  // BackScrollイベント処理
  _smoothScroll: function(target_x,target_y) {
    //再帰処理用イベント
    return this.each(function(){
      _self = this;
      var pos = {
      target_x: target_x,
      target_y: target_y
      }
      pos._x = d.body.scrollLeft || d.documentElement.scrollLeft || window.pageXOffset;
      if(typeof pos._x == "undefined"){ pos._x = 0 }
      pos._y = d.body.scrollTop  || d.documentElement.scrollTop  || window.pageYOffset;
      if(typeof pos._y == "undefined"){ pos._y = 0 }
      pos.y = pos.target_y - pos._y + 2;
      pos.x = pos.target_x - pos._x;
      pos.x = pos._x + Math.floor(pos.x / 2);
      pos.y = pos._y + Math.floor(pos.y - (pos.y*4 / 5));
      window.scrollTo(pos.x, pos.y);
      if (pos.x == pos._x && pos.y == pos._y) {
        setTimeout("location.href = _self.href", 100)
      }else{
        setTimeout("ext(_self)._smoothScroll(\""+pos.target_x+"\",\""+pos.target_y+"\")", 1);
      }
    });
  },

  smoothScroll: function(){
    //イベント本体
    var myLocation = location.href.split('#')[0];
    return this.each(function(){
      var myLink = this.href;
      //var targetID = null;
      if(this.href.match(/(.+)#([^?]+)/)){
        myLink = RegExp.$1
        var target = d.getElementById(RegExp.$2);
        if(target){
          if(myLocation == myLink){
            this.onclick = function(){
              var target_y = 0;
              var target_x = 0;
              for (var i = target;i.offsetParent;i=i.offsetParent ){
                target_y += i.clientTop||0;
                target_y += i.offsetTop||0;
                target_x += i.offsetleft||0;
              }
              var viewport_y = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
              var document_y = document.documentElement.scrollHeight || document.body.scrollHeight;
              target_y = (document_y - viewport_y < target_y) ? document_y - viewport_y : target_y;
              ext(this)._smoothScroll(target_x,target_y);
              return false;
            }
          }
        }
      }
    });
  },

  // custom addEventListener
  click: function(funcRef,capture){
    return this.each(function(){
      if(this.addEventListener) {
        this.addEventListener("click",funcRef,capture||false);
      } else if(this.attachEvent) {
        this.attachEvent('onclick', funcRef);
      } else {
        return false;
      }
      return true;
    });
  },

  // custom input roll function
  inputRoll: function(activeClass){
    return this.each(function(){
      this.onfocus = function(){
        if(this.type == "text"||this.type == "textarea"||this.type == "file"){
          this.className = this.className.addClass( activeClass );
        }
      }
      this.onblur = function(){
        if(this.type == "text"||this.type == "textarea"||this.type == "file"){
          this.className = this.className.removeClass( activeClass );
        }
      }
    });
  }

};


// フォーム制御のファンクションをまとめる
inputFocus = function(variable){
  ext("input").inputRoll(variable);
  ext("textarea").inputRoll(variable);
}

//Load functions 関数のロード
window.onload = function(){
  inputFocus("focus");
  ext("img").imageRoll("_o","_h");
  ext("input").imageRoll("_o","_h");
  ext("a").smoothScroll();
};

//Load plugins after html loaded 関数のロード(HTML読み込み完了後に実行)
htmlLoad = function(){
  ext('#local-navigation').navigate('active', 'current');
}
