 // !Document Information
/**
 *	TMEffect
 *	TheoreticalMedia
 *	@author Nate Schulz
 *	@version 0.1.2
 */

Object.prototype.Effect 	= function(t,d,c) {new Effect(t,d,c,this);};
Object.prototype.Hide			= function() {if (typeof this != "function"){ this.style.opacity = 0;this.style.display = "none";}};
Object.prototype.Show			= function() {if (typeof this != "function"){ this.style.display = "block"}};
Object.prototype.FadeIn		= function(d,c) {new Effect('fadeIn',d,c,this);};
Object.prototype.FadeOut	= function(d,c) {new Effect('fadeOut',d,c,this);};


function Effect(type, duration, callback, obj) {
	this._effectID = $TMEffectsCore.push(this);
	this._obj = obj;
	this._type = type.split("->")[0];
	this._target = type.split("->")[1];
	if (this._type == "blink") this.blink(0.5);
	if (this._type == "opacity") new Transition("opacity", duration, obj, this._target);
	if (type=="fadeIn") new Transition("opacity", duration, obj, 1.0);
	else if (type=="fadeOut") new Transition("opacity", duration, obj, 0);
	this._callback = callback;
};
Effect.prototype = {
	xcb: function() {
		if (this._callback != undefined) {
			//var cbf = 
			this._callback();
			//cbf();
		}
	}
};
function Transition(property, duration, obj, target) {
	this._startTime = this.now();
	if (duration == undefined) duration = 250;
	this._duration = duration;
	this._property = property;
	this._obj = obj;
	this._obj.style.display = "block";
	this._fromValue = this._obj.style[property];
	this._toValue = target;
	if ( this._fromValue == "" || this._fromValue == undefined ) {
		if (this._toValue < 1) this._fromValue = 1;
		else this._fromValue = 0;
	}
	this._fromValue = parseFloat(this._fromValue);
	this._toValue = parseFloat(this._toValue);
	this._stepCount = 0;
	this._timerID = $TMEffectsCore.setTimer(this);
};

Transition.prototype = {
	step: function() {
		this._cV = this._obj.style[this._property].split("px")[0];
		if (this.elapsed() < this._duration && this._toValue < this._fromValue){
			this._obj.style[this._property] = this._fromValue-(this.elapsed()/this._duration);
		} else if ( this.elapsed() < this._duration ) { // && Math.abs(this._cV-this._toValue) > 0
			this._obj.style[this._property] = this._fromValue+((this.elapsed()/this._duration));//*this._toValue
		} else {
			if (this._toValue == 0 && this._property == "opacity") this._obj.style.display = "none";
			this._obj.style[this._property] = this._toValue;
			$TMEffectsCore.clearTimer(this._timerID);
		}
	},
	now: function(){
		return new Date().getTime();
	},
	elapsed: function() {
		return new Date().getTime()-this._startTime;
	}
};

function TMEffectCore() {
	this._timers = [];
	this._timerEvents = [];
	this._effects = [];
};

TMEffectCore.prototype = {
	push: function(E) {
		var eID = this._effects.length;
		this._effects[eID] = E;
		return eID;
	},
	pop: function(i) {
		this._effects[i].xcb();
		//this._effects.splice(i,1)
	},
	setTimer: function(E) {
		var tID = this._timers.length;
		this._timerEvents[tID] = E;
		this._timers[tID] = setInterval("$TMEffectsCore._timerEvents["+tID+"].step()", 20);
		return tID;
	},
	clearTimer: function(i) {
		clearInterval(this._timers[i]);
		clearTimeout(this._timers[i]);
		this.pop(i);
	},
	clearAllTimers: function() {
		for (var i=0; i<this._timers.length; i++)
		{
			clearInterval(this._timers[i]);
			clearTimeout(this._timers[i]);
		}
		delete this._timers;
		this._timers = [];
	}
};

var $TMEffectsCore = TMCore.TMEffectsCore = new TMEffectCore();
