var MOTION_FADE_LIBRARY = new Array();

function getPos(element) {
  var r = { x: element.offsetLeft, y: element.offsetTop };
  if (element.offsetParent) {
    var tmp = getPos(element.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
}

function exep(msg) {
	window.setTimeout(function() {exep2(msg); }, 1);
}

function exep2(msg) {
	throw(msg);
}

function motion_ease(object_container, object_toBeEased) {
	if (typeof object_container != "string") {
		this.contBox = object_container;
	}else{
		this.contBox = document.getElementById(object_container);
	}
	
	if (typeof object_toBeEased != "string") {
		this.moveBox = object_toBeEased;
	}else{
		this.moveBox = document.getElementById(object_toBeEased);
	}
	this.inMotion = false;
	this.overrideMotion = true;
	this.registeredEvent = 0;
	this.dx = 0;
	this.dy = 0;
	
	//initialize safe CSS values
	if (this.moveBox.style.left == "") {
		this.moveBox.style.left = "0px";
	}
	if (this.moveBox.style.top == "") {
		this.moveBox.style.top = "0px";
	}
	
	//non-breaking call function
	this.call = function(fn) {
		window.setTimeout(fn, 1);
	}
	
	//play controls
	this.stop = function() {
		window.clearTimeout(this.registeredEvent);
	}
	this.ffwd = function() {
		window.clearTimeout(this.registeredEvent);
		this.sp(this.dx, this.dy);
	}
	
	
	//function to determine the maximum x,y value of the box
	this.edx = function() {
		return this.moveBox.clientWidth;
	}
	
	this.edy = function() {
		return this.moveBox.clientHeight;
	}
	
	//set the poition of the block
	this.sp = function(x, y) {
		this.moveBox.style.left = x + "px";
		this.moveBox.style.top = y + "px";
	}
	
	//set the desired position of the block
	this.sd = function(x, y) {
		this.dx = x;
		this.dy = y;
	}
	
	//accessor methods
	this.getX = function() {
		return parseInt(this.moveBox.style.left);
	}
	
	this.getY = function() {
		return parseInt(this.moveBox.style.top);
	}
	
	//setting px to a reasonable value
	this.px = this.getX();
	this.py = this.getY();
	
	//checks to see if it is safe to proceed
	this.checkProceed = function() {
		if (this.inMotion == true) {
			if (this.overrideMotion == true) {
				window.clearTimeout(this.registeredEvent);
			}else{
				return false;
			}
		}
		this.inMotion = true;
		return true;
	}
	
	//events that fire and can be customized
	this.onStart = function() { }
	this.onFinish = function() { }
	
	this.elastic = function(factor, frequency, t, ox, oy) {
		var x, y, me = this;
		
		if (typeof t == "undefined") {
			t = 0;
			this.px = this.getX() + 1000;
			this.py = this.getY() + 1000;
			ox = this.getX();
			oy = this.getY();
			if (this.checkProceed() == false) {
				return;
			}
			this.call(this.onStart);
		}
		
		x = ((ox - this.dx) * Math.exp(-factor * t) * Math.sin(frequency * t + Math.PI/2)) + this.dx;
		y = ((oy - this.dy) * Math.exp(-factor * t) * Math.sin(frequency * t + Math.PI/2)) + this.dy;
		
		this.moveBox.style.left = x + "px";
		this.moveBox.style.top =  y + "px";
		
		if (Math.abs(x - this.px) >= .01 || Math.abs(y - this.py) >= .01 && Math.abs(x - this.dx) >= 1 || Math.abs(y - this.dy) >= 1 ) {
			this.registeredEvent = window.setTimeout(function() {
				me.elastic(factor, frequency, t+.1, ox, oy);
			}, 30);
		}else{
			this.moveBox.style.left = this.dx + "px";
			this.moveBox.style.top = this.dy + "px";
			this.inMotion=false;
			this.call(this.onFinish);
		}
		
		this.px = x;
		this.py = y;
	}
	
	
}

function addMouseoverFade(object, faderObj) {
	object.style.opacity = ".5";
	object.style.filter = "alpha(opacity=50)";
	object.onmouseover = function() {
		faderObj.fade(1, 2.5);
	}
	
	object.onmouseout = function() {
		faderObj.fade(0.5, 2.5);
	}
}

function fader(object) {
	if (typeof object == "string") {
		this.moveBox = document.getElementById(object);
	}else{
		this.moveBox = object;
	}
	this.registeredEvent = 0;
	this.fading = false;
	this.onComplete = function() { };
	
	this.fade = function(dop, factor, t, oop) {
		var op, me=this;
		if (typeof t == "undefined") {
			if (this.fading == true) {
				window.clearTimeout(this.registeredEvent);
			}
			this.fading = true;
			t = 0;
			oop = parseFloat(this.moveBox.style.opacity);
		}
		
		op = (oop - dop) * Math.exp(-factor * t) + dop;
		
		this.moveBox.style.opacity = op;
		//if (this.moveBox.style.filter) {
		this.moveBox.style.filter = "alpha(opacity=" + Math.floor(op * 100) + ")";
		//}
		
		if ( Math.abs(op-dop) >= .01) {
			this.registeredEvent = window.setTimeout(function() {
				me.fade(dop, factor, t + .1, oop);
			}, 30);
		}else{
			this.moveBox.style.opacity = dop;
			this.fading = false;
			this.onComplete();
		}
	
	
	}
}

//when dynamically generating motions, this is a useful memory freer
function garbage_collect_motions() {
	//for(var i = 0; i < MOTION_FADE_LIBRARY.length; i++) {
		alert( MOTION_FADE_LIBRARY.length);
	//}
}