/* -------------------------------- 
    ユーティリティクラス　base
 -------------------------------- */

var base = {
  addevent: function(node,evt,func){
	if(node.addEventListener){
		node.addEventListener(evt,func,false);	
	} else if(node.attachEvent){
		node.attachEvent("on"+evt,func);	
	}
  },
  bind: function(){
	var args=[];
    if(arguments){
      for(var i=0,n=arguments.length;i<n;i++){
        args.push(arguments[i]);
      }
    }

    var object=args.shift();
    var func=args.shift();

    return function(event) {
      return func.apply(object,[event||window.event].concat(args));
    }
  }
};

/* -------------------------------- 
    アニメーションクラス　Animation
 -------------------------------- */

var Animation = function(obj){
    var args = arguments[0];
    this.int;
    this.minute = 50;
    this.per = 5;
    if(Object.prototype.toString.call(obj) == "[object HTMLDivElement]"){
        this.obj = obj;	    
    } else {
        for(item in obj){ this[item] = obj[item]; }
    };
};

Animation.prototype = {
    run: function(type,beginvalue,aftervalue,func){
        this.type = type;
        this.bvalue = beginvalue;
        this.avalue = aftervalue;
        this.type == "opacity" ? this.unit = 0 : this.unit = "px";
	this.obj.style[this.type] = beginvalue + this.unit;
	this.func = func;
        //判定する関数をかませる
        this.bvalue > this.avalue ? this.decreament() : this.increament();
    },
    // decreament
    decreament: function(){
        if(this.int){ clearTimeout(this.int); };
        if(this.bvalue > this.avalue){
            this.bvalue -= this.per;
            this.obj.style[this.type] = this.bvalue + this.unit;
            this.int = setTimeout(base.bind(this,this.decreament),this.minute);
        } else {
            clearTimeout(this.int);
	    if(this.func){ this.func(); };
        }
    },
    // increament
    increament: function(){
        if(this.int){ clearTimeout(this.int); };
        if(this.bvalue < this.avalue){
            this.bvalue += this.per;
            this.obj.style[this.type] = this.bvalue + this.unit;
            this.int = setTimeout(base.bind(this,this.increament),this.minute);
        } else {
            clearTimeout(this.int);
	    if(this.func){ this.func(); };
        }
    }

};


(function(){
base.addevent(window,"load",function(){

var hoge = function(){ alert("ok"); };
var hash = {
    obj : document.getElementById('tip'),
    minute : 20,
    per : 5
};
var obj = document.getElementById('tip');
var es = new Animation(hash).run('left',100,200,hoge);

});
})();

