/**
 * @author Marcel Fiedrich
 */

var newsMorph = function() {
	
	// Configuration Vars
	var periodicalTime = 5000;
	var transitionTime = 1250;
	var newsObjs = $$('.newsObj');
	var newsList = $('newsHomeLatestContainer');
	
	if ($chk(newsList) && newsObjs.length > 1) {
	
		var midContentHeight = new function(){
			var midContent = $('midContent');
			var stageContent = $('loeweStageContent');
			var objectHeightMid;
			var objectHeightStage;
			
			objectHeightStage = stageContent.getStyle('height').toInt();
			objectHeightStage = objectHeightStage - 15;
			objectHeightMid = midContent.getStyle('height').toInt();
			
			if (objectHeightMid < objectHeightStage) {
				midContent.setStyles({
					'min-height': objectHeightStage
				});
			}
		}
		
		if ($chk(newsList)) {
		
			//Starting the part of the news switcher
			
			newsObjs.each(function(element, index){
			
				//since the viewer obviously has javascript on, we can remove the 'firstObj' class
				if (index == 0) {
					element.removeClass('firstObj');
					element.setStyle('display', "block");
				}
				else {
					element.setStyle('display', "none");
					element.setStyle('opacity', "0");
					
				}
				
			});
			
			
			var newsFaderFunc = new function(){
			
				var numObjs = newsObjs.length;
				var objNum = 0;
				
				var fx = function(){
				
					var curObj = newsObjs[objNum];
					
					if (objNum < (numObjs - 1)) {
						objNum++;
					}
					else {
						objNum = 0;
					}
					
					var newObj = newsObjs[objNum];
					
					var fadeIn = new Fx.Morph(newObj, {
						duration: transitionTime,
						transition: Fx.Transitions.Quad.easeInOut,
						wait: false
					}).addEvent('onStart', function(){
						newObj.setStyle('display', "block");
					});
					
					var fadeOut = new Fx.Morph(curObj, {
						duration: 500,
						transition: Fx.Transitions.Quad.easeInOut,
						wait: false
					}).addEvent('onComplete', function(){
						curObj.setStyle('display', "none");
					});
					
					fadeOut.start({
						'opacity': [0]
					});
					fadeIn.start({
						'opacity': [0, 1]
					});
					
				};
				var periodical;
				periodical = fx.periodical(periodicalTime, this);
				newsList.addEvent('mouseenter', function(){
					$clear(periodical);
				});
				newsList.addEvent('mouseleave', function(){
					periodical = fx.periodical(periodicalTime, this);
				});
			}
		}
	}
};