/* Copyright 2004 Ken Waletzki www.brainkinetics.com complete IP info at brainketics.com */

if (typeof bk == "undefined") {
	bk = new function() { ; };
}

function retFalse()
{
	return false;
}

easeMotion = function(o, ndx, easeFunc, ms, freq, func, endFunc)
{
	if (typeof endFunc == "undefined") {
		endFunc = function () { return false; };
	}
	this.o = o;
	this.objId = o.id;
	this.ndx = ndx;
	this.easeFunc = easeFunc;
	this.inProgress = 1;
	this.tick = 0;
	this.maxTicks = ms /freq;
	this.ms = ms;
	this.freq = freq;
	this.func = func;
	this.endFunc = endFunc;
	this.startTimer = function () {
		this.msStart = ((new Date())-0);
		this.intervalTimer = setInterval("easeTick('"+this.objId+"','"+this.ndx+"')", this.freq );
	}
}

bk.easeInOutQuad = function (t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t + b; return -c/2 * ((--t)*(t-2) - 1) + b; }
bk.easeOutQuint = function (t, b, c, d) { return c*((t=t/d-1)*t*t*t*t + 1) + b; }
bk.easeInBack = function (t, b, c, d, s) { if (typeof s == "undefined") s = 1.70158; return c*(t/=d)*t*((s+1)*t - s) + b; }
bk.easeOutBack = function (t, b, c, d, s) { if (typeof s == "undefined") s = 1.7; return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b; };
bk.easeInOutBack = function (t, b, c, d, s) { if (typeof s == "undefined") s = 1.7; if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.5))+1)*t - s)) + b; return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; }

/*
This 'tick' function gets called by intervalTimer every 'freq' ms.  However, it adjusts the 'tick' as necessary based upon the current time.
In theory if the freq of the tick was 100ms, and the duration was 1000ms (one second), there should be 10 total ticks.  But, based upon 
browser rendering speed, for instance, it could take 1500+- ms to call 'tick' 10 times.  Therefore, if we DIDN'T adjust the tick based
upon the current time, some operation that was to take one second could take more than one second.  This method will not guarantee the 
number of ticks, but does guarantee that the operation gets completed in the alloted time, whether or it is 10 or fewer ticks.
*/
easeTick = function(id, ndx)
{
	var o = document.getElementById(id);
	var n;
	var s;

	if (o) {
		var eo = o.easeObj[ndx];
		var ttlMs  = ((new Date()) - 0) - eo.msStart;
		eo.tick = ttlMs / eo.freq;
		if (eo.tick < eo.maxTicks) {
			eo.func(o,eo);
		} else {
			clearInterval(eo.intervalTimer);
			eo.intervalTimer = 0;
			eo.inProgress = 0;
			eo.tick = 0;
			if (typeof eo.end != "undefined") {
				eo.func(o,eo);
			}
			eo.endFunc();
		}
	}
}

bk.setFade = function(o,eo)
{
	var n;

	if (o) {
		if (eo.inProgress) {
			eval("n = "+eo.easeFunc+"(eo.tick, eo.start, eo.distance, eo.maxTicks)");
			if (document.all) {
				o.filters.alpha.opacity = n;
			} else {
				o.style.MozOpacity = Math.abs(n / 100); 
			}
		} else {
			if (document.all) {
				o.filters.alpha.opacity = eo.end;
			} else {
				o.style.MozOpacity = Math.abs(eo.end / 100); 
			}
		}
	}
}

bk.fade = function(obj, start, end, ms)
{
	var ndx = "fader";
	var eo;

	if (typeof obj == "string") {
		obj = document.getElementById(obj);
	}

	if (typeof obj.easeObj == "undefined") {
		obj.easeObj = new Array();
	}

	eo = obj.easeObj[ndx] = new easeMotion(obj, ndx, bk.easeInOutQuad, ms, 35, bk.setFade);
	eo.start = start;
	eo.end = end;
	eo.distance = end - start;

	eo.startTimer();
}

bk.setXY = function(o, eo)
{
	var x;
	var y;
	if (o) {
		if (eo.inProgress) {
			eval("x = "+eo.easeFunc+"(eo.tick, eo.xStart, eo.xDistance, eo.maxTicks)");
			eval("y = "+eo.easeFunc+"(eo.tick, eo.yStart, eo.yDistance, eo.maxTicks)");

			o.style.left = x + "px";
			o.style.top = y + "px";
		} else {
			o.style.left = eo.xEnd + "px";
			o.style.top = eo.yEnd + "px";
		}
	}
}

bk.moveTo = function (obj, x, y, duration, freq, easeFunc)
{
	if (typeof obj == "string") { obj = document.getElementById(obj); }
	if (typeof duration == "undefined") { duration = 700; }
	if (typeof freq == "undefined") { freq = 35; }
	if (typeof easeFunc == "undefined") { easeFunc = bk.easeInOutQuad; }
	
	var curX = parseInt(obj.style.left);
	var curY = parseInt(obj.style.top);
	var eo;

	if ((curX==x) && (curY==y)) {
		return false;
	}

	if (obj && ((typeof obj.inProgress == "undefined")) || (obj.inProgress==0)) {
		if (typeof obj.easeObj == "undefined") {
			obj.easeObj = new Array();
		}
		eo = obj.easeObj['xy'] = new easeMotion(obj, 'xy', easeFunc, duration, freq, bk.setXY);

		eo.xStart = curX;
		eo.xEnd = x;
		eo.xDistance = eo.xEnd - eo.xStart;
		
		eo.yStart = curY;
		eo.yEnd = y;
		eo.yDistance = eo.yEnd - eo.yStart;

		eo.startTimer();
	}
	return false;
}

