// -----------------------------------------------
//    xonTAB Javascript API - Animation Module
// -----------------------------------------------
// API Module: Animation
// Version: 1.1
// Created by: xonTAB (eosArts)
// Copyright: 2008-2009

// Additional Notes:
// - This API is backwards compatible with v1.0.

// -----------------------------------------------

// Desc: Animation Class
// Returns: void
function Animation(divelem) {
	var elem = divelem;
	var anims = null;
	
	this.getElem = function() {
		return elem;
	}
	
	this.setTOV = function(tov) {
		anims = tov;
	}
	
	this.clearTOV = function() {
		clearTimeout(anims);
	}
}

var anis = null;

// -----------------------------------------------

// Desc: Includes all the logic needed to run animation.
// Type: Private
// Returns: void
function st_animation_alias(obj, css, frame, to, speed, rate, slow) {
	if (Math.abs(to-frame) > 0) {
		// if slow is set to true the animation will slow down
		if ((slow) && (Math.abs(to-frame) <= Math.abs(rate*5))) {
			speed+=5;
			if (rate>0) {
				rate-=3;
				if (rate<1) rate = 1;
			} else {
				rate+=3;
				if (rate>-1) rate = -1;
			}
		}
		if (Math.abs(to-frame) < Math.abs(rate)) {
			rate = Math.abs(to-frame);
		}
		// Set rate to + or - according to the values of frame and to
		if (to >= frame) { 
			rate = Math.abs(rate);
		} 
		else {
			rate = -Math.abs(rate); 
		}
		frame+=rate;
		switch (css) {
			// Scroll Y-Axis Animation
			case "scrolly" : 
				obj.getElem().scrollTop = frame;
				break;
			// Scroll X-Axis Animation
			case "scrollx" : 
				obj.getElem().scrollLeft = frame;
				break;
			// Increase Width Animation
			case "width" : 
				obj.getElem().style.width = frame+'px';
				break;
			// Increase Height Animation
			case "height" : 
				obj.getElem().style.height = frame+'px';
				break;
			// Move Top Animation
			case "top" : 
				obj.getElem().style.top = frame+'px';
				break;
			// Move Left Animation
			case "left" : 
				obj.getElem().style.left = frame+'px';
				break;
			// Text Indent Animation
			case "textIndent" : 
				obj.getElem().style.textIndent = -frame+'px';
				break;
			// Text Indent Animation
			case "textScroll" : 
				obj.getElem().style.clip = 'rect('+frame+'px,auto,auto,auto)';
				break;
			// Opacity Animation
			case "opacity" : 
				obj.getElem().style.opacity = frame;
				obj.getElem().style.filter = 'alpha(opacity='+frame*100+')';
				break;
			case "opacitynoie" : 
				obj.getElem().style.opacity = frame;
				break;
		}
		obj.setTOV(setTimeout(function(){st_animation_alias(obj, css, frame, to, speed, rate, slow)},speed));
	}
}

// -----------------------------------------------

// Desc: Used for animations without init an Animation Objects
// Type: Public
// Returns: void
function st_animation(elem, css, frame, to, speed, rate, slow) {
	var obj = new Animation(elem);
	st_animation_alias(obj, css, frame, to, speed, rate, slow);
}

// -----------------------------------------------

// Desc: Used for animations after creating Animation Object
// Type: Public
// Returns: void
function st_animationb(obj, css, frame, to, speed, rate, slow) {
	clearTimeout(obj.clearTOV());
	setTimeout(function(){st_animation_alias(obj, css, frame, to, speed, rate, slow)},speed);
}

// -----------------------------------------------

