/*!************************** rbmFX.js *******************************
**** Version 1.02.0001
**** last changed 20.03.2008
**** (c) 2007, 2008 Bernd Rudolf, rbm digitaltechnik, www.rbm.de
*********************************************************************/
//
// 19.03.2008	scrollTo() added
// 20.03.2008	fade(): disVis -1 for not turning off the visibility/display added
//
//********************************************************************


var rbmFX = {

	version: "1.01.0001",

	fxFunction: function(x, t, xMax)	{
//		x = x + step;
		x = (x > xMax) ? xMax : x;

		var y;
		switch(t)	{
			case 1:			//linear
					y = x;
					break;
			case 2:			//quadratisch
					y = - Math.round(Math.pow(((x / 10) - 10), 2)) + 100;
					break;
			case 3:			//kubisch
					y = Math.round(Math.pow(((x / 18) - 4.65), 3)) + 99;	//((x / 18) - 4.65) ^ 3 + 100
					break;
			case 4:			//x^4
					y = - Math.round(Math.pow(((x / 26) - 3.15), 4)) + 100;			//-((x / 24) - 3.15)^4 + 100
					break;
			case 5:			//1/x
					y = - Math.round(100 / (x + 1)) + 101; 							//	- (100 / (x + 1)) + 100
					break;
			case 6:			//sin
					y = Math.round(100 * Math.sin(x / 60));
					break;
				
		}
		return y;
	}
	
}


rbmFX.fade = function(element, d, max, s, transition, duration)	{	//d: direction, s = step, max = %,  s, transition and duration are options

	this.parent = $fx(element);
	this.d = d || -1;
	this.xMax = max || 100;
	this.step = s || 4;
	this.t = transition || 2;
	this.duration = duration || 500;
	this.timeStep = 10;
	this.delay = 0;
	this.disVis = 0;	// 0 = set/clear display, 1 = set/clear visibility; -1 = do nothing
	

	this.init = function()	{
		this.fxx = 0;
//XXX opacity ermitteln??		
		this.start = (this.d == 1) ? 0 : this.xMax;
		this.timeStep = Math.round(this.duration / (this.xMax / this.step));
	}

	this.init();
	

	this.delayed = function(delay)	{
		this.delay = delay || this.delay;
		this.delaytimer = window.setTimeout(this.go.bind(this, this.d), this.delay)
	}

	this.go = function(d)	{
		if(!this.active || d != this.d)	{
			this.d = d || this.d;	
			//only fade if status and direction match!!
			if(((this.d == 1) && ((typeof(this.op) == "undefined") || (this.op <= 0))) || (this.d == -1) && (this.op > 0))	{
				this.active = true;
				this.init();
				this.onStart();
		
				if(typeof(this.parent.visibility) == "undefined" || this.parent.visibility == 0)	{
					this.parent.show(true);
				}
				
				this.fading();
			}
		}
	}

	this.toggle = function()	{
		this.d = (this.op != "undefined") ? ((this.op > 0) ? -1 : 1) : -1;
		this.init();
		this.go();
			
	}
		
	this.fading = function()	{
		if(this.fxx < this.xMax)	{
			this.op = this.start + this.d * rbmFX.fxFunction((this.fxx += this.step), this.t, this.xMax);		//this.margin + d * this.step
			this.parent.setOpacity(this.op);
			this.timer = window.setTimeout(this.fading.bind(this, d), this.timeStep);
		}
		else	{
			this.onEnd();
			this.active = false;
			if(this.op <= 0)	 {
				switch(this.disVis)	{
					case 0:
						this.parent.show(false);
						break;
					case 1:
						this.parent.setDisplay(false);
						break;
					case -1:
						break;
//				(this.disVis = 1) ? this.parent.show(false) : this.parent.setDisplay(false);
					
				}
			}
		}
	}

	this.onStart = function() { return null}

	this.onEnd = function() { return null}
	
}



rbmFX.size = function(element, w, h, s, transition, duration)	{	//s, transition and duration are options
	
	this.parent = $fx(element);
	this.w = w;
	this.h = h;
	this.xMax = 100;
	this.step = s || 4;
	this.t = transition || 2;
	this.duration = duration || 1000;
	this.timeStep = 10;
	this.twin = null;			//points to  an element that will be sized in opposite direction simultaneously
	
	this.init = function()	{
		this.fxx = 0;
		this.xMax = 100;
		this.delta = this.w - this.parent.offsetWidth;
		this.delta2 = this.h - this.parent.offsetHeight;
		this.start = this.parent.offsetWidth;
		this.start2 = this.parent.offsetHeight;
		this.timeStep = Math.round(this.duration / (this.xMax / this.step));
		if(this.twin)	{
			this.twin.start = this.twin.offsetWidth;
			this.twin.start2 = this.twin.offsetHeight;
		}
	}
	
	this.delayed = function(d)	{
		d = d || this.delay;
		this.delaytimer = window.setTimeout(this.go.bind(this), d)
	}
	
	this.go = function()	{
		if(!this.active)	{
			this.active = true;
			this.onStart();
			this.init();
			this.sizing();
		}
	}

	this.sizing = function()	{
		if(this.fxx < this.xMax)	{
			var y = rbmFX.fxFunction((this.fxx += this.step), this.t, this.xMax);
			var dw = Math.round(this.delta * y / 100);		//this.margin + d * this.step
			var dh = Math.round(this.delta2 * y/ 100);		//this.margin + d * this.step
			this.w = this.start + dw;
			this.h = this.start2 + dh;
			if(this.twin)	{
				this.twin.size(this.twin.start - dw, this.twin.start2 - dh);
			}
			this.parent.size(this.w, this.h);
			//this.sizing();
			this.timer = window.setTimeout(this.sizing.bind(this), this.timeStep);
		}
		else	{
			this.onEnd();
			this.active = false;
		}
	}

	this.onStart = function() { return null}

	this.onEnd = function() { return null}

}

rbmFX.shift = function(element, d, s, transition, duration)	{	//s, transition and duration are options

	this.parent = $fx(element);
	this.d = d || -1;
	this.xMax = 100;
	this.step = s || 4;
	this.t = transition || 4;
	this.duration = duration || 500;
	this.timeStep = 10;



	this.init = function()	{	
		this.delta = this.parent.parentNode.offsetWidth ;
		this.margin = 0;
		this.timeStep = Math.round(this.duration / (this.xMax / this.step));
	}

	this.init();

	this.delayed = function(d)	{
		d = d || this.delay;
		this.delaytimer = window.setTimeout(this.go.bind(this), d)
	}
	
	this.go = function(d)	{
		if(!this.active)	{
			this.d = d || this.d;
			this.fxx = 0;
			this.xMax = 100;
			this.start = this.margin;
			this.active = true;
			this.onStart();
			if(( this.d == -1 && this.start > -1 * (this.parent.offsetWidth - this.delta - 10) || (this.d == 1 && this.start < 0))) {	//nur shiften, wenn letzes Bild noch nicht erreicht
				this.shifting(d);
			}
			else	{
				this.onEnd();
				this.active = false;
			}			
		}
	}


	this.shifting = function(d)	{
		if(this.fxx < this.xMax)	{
			this.margin = this.start + this.d * this.delta * rbmFX.fxFunction((this.fxx += this.step), this.t, this.xMax) / 100;		//this.margin + d * this.step
			this.parent.style["marginLeft"] = this.margin + "px";
			this.timer = window.setTimeout(this.shifting.bind(this, d), this.timeStep);
		}
		else	{
			this.onEnd();
			this.active = false;
		}
	}

	this.onStart = function() { return null}

	this.onEnd = function() { return null}

}


rbmFX.scrollTo = function(element, d, max, s, transition, duration)	{	//d: direction, s = step, max = pixel,  s, transition and duration are options

	this.parent = $fx(element);
	this.d = d || -1;
	this.xMax = 100;
	this.max = max;
	this.step = s;
	this.t = transition || 2;
	this.duration = duration || 500;
	this.timeStep = 10;
	this.delay = 0;
	this.myScroll = 0;
	

	this.init = function()	{
		this.fxx = 0;
		this.start = (this.d == 1) ? 0 : this.max;
		this.timeStep = Math.round(this.duration / (this.xMax / this.step));
		t.debugprint("timstep " + this.timeStep);
	}

	this.init();
	

	this.delayed = function(delay)	{
		this.delay = delay || this.delay;
		this.delaytimer = window.setTimeout(this.go.bind(this, this.d), this.delay)
	}

	this.go = function(d)	{
		if(!this.active || d != this.d)	{
			this.d = d || this.d;	
			//only scroll if direction matches!!
//			if(((this.d == 1) && ((typeof(this.op) == "undefined") || (this.op <= 0))) || (this.d == -1) && (this.op > 0))	{
				this.active = true;
				this.init();
				this.onStart();
		
				
				this.scrolling();
//			}
		}
	}


	this.scrolling = function()	{
		if(this.fxx < this.xMax)	{
			this.myScroll = this.start + this.d * this.max * rbmFX.fxFunction((this.fxx += this.step), this.t, this.xMax) / 100;		//this.margin + d * this.step
t.debugprint(this.myScroll);
//left right?
			this.parent.scrollTop = this.myScroll;
			this.timer = window.setTimeout(this.scrolling.bind(this, d), this.timeStep);
		}
		else	{
			this.onEnd();
			this.active = false;
		}
	}

	this.onStart = function() { return null}

	this.onEnd = function() { return null}
	
}




/*
		element.slide = function(d)	{	// d = + 1: right; d = -1 : left
			if(this.fxx == 100 ||  this.fxx == 0)	{	//do not reenter function
				this.fxx = 0;
				this.xMax = 100;
				this.delta = this.parentNode.offsetWidth ;
				this.start = this.margin;
				this.sliding(d);
			}
		}
		
		element.sliding = function(d)	{
			if(this.fxx < this.xMax)	{
				this.margin = this.start + this.delta * rbmFX.fxFunction((this.fxx += this.step), this.t, this.xMax) / 100;		//this.margin + d * this.step
				this.style["marginLeft"] = this.margin + "px";
				if (this.timeStep > 0)
					this.timer = window.setTimeout(this.sliding.bind(this, d), this.timeStep);
				else
					this.sliding(d);
			}
		}

*/