var animate = {
	framesPerSec : 15,
	elementArray : new Array (),
	animationArray : new Array (),
	setEvent : function () {
		this.event          = arguments[0].replace(/"/g,"\\\"");
		this.event          = this.event.replace(/'/g,"\\\'");
		this.time           = (arguments[1]/this.framesPerSec)*1000;
		setTimeout("eval(\""+this.event+"\")",this.time);
	},
	newAnimation : function () {
		this.animation      = {
			stopped : false,
			repeat : false,
			events : new Array(),
			addEvent : function (event) {
				this.events.push(event);
			},
			go : function () {
				animate.startAnimation(this);
			},
			stop : function () {
				this.stopped = true;
			}
		}
		return this.animation;
	},
	startAnimation : function (animation,repeated) {
		if (repeated) {
			this.arrayNum   = animation;
			animation       = animate.animationArray[animation];
		}
		if (animation.stopped == false) {
			this.repeat         = animation.repeat;
			this.events         = animation.events;
			this.duration       = animation.duration;
			this.element        = animation.element;
			this.nextEA         = animate.elementArray.length;
			animate.elementArray[this.nextEA] = this.element;
			
			for (var i=0; i<this.events.length; i++) {
				this.event      = this.events[i];
				
				switch (this.event.effect) {
					case "fade":
						animate.setEvent("animate.fade(animate.elementArray["+this.nextEA+"],"+this.event.opacity+","+this.event.duration+")",this.event.startFrame);
						break;
					case "move":
						animate.setEvent("animate.move(animate.elementArray["+this.nextEA+"],"+(this.event.top ? this.event.top : "null")+","+(this.event.left ? this.event.left : "null")+","+this.event.duration+")",this.event.startFrame);
						break;
					case "scale":
						animate.setEvent("animate.scale(animate.elementArray["+this.nextEA+"],"+(this.event.newWidth ? this.event.newWidth : "null")+","+(this.event.newHeight ? this.event.newHeight : "null")+","+this.event.duration+")",this.event.startFrame);
						break;
					case "action":
						animate.setEvent(this.event.action,this.event.startFrame);
						break;
				}
			}
			if (this.repeat) {
				if (repeated == null) {
					animate.animationArray.push(animation);
				}
				this.time       = (this.duration/animate.framesPerSec)*1000;
				setTimeout("animate.startAnimation("+(this.arrayNum ? this.arrayNum : (animate.animationArray.length-1))+",true)",this.time);
			}
		}
	},
	animateThis : function (animation) {
		animation.element = this;
		animate.startAnimation(animation);
	},
	setPosition : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newY       = arguments[0];
			this.newX       = arguments[1];
		}
		else {
			arguments[0]    = eval(arguments[0]);
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newY       = arguments[1];
			this.newX       = arguments[2];
		}
		var styles          = {
			top : this.newY != null ? this.newY+"px" : null,
			left : this.newX != null ? this.newX+"px" : null
		};
		
		this.element.setStyles(styles);
	},
	setWidth : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newWidth   = arguments[0];
		}
		else {
			arguments[0]    = eval(arguments[0]);
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newWidth   = arguments[1];
		}
		this.element.setStyles({width: this.newWidth+"px"});
	},
	setHeight : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newHeight  = arguments[0];
		}
		else {
			arguments[0]    = eval(arguments[0]);
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newHeight  = arguments[1];
		}
		this.element.setStyles({height: this.newHeight+"px"});
	},
	setOpacity : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newOpacity = arguments[0];
		}
		else {
			arguments[0]    = eval(arguments[0]);
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newOpacity = arguments[1];
		}
		this.element.setStyles({opacity: this.newOpacity});
	},
	setBackground : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newColor   = arguments[0];
		}
		else {
			arguments[0]    = eval(arguments[0]);
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newColor   = arguments[1];
		}
		this.element.setStyles({backgroundColor: this.newColor+"px"});
	},
	move : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newY       = arguments[0];
			this.newX       = arguments[1];
			this.steps      = arguments[2];
		}
		else {
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newY       = arguments[1];
			this.newX       = arguments[2];
			this.steps      = arguments[3];
		}
		
		//add element to array
		this.nextEA         = animate.elementArray.length;
		animate.elementArray[this.nextEA] = this.element;
		
		//current
		this.currentX       = this.element.offsetLeft;
		this.currentY       = this.element.offsetTop;
		
		//the difference between current and new
		this.diffX          = this.currentX>this.newX ? 0-(this.currentX-this.newX) : this.currentX<this.newX ? this.newX-this.currentX : 0;
		this.eachX          = this.diffX/this.steps;
		
		this.diffY          = this.currentY>this.newY ? 0-(this.currentY-this.newY) : this.currentY<this.newY ? this.newY-this.currentY : 0;
		this.eachY          = this.diffY/this.steps;
		
		for (var i=0; i<=this.steps; i++) {
			if (this.diffX || this.diffY) {
				this.nextY = this.newY != null ? parseInt(this.currentY)+parseInt((parseInt(this.eachY)*i)) : null;
				this.nextX = this.newX != null ? parseInt(this.currentX)+parseInt((parseInt(this.eachX)*i)) : null;
				animate.setEvent("animate.setPosition(\"this.elementArray["+this.nextEA+"]\","+this.nextY+","+this.nextX+")",i);
			}
		}
		if (this.nextX != this.newX || this.nextY != this.newY) {
			animate.setEvent("animate.setPosition(\"this.elementArray["+this.nextEA+"]\","+this.newY+","+this.newX+")",this.steps);
		}
	},
	scale : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newWidth   = arguments[0];
			this.newHeight  = arguments[1];
			this.steps      = arguments[2];
		}
		else {
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newWidth   = arguments[1];
			this.newHeight  = arguments[2];
			this.steps      = arguments[3];
		}
		
		//add element to array
		this.nextEA         = animate.elementArray.length;
		animate.elementArray[this.nextEA] = this.element;
		
		//current
		this.currentWidth   = this.element.currentStyle ? this.element.currentStyle["width"] : window.getComputedStyle(this.element,"").getPropertyValue("width");
		this.currentWidth   = this.currentWidth.replace(/[a-zA-Z]{2}/,"",this.currentWidth);
		
		this.currentHeight  = this.element.currentStyle ? this.element.currentStyle["height"] : window.getComputedStyle(this.element,"").getPropertyValue("height");
		this.currentHeight  = this.currentHeight.replace(/[a-zA-Z]{2}/,"",this.currentHeight);
		
		//the difference between current and new
		this.diffW          = this.newWidth != null ? this.currentWidth>this.newWidth ? 0-(this.currentWidth-this.newWidth) : this.currentWidth<this.newWidth ? this.newWidth-this.currentWidth : 0 : null;
		this.eachW          = this.newWidth != null ? this.diffW/this.steps : null;
		
		this.diffH          = this.newHeight != null ? this.currentHeight>this.newHeight ? 0-(this.currentHeight-this.newHeight) : this.currentHeight<this.newHeight ? this.newHeight-this.currentHeight : 0 : null;
		this.eachH          = this.newHeight != null ? this.diffH/this.steps : null;
		
		for (var i=0; i<=this.steps; i++) {
			if (this.newWidth) {
				this.nextW = parseInt(this.currentWidth)+parseInt((parseInt(this.eachW)*i));
				animate.setEvent("animate.setWidth(\"this.elementArray["+this.nextEA+"]\","+this.nextW+")",i);
			}
			else {
				this.nextW = null;
			}
			if (this.newHeight) {
				this.nextH = parseInt(this.currentHeight)+parseInt((parseInt(this.eachH)*i));
				animate.setEvent("animate.setHeight(\"this.elementArray["+this.nextEA+"]\","+this.nextH+")",i);
			}
			else {
				this.nextH = null;
			}
		}
		if (this.nextH != this.newHeight || this.nextW != this.newWidth) {
			if (this.newWidth && this.diffW) { animate.setEvent("animate.setWidth(\"this.elementArray["+this.nextEA+"]\","+this.newWidth+")",this.steps); }
			if (this.newHeight && this.diffH) { animate.setEvent("animate.setHeight(\"this.elementArray["+this.nextEA+"]\","+this.newHeight+")",this.steps); }
		}
	},
	fade : function () {
		if (this.nodeType) {
			this.element    = this;
			this.newOpacity = arguments[0];
			this.steps      = arguments[1];
		}
		else {
			this.element    = arguments[0].nodeType ? arguments[0] : $(arguments[0]);
			this.newOpacity = arguments[1];
			this.steps      = arguments[2];
		}
		
		//add element to array
		this.nextEA         = animate.elementArray.length;
		animate.elementArray[this.nextEA] = this.element;
		
		//current
		this.currentOpacity = this.element.currentStyle ? this.element.currentStyle["opacity"] : window.getComputedStyle(this.element,"").getPropertyValue("opacity");
		this.currentOpacity = this.currentOpacity ? this.currentOpacity*100 : 100;
		
		this.diffOpac       = this.currentOpacity>this.newOpacity ? 0-(this.currentOpacity-this.newOpacity) : this.currentOpacity<this.newOpacity ? this.newOpacity-this.currentOpacity : 0;
		this.eachOpac       = this.diffOpac/this.steps;
		
		for (var i=0; i<=this.steps; i++) {
			this.nextO = (parseInt(this.currentOpacity)+parseInt((parseInt(this.eachOpac)*i)))/100;
			animate.setEvent("animate.setOpacity(\"this.elementArray["+this.nextEA+"]\","+this.nextO+")",i);
		}
		if (this.nextO != this.newOpacity) {
			if (this.newOpacity) { animate.setEvent("animate.setOpacity(\"this.elementArray["+this.nextEA+"]\","+(parseInt(this.newOpacity)/100)+")",this.steps); }
		}
	}
};