var currentEffects = new Array();

function Activa_FX(target, engine, duration, callback, framelimit) {
		if ( currentEffects.inArray(target) ) {
			return;
		} else {
			currentEffects.push(target);
		}
		
		this.target = domcheck(target);
		this.resolution = 30;
		
		this.engine = typeof(engine) == 'undefined' ? null : this[engine];
		this.duration = typeof(duration) == 'undefined' ? 1000 : duration;
		this.callback = typeof(callback) == 'undefined' ? null : callback;
		this.framelimit = typeof(framelimit) == 'undefined' ? 1 : framelimit;
		
		if ( this.duration != null ) {
			this.start = new Date().getTime();
			this.end = this.start + this.duration;
		}
		
		if ( this.engine != null ) {
			this.engine();
			this.timer = window.setTimeout(createDelegate(this, 'framecalc'), this.resolution);
		}
	
	this.framecalc = function() {
		var time = new Date().getTime();
		if ( time > this.end ) {
			time = this.end;
		}
		
		this.engine.frame.call(this, ((time - this.start) / (this.end - this.start)) * this.framelimit);
		
		if ( time < this.end ) {
			this.timer = window.setTimeout(createDelegate(this, 'framecalc'), this.resolution);
		} else {
			if ( this.engine.end ) {
				this.engine.end.call(this);
			}
			
			currentEffects.remove(currentEffects.find(this.target));
			
			if ( typeof(this.callback) != 'undefined' && this.callback != null ) {
				this.callback();
			}
		}
	}
	
	this.startEngine = function(engine, duration, callback, framelimit) {
		this.engine = this[engine];
		this.duration = typeof(duration) == 'undefined' ? 1000 : duration;
		this.callback = callback;
		this.framelimit = typeof(framelimit) == 'undefined' ? 1 : framelimit;
		
		this.start = new Date().getTime();
		this.end = this.start + this.duration;
		
		this.engine();
		
		this.timer = window.setTimeout(createDelegate(this, 'framecalc'), this.resolution);
	}
}

Activa_FX.prototype.startRoll = function() {
	this.width = this.target.clientWidth;
	this._width = this.target.style.width;
	
	this.height = this.target.clientHeight;
	this._height = this.target.style.height;
	
	this._overflow = this.target.style.overflow;
	
	this.target.style.overflow = 'hidden';
	
	for ( x=0; x<this.target.childNodes.length; x++ ) {
		if ( this.target.childNodes[x].nodeType == 1 ) {
			this.target.childNodes[x]._width = this.target.childNodes[x].style.width;
			this.target.childNodes[x].style.width = this.target.childNodes[x].clientWidth + 'px';
			
			this.target.childNodes[x]._height = this.target.childNodes[x].style.height;
			this.target.childNodes[x].style.height = this.target.childNodes[x].clientHeight + 'px';
		}
	}
}

Activa_FX.prototype.endRoll = function() {
	this.target.style.width = this._width;
	this.target.style.height = this._height;
	
	this.target.style.overflow = this._overflow;
	
	for ( x=0; x<this.target.childNodes.length; x++ ) {
		if ( this.target.childNodes[x].nodeType == 1 ) {
			this.target.childNodes[x].style.width = this.target.childNodes[x]._width;
			this.target.childNodes[x].style.height = this.target.childNodes[x]._height;
		}
	}
}

Activa_FX.prototype.RollInLeft = function() {
	this.startRoll();
}

Activa_FX.prototype.RollInLeft.frame = function(scale) {
	this.target.style.width = Math.round(( (1 - scale) *  this.width)) + 'px';
}

Activa_FX.prototype.RollInLeft.end = function() {
	this.target.style.display = 'none';
	this.endRoll();
}

Activa_FX.prototype.RollUp = function() {
	this.startRoll();
}

Activa_FX.prototype.RollUp.frame = function(scale) {
	this.target.style.height = Math.round(( (1 - scale) *  this.height)) + 'px';
}

Activa_FX.prototype.RollUp.end = function() {
	this.target.style.display = 'none';
	this.endRoll();
}

Activa_FX.prototype.RollDown = function() {
	this.target.style.display = '';
	
	this.startRoll();
	this.target.style.height = '0px';
}

Activa_FX.prototype.RollDown.frame = function(scale) {
	this.target.style.height = Math.round(( scale *  this.height)) + 'px';
}

Activa_FX.prototype.RollDown.end = function() {
	this.endRoll();
}

Activa_FX.prototype.FadeOut = function() {
	
}

Activa_FX.prototype.FadeOut.frame = function(scale) {
	this.target.style.opacity = 1 - scale;
	
	if ( this.target.filters ) {
		try {
			this.target.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100 - (100 * scale);
		} catch ( e ) {
			
		}
	} else {
		this.target.style.opacity = 1- scale;
	}
}

Activa_FX.prototype.FadeIn = function() {
	this.display = false;
}

Activa_FX.prototype.FadeIn.frame = function(scale) {
	if ( this.target.filters ) {
		try {
			this.target.filters.item("DXImageTransform.Microsoft.Alpha").opacity = (100 * scale);
		} catch ( e ) {
			
		}
	} else {
		this.target.style.opacity = scale;
	}
	
	if ( !this.display ) {
		this.target.style.display = '';
		
		for ( x=0; x<this.target.childNodes.length; x++ ) {
			if ( this.target.childNodes[x].nodeType == 1 ) {
				this.target.childNodes[x].style.display = '';
			}
		}
		
		this.display = true;
	}
}

Activa_FX.prototype.BoxUp = function() {
	this._top = this.target.offsetTop;
	this._left = this.target.offsetLeft;
	
	this.target.style.top = -1000+'px';
	this.target.style.left = -1000+'px';
	this.target.style.display = '';
	
	this.startRoll();
	
	this.target.style.height = '0px';
	this.target.style.width = '10px';
}

Activa_FX.prototype.BoxUp.frame = function(scale) {
	//this.target.style.height = Math.round(((scale) *  this.height)) + 'px';
	this.target.style.height = ((scale) *  this.height) + 'px';

	this.target.style.top = Math.round((this._top + (this.height/2)) - (((scale) * this.height) / 2))+'px';
	this.target.style.left = Math.round((this._left + (this.width/2)) - (((0) * this.width) / 2))+'px';
	
	this.target.style.display = '';
}

Activa_FX.prototype.BoxUp.end = function() {

}

Activa_FX.prototype.BoxOut = function() {
}

Activa_FX.prototype.BoxOut.frame = function(scale) {
	//this.target.style.width = Math.round(((scale) *  this.width)) + 'px';
	this.target.style.width = ((scale) *  this.width) + 'px';
	
	this.target.style.left = Math.round((this._left + (this.width/2)) - (((scale) * this.width) / 2))+'px';
}

Activa_FX.prototype.BoxOut.end = function() {
	//this.endRoll();
}

function draggable(elem, handle) {
	this.elem = domcheck(elem);
	if ( typeof(handle) != 'undefined' ) {
		this.handle = domcheck(handle);
	} else {
		this.handle = domcheck(elem);
	}
	this.dragging = false;
	registerEvent(this.handle, 'mousedown', createDelegate(this, 'mousedown'));
	this.offsetLeft = 0;
	this.offsetTop = 0;
	
	this.mousedown = function(e) {
		this.offsetLeft = e.clientX - getX(this.elem);
		this.offsetTop = e.clientY - getY(this.elem) + 10;
		this.dragging = this.elem.cloneNode(true);
		this.dragging.style.position = 'absolute';
		this.dragging.style.top = (e.clientY - this.offsetTop)+'px';
		this.dragging.style.left = (e.clientX - this.offsetLeft)+'px';
		this.dragging.style.width = this.elem.clientWidth+'px';
		this.dragging.style.height = this.elem.clientHeight+'px';
		this.dragging.style.opacity = 0.5;
		this.elem.parentNode.appendChild(this.dragging);
		this.mousemovedelegate = createDelegate(this, 'mousemove');
		this.mouseupdelegate = createDelegate(this, 'mouseup');
		registerEvent(document, 'mousemove', this.mousemovedelegate);
		registerEvent(document, 'mouseup', this.mouseupdelegate);
		if ( e.preventDefault ) {
			e.preventDefault();
		}
		document.body.focus();
		if ( this.start ) {
			this.start();
		}
		return false;
	}
	
	this.mousemove = function(e) {
		this.dragging.style.top = (e.clientY-this.offsetTop)+'px';
		this.dragging.style.left = (e.clientX-this.offsetLeft)+'px';
		if ( this.moved ) {
			this.moved(e);
		}
	}
	
	this.mouseup = function(e) {
		if ( this.stop ) {
			this.stop();
		}
		unregisterEvent(document, 'mousemove', this.mousemovedelegate);
		unregisterEvent(document, 'mouseup', this.mouseupdelegate);
		this.dragging.parentNode.removeChild(this.dragging);
	}
	
}

function getX(elem) {
	if ( elem.offsetParent ) {
		return getX(elem.offsetParent) + elem.offsetLeft;
	} else {
		return elem.offsetLeft;
	}
}

function getY(elem) {
	if ( elem.offsetParent ) {
		return getY(elem.offsetParent) + elem.offsetTop;
	} else {
		return elem.offsetTop;
	}
}