boxFaders = { };

boxFader = function(box) {
	this.box = box;
	this.id = box.id;
	this.css = this.box.style;
	this.hide();
	boxFaders[this.id] = this;
};

boxFader.getInstance = function(id) {
	var box = boxFaders[id];
	if (box == null) box = boxFaders[id] = new boxFader(document.getElementById(id));
	return box;
};

boxFader.prototype = {
	mode : false,
	alpha : 1,
	timer : 0,
	opacity : false,
	speed : 100,
	begin : 0,
	end : 0,
	rate : 0,

	fadeIn : function(time) {
		var now = new Date();
		this.begin = now.getTime();
		this.end = time * 1000;
		this.rate = Math.PI / (2 * this.end);
		this.setMode("in");
		this.show();
		this.setAlpha(1);
		this.setTimer("start", this.speed);
	},
	onFadeIn : function() {},

	fadeOut : function(time) {
		var now = new Date();
		this.begin = now.getTime();
		this.end = time * 1000;
		this.rate = Math.PI / (2 * this.end);
		this.setMode("out");
		this.setAlpha(100);
		this.setTimer("start", this.speed);
	},
	onFadeOut : function() {},

	fade : function(fade, pause) {
		this.onFadeIn = function() { this.pause(pause); };
		this.onResume = function() { this.fadeOut(fade); };
		this.onFadeOut = function() {
			this.onFadeIn = function() {};
			this.onResume = function() {};
			this.onFade();
		}
		this.fadeIn(fade);
	},
	onFade : function() {},

	start : function() {
		var now = new Date();
		if (this.getMode("in")) {
			var alpha = this.alpha * Math.sin(this.rate * (now.getTime() - this.begin)) + this.alpha;
			if (this.setAlpha(alpha) == 100) {
				this.clearTimer();
				this.setMode(false);
				this.onFadeIn();
			};
		}
		else if (this.getMode("out")) {
			var alpha = this.alpha * Math.cos(this.rate * (now.getTime() - this.begin)) + this.alpha;
			if (this.setAlpha(alpha) == 0) {
				this.clearTimer();
				this.setMode(false);
				this.hide();
				this.onFadeOut();
			};
		};
	},
	pause : function(time) {
		this.setMode("pause");
		this.setTimer("resume", time * 1000);
	},
	resume : function() {
		this.clearTimer();
		this.setMode(false);
		this.onResume();
	},
	onResume : function() {},

	setMode : function(mode) {
		if (!mode) mode = false;
		this.mode = mode;
	},
	getMode : function(mode) {
		return (mode) ? (this.mode == mode) : this.mode;
	},

	setAlpha : function(alpha) {
		if (alpha && alpha > 100) alpha = 100;
		if (alpha && alpha < 0) alpha = 0;
		if (alpha) this.alpha = Math.round(alpha);
		if (this.getOpacity() == "none") return this.alpha;
		this.css[this.opacity] = (this.opacity == "filter") ? "alpha(opacity=" + this.alpha + ")" : (this.alpha / 100);
		return this.alpha;
	},
	getOpacity : function() {
		if (!this.opacity) {
			if (typeof this.css.MozOpacity != "undefined") this.opacity = "MozOpacity";
			else if (typeof this.css.KhtmlOpacity != "undefined") this.opacity = "KhtmlOpacity";
			else if (typeof this.css.filter != "undefined") this.opacity = "filter";
			else if (typeof this.css.opacity != "undefined") this.opacity = "opacity";
			else this.opacity = "none";
		};
		return this.opacity;
	},

	show : function() {
		this.css.display = "block";
	},
	hide : function() {
		this.css.display = "none";
	},

	setTimer : function(execute, time) {
		this.timer = setInterval("boxFaders['" + this.id + "']." + execute + "()", time);
	},
	clearTimer : function() {
		clearInterval(this.timer);
	}
};

boxFade = function() {
	if (document.getElementById) {
		var i = 0, box = new Array(3);
		for (; i < 3; i++) box[i] = boxFader.getInstance("fade_" + (i + 1));

		box[0].onFade = function() { box[1].fade(5, 6); };
		box[1].onFade = function() { box[2].fade(5, 6); };
		box[2].onFade = function() { box[0].fade(5, 6); };
		box[0].fade(5, 6);
	};
};

boxFade();
