var currentEffects = new Array();

function Activa_FX(engine, target, duration) {
		target = domcheck(target);
		if( currentEffects.inArray(target) ) {
			return;
		} else {
			currentEffects.push(target);
		}
		this.resolution = 30;
		this.target = target;
		this.duration = typeof(duration) == 'undefined' ? 1000 : duration;
		this.start = new Date().getTime();
		this.end = this.start + this.duration;
		this.engine_name = engine;
		this.engine = this[engine];
		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) );
		
		if ( time < this.end ) {
			this.timer = window.setTimeout(createDelegate(this, 'framecalc'), this.resolution);
		} else {
			this.engine.end.call(this);
			currentEffects.remove(currentEffects.find(this.target));
		}
	}
}


Activa_FX.prototype.startRoll = function() {
	if(this.engine_name != 'RollDown' && this.engine_name != 'RollUp') {
		this.width = this.target.clientWidth;
		this._width = this.target.style.width;
	}
	if(fxHeight) {
		this.height = fxHeight;//this.target.clientHeight;
		this._height = fxHeight;
	} else {
		this.height = 40;//this.target.clientHeight;
		this._height = 40;
	}
	//this.height = this.target.clientHeight;
	//this._height = this.target.style.height;
	this._overflow = this.target.style.overflow;
	this.target.style.overflow = 'hidden';
	if(this.engine_name != 'RollDown' && this.engine_name != 'RollUp') {
		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';
			}
		}
	}
	
}

Activa_FX.prototype.endRoll = function() {
	if(this.engine_name != 'RollDown' && this.engine_name != 'RollUp') {
		this.target.style.width = this._width;
	}
	this.target.style.height = this._height;
	this.target.style.overflow = this._overflow;
	if(this.engine_name != 'RollDown' && this.engine_name != 'RollUp') {
		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;
			}
		}
	}
	
}

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.height = '0px';
	this.target.style.display = '';
	//this.target.style.height=hgt+"px";
	
	this.startRoll();
	
	
}

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();
}

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;
	}
}