// init vars
var vuz_textFader_list = Array();
document.addEvent('domready',function(){


	$$('.vuz_textFader').each(function(item) {
		initVuzReader(item);
	});
});


function initVuzReader(vuzReaderDiv) {
	var test = new vuz_textFader(vuzReaderDiv,vuzReaderDiv.get('options'));
}

// class def
var vuz_textFader = new Class({
	Implements: [Options,Events],
	options: {
		delay: 1000
	},
	initialize: function(_rootdiv, the_options){
		this.rootdiv = _rootdiv;
		this.activeLine = 0;
		// add the lines
		this.lines = Array();
		this.rootdiv.getElements('ul > li').each(function(liItem) {
			this.lines.include(liItem.innerHTML);
		},this);

		// empty the div
		this.rootdiv.empty();

		

		// add div for txt
		this.textdiv = new Element('div',{'class':'vuz_textFader_text'});
		this.textdiv.inject(this.rootdiv);

		// set options 
		this.setOptions(JSON.decode(the_options));
		
		// enable annimation
		this.fx = new Fx.Tween(this.textdiv,{duration: 1000});
		// set initialtext
		this.textdiv.innerHTML = this.lines[0];
		// add events
		this.rootdiv.addEvent('mouseenter',function(){
			if($chk(this.fadeTimer)) {
				this.blocked = true;
			}
		}.bind(this));
		this.rootdiv.addEvent('mouseleave',function(){
			if($chk(this.fadeTimer)) {
				this.blocked = false;
			}
		}.bind(this));
		this.doAnimation();

    },

	doAnimation: function() {
		if(!this.blocked) {
			// calc activeLine
			this.activeLine += 1;
			if(this.activeLine >= this.lines.length) { this.activeLine = 0; }

			// do animation
			//this.rootdiv.fade('out');
			

			this.fx.start('opacity',0).chain(function(){
				this.textdiv.innerHTML = this.lines[this.activeLine];
				this.fx.start('opacity',0.99);
			}.bind(this)).chain(function() {
				this.fadeTimer = this.doAnimation.bind(this).delay(this.options.delay);
			}.bind(this));
		} else {
			this.fadeTimer = this.doAnimation.bind(this).delay(this.options.delay);
		}
	}
})