var Animation = new Object();

Animation.positiontans = function(o, tpos, time, step, type){
	var spos = Element.getStyle(o, type);
	var apos = 0;
	var self   = this;
	var stpos = parseInt((tpos - spos) / step);
	var zpos = (spos > tpos? 0: 1);
	var action = 0;

	if (spos==tpos) return false;

	this.anipositiontrans = function(){
		var spos = Element.getStyle(o, type);
		var npos;
		
		if (zpos==0) { //如果-
			npos = stpos+spos;
			if (spos<tpos){ //超过
				if (action==0) action=1;//zheng xiang zou
				npos = spos-parseInt(stpos/step/0.5);
				if(npos>=tpos) npos = tpos;					
			}
		}
		else{
			npos = stpos+spos;
			if (spos>tpos){
				if (action==0) action=1;//zheng xiang zou
				npos = spos-parseInt(stpos/step/0.5);
				if(npos<=tpos) npos = tpos;					
			}
		}

		Element.setStyle(o, type, npos)
		if (action==1&&npos==tpos) return false;
		clearTimeout(o.tag);
		o.tag = setTimeout(function(){self.anipositiontrans()}, time)
		}

	this.anipositiontrans();
	}

Animation.opacitytans = function(obj, v, step){
	var nv = Element.getStyle(obj, 'opacity');
	clearTimeout(obj.tag);
	Element.setStyle(obj, 'opacity', nv + step);
	if ((nv + step)==v) return false;
	obj.tag = setTimeout(function(){Animation.opacitytans(obj, v, step)}, 60); 
	}

Animation.colortans = function(o, color, time, step, type){
	this.rgbcolor = function(s){
		if (s.substring(0, 1)=='#') s = s.substring(1, s.length);
		if (s.length==3) s = s.substring(0, 1) + s.substring(0, 1) + s.substring(1, 2) + s.substring(1, 2) + s.substring(2, 3) + s.substring(2, 3);
		var s_r = parseInt(s.substring(0, 2), 16);
		var s_g = parseInt(s.substring(2, 4), 16);
		var s_b = parseInt(s.substring(4, 6), 16);
		return Array(s_r, s_g, s_b);
		}

	var scolor = this.rgbcolor(Element.getStyle(o, type));
	var acolor = new Array();
	var tcolor = this.rgbcolor(color);
	var self   = this;

	this.anicolortrans = function(){
		var scolor = this.rgbcolor(Element.getStyle(o, type));
		var ncolor = new Array();
		//if (scolor[0]==tcolor[0]&&scolor[1]==tcolor[1]&&scolor[2]==tcolor[2]) return false;

		for (var i=0; i<3; i++){
			if(acolor[i]>=0) ncolor[i] = (scolor[i] + acolor[i]) >= tcolor[i]? tcolor[i].toString(16): (scolor[i] + acolor[i]).toString(16);
			else ncolor[i] = (scolor[i] + acolor[i]) <= tcolor[i]? tcolor[i].toString(16): (scolor[i] + acolor[i]).toString(16);
			if (ncolor[i].length!=2) ncolor[i] = '0' + ncolor[i];
			}
		
		Element.setStyle(o, type, '#' + ncolor[0]+ ncolor[1]+ ncolor[2])
		if (ncolor[0]==tcolor[0]&&ncolor[1]==tcolor[1]&&ncolor[2]==tcolor[2]) return false;
		clearTimeout(o.tag);
		o.tag = setTimeout(function(){self.anicolortrans()}, time)
		}

	for (var i=0; i<3; i++){
		acolor[i] = parseInt((tcolor[i] - scolor[i]) / step);
		if ((tcolor[i]-scolor[i])%step!=0) acolor[i] = acolor[i]>0?acolor[i]+1: acolor[i]-1;
		}

	this.anicolortrans();
	}
