// DDS Javascript Animation Library 1.1

function DDSAnimation() {
	this.objs = new Array();
	this.lastid = null;
	
	this.init = function(eid,initonce) {
		if(!initonce || (!this.objs[eid] && initonce)) { 
			this.objs[eid] = document.getElementById(eid);
			this.objs[eid].startvalues = new Array();
			this.objs[eid].newvalues = new Array();
			this.objs[eid].easings = new Array();
			this.objs[eid].duration = 1000;
		}
	}
	
	this.animate = function(eid,changes,time,p,initonce) {
		this.init(eid,initonce);
		var obj = this.objs[eid];
		if(p) obj.postAction=p;
			obj.lastaction = new Array();
			obj.lastaction['time'] = time;
			obj.lastaction['changes'] = new Array();
			obj.animState=1;
		this.lastid = eid;
		
		for(var i in changes) {
			var startvalue = 0;
			var newvalue = 0;
			var easing = "easeLinear";
			if (i == "width") startvalue = (obj.style.width)?obj.style.width:obj.offsetWidth;
			else if (i == "height") startvalue = (obj.style.height)?obj.style.height:obj.offsetHeight;
			else if (i == "top") startvalue = (obj.style.top)?obj.style.top:obj.offsetTop;
			else if (i == "left") startvalue = (obj.style.left)?obj.style.left:obj.offsetLeft;
			else if (i == "fadeIn") startvalue = 0;
			else if (i == "fadeOut") startvalue = 100;
			else if (i == "fadeTo") startvalue = this.getOpacity(obj);
			else if (i == "scrollLeft") startvalue = obj.scrollLeft;
			else startvalue = this.objs[eid].style[i];
			
			obj.startvalues[i]=parseInt(startvalue);
			
			if (i == "fadeIn") newvalue = 100;
			else if (i == "fadeOut") newvalue = 0;
			else newvalue=changes[i][0];
			
			obj.newvalues[i]= newvalue;
			if(changes[i][1] != undefined) easing=changes[i][1];
			if(!obj.easings[i]) obj.easings[i]=eval(easing);
			
			if (i == "fadeIn") obj.lastaction['changes']["fadeOut"] = [0,easing];
			else if (i == "fadeOut") obj.lastaction['changes']["fadeIn"] = [0,easing];
			else obj.lastaction['changes'][i] = [startvalue,easing];
		}
		if(time) obj.duration = time;
		
		obj.timeLeft = obj.duration;
		
		var self = this;
		this.process = function(lastTick,eid) {
			var obj = self.objs[eid];
			var curTick = new Date().getTime();
			var elapsedTicks = curTick - lastTick;
			if(obj.timeLeft <= elapsedTicks) {
				for(var i in obj.newvalues) {
					if (i == "fadeIn") self.setOpacity(obj,100);
					else if (i == "fadeOut") {
						self.setOpacity(obj,0);
					}
					else if (i == "fadeTo") self.setOpacity(obj,obj.newvalues[i]);
					else if (i == "scrollLeft") obj.scrollLeft = obj.newvalues[i];
					else obj.style[i] = obj.newvalues[i]+'px';
				}
				if(obj.hide) {
					obj.style.display='none';
					obj.hide=null;
				}
				if(obj.show) {
					obj.style.display='';
					obj.show=null;
				}
				obj.animState=0;
				if(typeof(obj.postAction) == "function") {
					obj.postAction();
				} else if(obj.postAction!=""){
					eval(obj.postAction);
				}
				obj.postAction="";
				return;
			}

			obj.timeLeft -= elapsedTicks;
			for(var i in obj.newvalues) {
				var easeFunc = obj.easings[i];
				var newval = easeFunc((obj.duration-obj.timeLeft),obj.startvalues[i],obj.newvalues[i]-obj.startvalues[i],obj.duration);
				if (i == "fadeIn" || i == "fadeOut" || i == "fadeTo") self.setOpacity(obj,newval);
				else if (i == "scrollLeft") obj.scrollLeft = newval;
				else obj.style[i] = newval+'px';
			}
			var timer = setTimeout(function(){ self.process(curTick,eid); }, 33);
		}
		
		this.process(new Date().getTime(),eid);
	}
	
	this.zoom = function(e,percent,t,f) {
		this.init(e);
		var obj = this.objs[e];
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;
		
		var nw = (ow*percent)/100;
		var nh = (oh*percent)/100;
		
		var ot = obj.offsetTop;
		var ol = obj.offsetLeft;
		var ch = ow/2 + ol;
		var cv = oh/2 + ot;
		
		var nl = ch - nw/2;
		var nt = cv - nh/2;
		if(f) this.animate(e,{"width":[nw],"height":[nh],"left":[nl],"top":[nt],"fadeOut":[]},t);
		else this.animate(e,{"width":[nw],"height":[nh],"left":[nl],"top":[nt]},t);
	}
	
	this.resizeTo = function(e,w,h,t,l,time) {
		this.init(e);
		var obj = this.objs[e];
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;
		
		var nw = w;
		var nh = h;
		
		var ot = obj.offsetTop;
		var ol = obj.offsetLeft;
		var ch = ow/2 + ol;
		var cv = oh/2 + ot;
		
		/*var nl = ch - nw/2;
		var nt = cv - nh/2;*/
		var nl = l;
		var nt = t;
		
		this.animate(e,{"width":[nw],"height":[nh],"left":[nl],"top":[nt]},time);
	}
	this.revert = function(a,e) {
		var id;
		if(!e && !this.lastid) return;
		if(e) id = e;
		else if(this.lastid) id = this.lastid;
		else return;
		if(id == a) return;
		var last = this.lastaction[id]; 
		this.lastaction[id] = new Array();
		this.animate(id,last['changes'],last['time'],1);
	}
	
	this.setOpacity = function(object,opacity) {
		object.style.opacity = (opacity / 100);
		object.style.MozOpacity = (opacity / 100);
		object.style.KhtmlOpacity = (opacity / 100);
		object.style.filter = "alpha(opacity=" + opacity + ")";
	}
	this.getOpacity = function(object) {
		if(object.style.opacity && object.style.opacity!="") return parseFloat(object.style.opacity)*100;
		if(object.style.MozOpacity && object.style.MozOpacity!="") return parseFloat(object.style.MozOpacity)*100;
		if(object.style.KhtmlOpacity && object.style.KhtmlOpacity!="") return parseFloat(object.style.KhtmlOpacity)*100;
		if(object.style.filter && object.style.filter!="") return parseInt(object.style.filter.match(/\d{1,3}/g));
		return 100;
	}
	
	this.fadeIn = function(e,t) {
		this.animate(e,{"fadeIn":[]},t);
	}
	this.fadeInS = function(e,t) {
		this.init(e);
		this.objs[e].show=1;
		this.animate(e,{"fadeIn":[]},t);
	}
	this.fadeOut = function(e,t,p) {
		this.animate(e,{"fadeOut":[]},t,p);
	}
	this.fadeOutH = function(e,t) {
		this.init(e);
		this.objs[e].hide=1;
		this.animate(e,{"fadeOut":[]},t);
	}
	this.fadeTo = function(e,to,t) {
		this.animate(e,{"fadeTo":[to]},t);
	}
	
	this.postproc = function(e,p) {
		this.init(e);
		this.objs[e].postProcess=p;
	}
	// easing equations by Robert Penner http://robertpenner.com/

	easeLinear = function (t, b, c, d) {
 		return (c/d)*t + b;
	};

	easeInQuad = function (t, b, c, d) {
		return c*(t/=d)*t + b;
	};
	easeOutQuad = function (t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	};
	easeInOutQuad = function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	};
	easeInCubic = function (t, b, c, d) {
		return c*(t/=d)*t*t + b;
	};
	easeOutCubic = function (t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	};
	easeInOutCubic = function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	};
	easeInQuart = function (t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	};
	easeOutQuart = function (t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	easeInOutQuart = function (t, b, c, d) {
	 	if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
	 	return -c/2 * ((t-=2)*t*t*t - 2) + b;
	};
	easeInQuint = function (t, b, c, d) {
	 	return c*(t/=d)*t*t*t*t + b;
	};
	easeOutQuint = function (t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	};
	easeInOutQuint = function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	};
	easeInSine = function (t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	};
	easeOutSine = function (t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	};
	easeInOutSine = function (t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	};
	easeInExpo = function (t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	};
	easeOutExpo = function (t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	easeInOutExpo = function (t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	};
	easeInCirc = function (t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	};
	easeOutCirc = function (t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	};
	easeInOutCirc = function (t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	};
	//a: amplitude (optional), p: period (optional)
	easeInElastic = function (t, b, c, d, a, p) {
		if (t==0) { return b; } 
		if ((t/=d)==1) { return b+c; }
		if (!p) { p=d*.3; }
		if (a < Math.abs(c)) {  a=c; s=p/4; }
		else { a=Math.abs(c); s = p/(2*Math.PI) * Math.asin(c/a);}
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	};
	easeOutElastic = function (t, b, c, d, a, p) {
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else {   a=Math.abs(c); var s = p/(2*Math.PI) * Math.asin (c/a);}
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	easeInBounce = function (t, b, c, d) {
 		return c - easeOutBounce (d-t, 0, c, d) + b;
	};
	easeOutBounce = function (t, b, c, d) {
 		if ((t/=d) < (1/2.75)) {
  			return c*(7.5625*t*t) + b;
 		} else if (t < (2/2.75)) {
  			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
 		} else if (t < (2.5/2.75)) {
  			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
 		} else {
  			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
 		}
	};
	easeInOutBounce = function (t, b, c, d) {
 		if (t < d/2) return easeInBounce (t*2, 0, c, d) * .5 + b;
 		return easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
	};

}

var DDSanim = new DDSAnimation();
