/*
moo.fx, simple effects library built with prototype.js (http://prototype.conio.net).
by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE.
for more info (http://moofx.mad4milk.net).
Sunday, March 05, 2006
v 1.2.3
*/

var fx = new Object();
//base
fx.Base = function(){};
fx.Base.prototype = {
	initialize: function(el, options) {
		this.el = $(el);
//		this.el.style.overflow = "hidden";
		this.now = 1;
		this.initValue = this.getValue();  
		this.now = this.initValue;
		this.options = {
			duration: 500,
			onComplete: '',
			transition: fx.sinoidal
		}
		this.setOptions(options);
	},

	setOptions: function(options) { 
		Object.extend(this.options, options || {});
	},

	step: function() {
		var time  = (new Date).getTime();
		if (time >= this.options.duration+this.startTime) {
			this.now = this.to;
			clearInterval (this.timer);
			this.timer = null;
			if (this.options.onComplete) {
				setTimeout(this.options.onComplete.bind(this), 10);
			}
		}
		else {
			var Tpos = (time - this.startTime) / (this.options.duration);
			this.now = this.options.transition(Tpos) * (this.to-this.from) + this.from;
		}
		this.setValue(this.now);
	},

	custom: function(from, to) {
		if (this.timer != null) return;
		this.from = from;
		this.to = to;
		this.startTime = (new Date).getTime();
		this.timer = setInterval (this.step.bind(this), 13);
	},

	set: function(to) {
		if (this.timer != null) return;
		this.now = to;
		this.setValue(this.now);
	},

	hide: function() {
		this.clearTimer();
		this.now = 0;
		this.setValue(this.now);
	},

	clearTimer: function() {
		if (this.timer != null)	{
			clearInterval(this.timer);
			this.timer = null;
		}
	},

	toggle: function() {
		if (this.getValue() > 0) this.custom(this.getValue(), 0);
							else this.custom(0, this.initValue);
	},

	getValue: function() {
		// abstracte Methode !
		return 0;
	},

	setValue: function(val) {
		// abstracte Methode !
	},

	debug:function(text) {
		if ($('debug')) {
			var innerHTML = $('debug').innerHTML;
			$('debug').innerHTML = text + '<br>' + innerHTML;
		}
	}

}

//stretchers
fx.Height = Class.create();
Object.extend(Object.extend(fx.Height.prototype, fx.Base.prototype), {	
	setValue: function(val) { this.el.style.height = val + "px"; },
    getValue: function()    { return this.el.offsetHeight; }
});

fx.Width = Class.create();
Object.extend(Object.extend(fx.Width.prototype, fx.Base.prototype), {	
	setValue: function(val) { this.el.style.width = val + "px"; },
    getValue: function()    { return this.el.offsetWidth; }
});

//movers
fx.Left = Class.create();
Object.extend(Object.extend(fx.Left.prototype, fx.Base.prototype), {	
	setValue: function(val) { this.el.style.left = val + "px"; },
    getValue: function()    { return this.el.offsetLeft; }
});

fx.Top = Class.create();
Object.extend(Object.extend(fx.Top.prototype, fx.Base.prototype), {	
	setValue: function(val) { this.el.style.top = val + "px"; },
    getValue: function()    { return this.el.offsetTop; }
});

//fader

fx.Opacity = Class.create();
Object.extend(Object.extend(fx.Opacity.prototype, fx.Base.prototype), {	
	
	setValue: function(opacity) {

		if (opacity == 0 && this.el.style.visibility != "hidden") {

			this.el.style.visibility = "hidden";
			if (window.ActiveXObject) this.el.style.setAttribute('filter', 'alpha(opacity=0)', false);
			this.el.style.opacity = null;

		} else 
			
		if (opacity == 1) {

			if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
			if (window.ActiveXObject) this.el.style.setAttribute('filter', 'alpha(opacity=100)', false);
			this.el.style.opacity = null;

		} else {

			if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
			if (window.ActiveXObject) this.el.style.setAttribute('filter', 'alpha(opacity=' + opacity*100 + ')', false);
			this.el.style.opacity = opacity;

		}

	},

	getValue: function() {
		return this.now?this.now:1;
	}

});


//transitions
fx.sinoidal = function(pos){
	return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	//this transition is from script.aculo.us
}
fx.linear = function(pos){
	return pos;
}
fx.cubic = function(pos){
	return Math.pow(pos, 3);
}
fx.circ = function(pos){
	return Math.sqrt(pos);
}
fx.sinopow2 = function(pos){
	return Math.pow( ((-Math.cos(pos*Math.PI)/2) + 0.5) ,2);
}
fx.sinopow3 = function(pos){
	return Math.pow( ((-Math.cos(pos*Math.PI)/2) + 0.5) ,3);
}
fx.sinosqrt = function(pos){
	return Math.pow( ((-Math.cos(pos*Math.PI)/2) + 0.5) ,0.5);
}
