var newsChangeTime = 5; //seconds
var newsDuration = 1; //seconds

function init(){
	new NewsToggle("change");
}

//Intervall-Klasse für zeitgesteuerte Funktionen
var Interval = Class.create({
	// handler -> function
	initialize: function(handler){
		if(typeof handler == 'function'){
			var index = 0;
			var handle = window.setInterval(
				function(){
					try {
						handler();
					}catch(e){}
				},
				(newsDuration + newsChangeTime) * 1000
			);
		}
	}
});

//Klasse für den Durchlauf von Nachrichten
var NewsFade = Class.create({

	initialize: function(rootClass) {
		var searchElement = $$("."+rootClass)[0];
		 
		if(searchElement){
			this.rootElement = searchElement.select(".image_change")[0].up();
			
			this.initVariables();
			
			//Nur bei mehr als 3 Einträgen ausführen
			if(this.newsElements.length > 1){
				this.pushEntries();
			}else{
				return null;
			}
		}else{
			return null;
		}
	},
	
	initVariables: function(){
		//Code von leeren Links (ohne href) bereinigen
		this.rootElement.select("a:not([HREF])").each(function(e) {
			e.remove();
		});
		
		this.getRootChilds();
	},
	
	getRootChilds: function() {
		this.newsElements = this.rootElement.childElements();
	},
	
	pushEntries: function() {
		var newsFade = this;
		
		new Interval(function(){
			var firstEl = newsFade.newsElements[0];
			Effect.SlideUp(firstEl, {
				duration: newsDuration,
				afterFinish: function(){
					firstEl = firstEl.remove();
					newsFade.rootElement.appendChild(firstEl);
					firstEl.show();
					newsFade.getRootChilds();
				}
			});
		});
		
	}
});

//Klasse für den Durchlauf von Nachrichten
var NewsToggle = Class.create({

	initialize: function(rootClass) {
		var searchElement = $$("."+rootClass)[0];
		 
		if(searchElement){
			this.rootElement = searchElement.select(".image_change")[0].up();
			
			this.initVariables();
			
			//Nur bei mehr als einem Eintrag ausführen
			if(this.newsElements.length > 1){
				this.pushEntries();
			}else{
				return null;
			}
		}else{
			return null;
		}
	},
	
	initVariables: function(){
		//Code von leeren Links (ohne href) bereinigen
		this.rootElement.select("a:not([HREF])").each(function(e) {
			e.remove();
		});
		
		this.getRootChilds();
	},
	
	getRootChilds: function() {
		this.newsElements = this.rootElement.childElements();
	},
	
	pushEntries: function() {
		var newsFade = this;
		
		new Interval(function(){
			var firstEl = newsFade.newsElements[0];
			Effect.SlideUp(firstEl, {
				duration: newsDuration,
				afterFinish: function(){
					firstEl = firstEl.remove();
					newsFade.rootElement.appendChild(firstEl);
					firstEl.show();
					newsFade.getRootChilds();
				}
			});
		});
		
	}
});
    
Event.observe(window, "load", init);

