
/*
----------------------------------------------------
helper functions
----------------------------------------------------
*/
function increment(endValue,startValue,changeBy) {
	if(Math.abs(endValue-startValue) > Math.abs(changeBy)) {
		return (startValue + changeBy) + 'px';
	}
	else {
		return endValue + 'px';
	}
}
function delta(x,y) {
	return Math.abs(x-y);
}



/*
----------------------------------------------------
Effect prototype
----------------------------------------------------
*/

function Effect(elem) {
	if (document.getElementById(elem)) { this.target = document.getElementById(elem); } else { return false; }
    aObjects.push(this); 
    this.ID = aObjects.length - 1;
	this.target.style.position = 'absolute'; // required for effects
}

Effect.prototype.setTimer = function (fn) {
	if (this.frame > 0) { 
		this.timer = setTimeout(fn,this.delay);
	} 
	else { 
		clearTimeout(this.timer); 
	}
}

// scroll functions depend upon a container DIV with style="overflow:hidden; position:relative;"
Effect.prototype.scrollDown = function (speed,FPS) {
	if (speed == null) speed = 10;
	if (FPS == null) FPS = 50;
	this.glide(null,-this.target.offsetHeight,null,null,FPS,speed*Math.abs(this.target.offsetTop+this.target.offsetHeight));
}
Effect.prototype.scrollUp = function (speed,FPS) {
	if (speed == null) speed = 10;
	if (FPS == null) FPS = 50;
	this.glide(null,0,null,null,FPS,speed*Math.abs(this.target.offsetTop));
}
Effect.prototype.scrollLeft = function (speed,FPS) {
	if (speed == null) speed = 10;
	if (FPS == null) FPS = 50;
	this.glide(-this.target.offsetWidth,null,null,null,FPS,speed*Math.abs(this.target.offsetLeft+this.target.offsetWidth));
}
Effect.prototype.scrollRight = function (speed,FPS) {
	if (speed == null) speed = 10;
	if (FPS == null) FPS = 50;
	this.glide(0,null,null,null,FPS,speed*Math.abs(this.target.offsetLeft));
}
Effect.prototype.scrollStop = function () {
	clearTimeout(this.timer);
}



/*
----------------------------------------------------
endX = final X position in pixels
endY = final Y position in pixels
startX = optional
startY = optional
FPS = Frames Per Second, optional, default 50
time = number of seconds to complete animation, optional, default 1 second
exec = any code to execute when the animation completes
----------------------------------------------------
*/
Effect.prototype.glide = function (endX,endY,startX,startY,FPS,time,exec) {
	this.endX = endX;
	this.endY = endY;
	this.startX = startX;
	this.startY = startY;
	this.FPS = FPS;
	this.time = time;
	this.exec = exec;
	
	// set defaults where value == null
	if (this.endX == null) this.endX = this.target.offsetLeft;
	if (this.endY == null) this.endY = this.target.offsetTop;
	if (this.startX == null) this.startX = this.target.offsetLeft;
	if (this.startY == null) this.startY = this.target.offsetTop;
	if (this.FPS == null) this.FPS = 50;
	if (this.time == null) this.time = 1000;
	if (this.exec == null) this.exec = 'return true';
	
	this.frame = this.FPS * (this.time/1000); // count down to zero
	this.delay = this.time/this.frame;

	this.moveX = Math.round(delta(this.startX,this.endX) / this.frame);
	this.moveY = Math.round(delta(this.startY,this.endY)/ this.frame);
	
	if (this.endX < this.startX) this.moveX = -this.moveX;
	if (this.endY < this.startY) this.moveY = -this.moveY;

	this.target.style.left = increment(this.endX,this.startX,this.moveX);
	this.target.style.top = increment(this.endY,this.startY,this.moveY);
	debug.info(this.target.id+': glide('+this.endX+','+this.endY+','+this.startX+','+this.startY+','+this.FPS+','+(this.frame*this.delay)+')');
	debug.info('increment('+this.endX+','+this.startX+','+this.moveX+'), result: '+increment(this.endX,this.startX,this.moveX));
	
	this.frame--;
	this.setTimer('aObjects[' + this.ID + '].glide('+this.endX+','+this.endY+',null,null,'+this.FPS+','+(this.frame*this.delay)+')');
}


/*
----------------------------------------------------
endWidth = final width in pixels
endHeight = final height in pixels
startWidth = optional
startHeight = optional
pivot = left, right or center - default left
FPS = Frames Per Second, optional, default 50
time = number of seconds to complete animation, optional, default 1 second
exec = any code to execute when the animation completes
----------------------------------------------------
*/
Effect.prototype.size = function (endWidth,endHeight,startWidth,startHeight,pivot,FPS,time,exec) {
	this.endWidth = endWidth;
	this.endHeight = endHeight;
	this.startWidth = startWidth;
	this.startHeight = startHeight;
	this.pivot = pivot;
	this.FPS = FPS;
	this.time = time;
	this.exec = exec;
	
	// set defaults where value == null
	if (this.endWidth == null) this.endWidth = this.target.offsetWidth;
	if (this.endHeight == null) this.endHeight = this.target.offsetHeight;
	if (this.startWidth == null) this.startWidth = this.target.offsetWidth;
	if (this.startHeight == null) this.startHeight = this.target.offsetHeight;
	if (this.pivot == null) this.pivot = "left";
	if (this.FPS == null) this.FPS = 50;
	if (this.time == null) this.time = 1000;
	if (this.exec == null) this.exec = 'return true';
	
	this.frame = this.FPS * (this.time/1000); // count down to zero
	this.delay = this.time/this.frame;

	this.growWidth = Math.round(delta(this.startWidth,this.endWidth) / this.frame);
	this.growHeight = Math.round(delta(this.startHeight,this.endHeight) / this.frame);

	this.target.style.width = increment(this.endWidth,this.startWidth,this.growWidth);
	this.target.style.height = increment(this.endHeight,this.startHeight,this.growHeight);
	
	this.frame--;
	this.setTimer('aObjects[' + this.ID + '].size('+this.endWidth+','+this.endHeight+',null,null,null,'+this.FPS+','+(this.frame*this.delay)+')');
}
