/*
bezier.js
based on http://timotheegroleau.com/Flash/experiments/easing_function_generator.htm
awesome stuff dude!
USE:
var myBezier; // global declaration
myBezier = new Bezier(); // so other scripts can use the bezier stuff
*/
var myBezier;

/* attach a function to it's object, to be able to use it from other functions */
function bind(obj, func){
	return function() { func.call(obj); }
}

function Bezier () {
	/*
	t = stepcounter
	b = start position
	c = distance
	d = how many steps
	*/
	this.no_easing = function (t, b, c, d) {
		t/=d;
		return b+c*(t);
	}
	this.in_out_cubic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(-2*tc + 3*ts);
	}
	this.in_out_quintic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(6*tc*ts + -15*ts*ts + 10*tc);
	}
	this.in_quintic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(tc*ts);
	}
	this.in_quartic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		return b+c*(ts*ts);
	}
	this.in_cubic = function (t, b, c, d) {
		var tc=(t/=d)*t*t;
		return b+c*(tc);
	}
	this.in_quadratic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		return b+c*(ts);
	}
	this.out_quintic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(tc*ts + -5*ts*ts + 10*tc + -10*ts + 5*t);
	}
	this.out_quartic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(-1*ts*ts + 4*tc + -6*ts + 4*t);
	}
	this.out_qubic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(tc + -3*ts + 3*t);
	}
	this.out_in_qubic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -6*ts + 3*t);
	}
	this.out_in_quartic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(6*tc + -9*ts + 4*t);
	}
	this.back_in_cubic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -3*ts);
	}
	this.back_in_quartic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(2*ts*ts + 2*tc + -3*ts);
	}
	this.out_back_cubic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(4*tc + -9*ts + 6*t);
	}
	this.out_back_quartic = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(-2*ts*ts + 10*tc + -15*ts + 8*t);
	}
	this.out_elastic_small = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var	tc=ts*t;
		return b+c*(33*tc*ts + -106*ts*ts + 126*tc + -67*ts + 15*t);
	}
	this.out_elastic_big = function(t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(56*tc*ts + -175*ts*ts + 200*tc + -100*ts + 20*t);
	}
	this.in_elastic_small = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(33*tc*ts + -59*ts*ts + 32*tc + -5*ts);
	}
	this.in_elastic_big = function (t, b, c, d) {
		var ts=(t/=d)*t;
		var tc=ts*t;
		return b+c*(56*tc*ts + -105*ts*ts + 60*tc + -10*ts);
	}
}

/* class to slide div's */
function DivSlider (div, startPos, distance) {
	this.div = document.getElementById(div);
	this.status = false;
	this.cStep = 0; // step counter
	this.steps = 25; // how many steps
	this.startPos = startPos;
	this.endPos = startPos + distance;
	this.distance = distance;
	
	this.slide = function () {
		if (this.cStep < this.steps) {
			this.div.style.overflow = "hidden";
			newpos = Math.floor(myBezier.out_qubic(this.cStep, this.startPos, this.distance, this.steps));
			this.div.style.height = newpos + "px";
			this.cStep++;
			window.setTimeout(bind(this, this.slide),25);
		} else {
			newpos = this.endPos;
			this.div.style.height = newpos + "px";
			// end slide, swap vars
			this.status = !this.status;
			this.cStep = 0;
			temp = this.startPos;
			this.startPos = this.endPos;
			this.endPos = temp;
			this.distance *= -1;
			if (this.status == false) {
				this.div.style.overflow = "visible";
			}
		}
	}	
}
function DivScroller (div) {
	this.div = document.getElementById(div);
	this.status = false;
	this.cStep = 0; // step counter
	this.steps = 25; // how many steps
	this.startPos = 0; // will be replaced with the offset it currently has
	this.endPos = 0; // will be replaced with target offset
	this.distance = 0; // will be replaced with the offset difference
	
	this.scrollTo = function (to) {
		this.startPos = this.div.offsetLeft;
		this.distance = to - this.startPos;
		this.endPos = to;
		this.status = true;
		this.slide();
	}
	this.slide = function () {
		if (this.cStep < this.steps) {
			newpos = Math.floor(myBezier.out_qubic(this.cStep, this.startPos, this.distance, this.steps));
			this.div.style.left = newpos + "px";
			this.cStep++;
			window.setTimeout(bind(this, this.slide),25);
		} else {
			this.div.style.left = this.endPos + "px";
			this.cStep = 0;
			this.status = false;
		}
	}	
}
function DivSliderXY (div,srcX,destX,srcY,destY) {
	this.div = document.getElementById(div);
	this.cStep = 0;
	this.steps = 25; // how many steps to reach target
	this.srcX = srcX;
	this.destX = destX;
	this.srcY = srcY;
	this.destY = destY;
	
	this.slide = function () {
		if (this.cStep < this.steps) {
			newXpos = Math.floor(myBezier.out_elastic_small(this.cStep, this.destX, this.srcX-this.destX, this.steps));
			this.div.style.left = newXpos + "px";
			newYpos = Math.floor(myBezier.out_elastic_small(this.cStep, this.destY, this.srcY-this.destY, this.steps));
			this.div.style.top = newYpos + "px";
			this.cStep++;
			window.setTimeout(bind(this, this.slide), 25);
		} else {
			this.div.style.left = this.srcX + "px";
			this.div.style.top = this.srcY + "px";
		}
	}
}

/* class to fade div's in and out */
function DivFader(div) {
	this.div = document.getElementById(div);
	this.cAlpha = 100; // currentAlpha
	this.useIEalpha = true; // set to false when you want to drag items outside the box (ie alpha breaks overflow when applying opacity through js)
	this.visi = false; // to use visibility, not display, so space remains taken. (usefull if the container has borders, so it doesnt instant hide border-taken space)
	
	this.chgAlpha = function (alpha) {
		this.div.style.opacity = alpha/100; // opera
		this.div.style.MozOpacity = alpha/100; // firefox
		this.div.style.KhtmlOpacity = alpha/100; // safari*/
		if (this.useIEalpha) {
			this.div.style.filter = "alpha(opacity=" + alpha + ")"; // internet explorer
		}
		
	}
	this.fadeOut = function () {
		if (this.cAlpha > 0) {
			this.cAlpha -= 4;
			this.chgAlpha(this.cAlpha);
			window.setTimeout(bind(this, this.fadeOut),25);
		} else {
			if (this.visi) {
				this.div.style.visibility = "hidden";
			} else {
				this.div.style.display = "none";
			}
		}
	}
	this.fadeIn = function () {
		if (this.cAlpha < 100) {
			if (this.visi) {
				this.div.style.visibility = "visible";
			} else {
				this.div.style.display = "block";
			}
			this.cAlpha += 4;
			this.chgAlpha(this.cAlpha);
			window.setTimeout(bind(this, this.fadeIn),25);
		} else {
			this.div.style.overflow = "visible";
		}
	}
	this.instantOut = function () {
		this.cAlpha = 0;
		if (this.visi) {
			this.div.style.visibility = "hidden";
		} else {
			this.div.style.display = "none";
		}
		this.div.style.opacity = 0;
		this.div.style.MozOpacity = 0;
		this.div.style.KhtmlOpacity = 0;
		this.div.style.filter = "alpha(opacity=0)";
	}
	this.instantIn = function () {
		this.cAlpha = 100;
		if (this.visi) {
			this.div.style.visibility = "visible";
		} else {
			this.div.style.display = "block";
		}
		this.div.style.opacity = 100;
		this.div.style.MozOpacity = 100;
		this.div.style.KhtmlOpacity = 100;
		this.div.style.filter = "alpha(opacity=100)";
	}
	this.useVisibility = function () {
		this.visi = true;
	}
	this.useDisplay = function () {
		this.visi = false;
	}
}