var textFade = Class.create({
	initialize: function(config) {
		this.id = config.id;
		if (!this.id) throw new Error('TextFade Error: Element of id not found!');
	
		this.freeze = config.freeze;
		this.duration = config.duration;
		this.content = config.content;
		this.finish = config.finish;
		this.position = 0;
		this.skipped = false;
		this.started = false;
		$(this.id).update("<div id=\"" + this.id + "_textFade0\">&nbsp;</div>" +
						  "<div id=\"" + this.id + "_textFade1\">&nbsp;</div>");
		$(this.id + "_textFade"+this.position).update(this.content[0]);
		this.resizeElement();
	},
	
	resizeElement: function() {
		$(this.id + "_textFade1").setStyle({
			'position': 'relative',
			'bottom': $(this.id + "_textFade0").getHeight()+'px'
		});
		
		/* even if the id is not determine in CSS 
		 * $(this.id).setStyle({
		 *	'height': $(this.id + "_textFade"+(this.position%2)).getHeight()+'px',
		 *	'overflow': 'hidden'
		 *});
		 */
	},
	
	fadeInOut: function(a,b) {
		new Effect.Parallel(
			[
			 	new Effect.Fade(a,{sync:true,from:0,to:1}),
			 	new Effect.Appear(b,{sync:true,from:1,to:0})
			], {
				duration: this.duration
			}
		);
	},
	
	switchText: function() {
		if((this.content.length-1) != this.position) {
			++this.position;
			$(this.id + "_textFade"+(1-(this.position%2))).update(this.content[this.position]);
			this.resizeElement();
			this.fadeInOut($(this.id + "_textFade"+(1-(this.position%2))),$(this.id + "_textFade"+(this.position%2)));
			return true;
		} else {
			if(!this.skipped)
				this.skip();
			return false;
		}
	},
	
	start: function() {
		if(!this.started) {
			this.switchText();
			this.started = true;
			var obj = this;
			var pe = new PeriodicalExecuter(function(){
				if(!obj.switchText())
					this.stop();
			},this.freeze);
		} else {
			throw new Error('TextFade Error: TextFade is already started!');
		}
	},
	
	skip: function() {
		this.skipped = true;
		this.position = this.content.length-1;
		$("skip").fade({duration:this.duration,from:1,to:0})
		$(this.id).fade({duration:this.duration,from:1,to:0})
		this.finish();
	}
})
