/*
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 = {
	setOptions: function(options) {
	this.options = {
		duration: 500,
		onComplete: '',
		transition: fx.sinoidal
	}
	Object.extend(this.options, options || {});
	if(options && typeof options.duration != "undefined" && options.duration == 0)
		this.options.duration = 0;
	},

	init: function(el, val, tog, options)
	{
		if(el) this.el = $(el);
		this.now = this.ini = val;
		if(tog != null) this.tog = tog;
		else this.tog = val;
		this.setOptions(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.increase();
	},

	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);
	},

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

	toggle: function() {
		if (this.now > 0) this.goTo(0);
		else this.goTo(this.tog);
	},

	reset: function() {
		this.goTo(this.ini);
	},

	setTo: function(to) {
		this.now = to;
		this.increase();
		if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
	},

	goTo: function(to) {
		this.clearTimer();
		this.custom(this.now, to);
	},

	setBy: function(by) {
		this.setTo(this.now + by);
	},

	goBy: function(by) {
		this.goTo(this.now + by);
	},

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

//stretchers
fx.Height = Class.create();
fx.Height.prototype = Object.extend(new fx.Base(), {
	increase: function() {
		if(this.now >= 0) this.el.style.height = this.now + "px";
	},

	initialize: function(el, options) {
		this.el = $(el);
		this.init(null, this.el.offsetHeight, this.el.scrollHeight, options);
		this.el.style.overflow = "hidden";
	}
});

fx.Width = Class.create();
fx.Width.prototype = Object.extend(new fx.Base(), {
	increase: function() {
		if(this.now >= 0) this.el.style.width = this.now + "px";
	},

	initialize: function(el, options) {
		this.el = $(el);
		this.init(null, this.el.offsetWidth, this.el.scrollWidth, options);
		this.el.style.overflow = "hidden";
	}
});

//movers
fx.Left = Class.create();
fx.Left.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		Element.makePositioned(this.el);
		this.el.style.left = Element.getStyle(this.el, 'left') || "0px";
		this.init(null, parseInt(this.el.style.left), null, options);
	},

	hide: null,

	increase: function() {
		this.el.style.left = this.now.toFixed(3) + "px";
	}
});

fx.Top = Class.create();
fx.Top.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		Element.makePositioned(this.el);
		this.el.style.top = Element.getStyle(this.el, 'top') || "0px";
		this.now = this.iniTop = parseInt(this.el.style.top);
		this.init(null, parseInt(this.el.style.top), null, options);
	},

	hide: null,

	increase: function() {
		this.el.style.top = this.now.toFixed(3) + "px";
	}
});

//fader
fx.Opacity = Class.create();
fx.Opacity.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.init(el, 1, 1, options);
		this.increase();
	},

	increase: function() {
		if (this.now == 1 && (/Firefox/.test(navigator.userAgent))) this.now = 0.9999;
		this.setOpacity(this.now);
	},

	setOpacity: function(opacity) {
		if (opacity == 0 && this.el.style.visibility != "hidden") this.el.style.visibility = "hidden";
		else if (this.el.style.visibility != "visible") this.el.style.visibility = "visible";
		if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + opacity*100 + ")";
		this.el.style.opacity = opacity;
	}
});

//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);
}
