var showSubNavDelay = null;

function getRootCategory(subNavDiv){
	var parentId = 'rex-article-'+subNavDiv.id.split('_')[1];
	var levelDiv = subNavDiv.getParent('div');
	if(levelDiv.id == 'navigationLevel3'){
		parentId = getRootCategory($(parentId).getParent('div'));
	}
	return parentId;
}

function getParentCategory(subNavDiv){
	var parentId = 'rex-article-'+subNavDiv.id.split('_')[1];
	return parentId;
}

function getCategoryPosition(categoryId){
	var list = $(categoryId).getParent('ul');
	var listElements = list.getChildren('li');
	var num_listElements = listElements.length;
	var listEntry;
	for(var i=0;i<num_listElements;i++){
		listEntry = listElements[i];
		if(listEntry.id == categoryId){
			return i;
		}
	}
}

function changePage(){
	if(quoteInterval){
		clearInterval(quoteInterval);
	}
	var navigation = $('navigation');
	// alle Effekte entfernen
	navigation.removeEvents('mouseleave');
	$('logo').removeEvents('mouseover');
	$('visual').removeEvents('mouseover');
	$('content').removeEvents('mouseover');
	var listElements = $('navigation').getElements('li');
	listElements.each(
		function(listEntry){
			listEntry.removeEvents('mouseenter');
		}
	);
	
	var listEntry = this;
	var anchor = listEntry.getChildren('a')[0];
	var href = anchor.get('href');
	var rel = anchor.get('rel').split(',')[0];
	var newImage = anchor.get('rel').split(',')[1];
	var newId = href.split('-')[0];
	var clickedId = listEntry.id.split('-')[2];
	var currentImage = $('visual').getStyle('background-image');
	var newOpacityValue = 0;
	var urlParameter = '';
	if(currentImage.indexOf('/'+newImage) > 0){
		newOpacityValue = 1;
		urlParameter = '?fadeVisual=0';
	}
	$('visual').set('morph',
		{
			duration: 800
		}
	);
	if(rel > 600){
		$('content').get('morph').start( { 'opacity': 0, 'height': 0, 'overflow': 'hidden' } );
		$('wrapper').get('morph').start( { 'padding-bottom': 0 } );
		$('visual').get('morph').start( { 'height': rel, 'opacity': newOpacityValue, 'margin-bottom': 0 } ).chain(
			function(){
				var rootCategoryId = listEntry.id;
				if(listEntry.getParent('div').id != 'navigationLevel1' && rootCategoryId != 'logo'){
					rootCategoryId = getRootCategory(listEntry.getParent('div'));
					var rootAnchor = $(rootCategoryId).getChildren('a')[0];
					window.location = href + urlParameter;
				}else{
					window.location = href + urlParameter;
				}
			}
		);
	}else{
		if($('reference')){
			$('reference').get('morph').start( { 'opacity': [1,0] } );
		}
		$('visual').get('morph').start( { 'height': rel, 'opacity': newOpacityValue } ).chain(
			function(){
				var rootCategoryId = listEntry.id;
				if(listEntry.getParent('div').id != 'navigationLevel1' && rootCategoryId != 'logo'){
					window.location = href + urlParameter;
				}else{
					window.location = href + urlParameter;
				}
			}
		);
	}
	return false;
}

// setzt die schwarzen Balken zurück
function resetListBackground(){
	var currentItems = $$('.rex-current');
	var num_currentItems = currentItems.length;
	var currentItem;
	var list;
	var position;
	var newBackgroundPosition;
	var lists = $$('.rex-navi1');
	var num_lists = lists.length;
	lists.each(
		function(list){
			newBackgroundPosition = '0 ' + breadcrumbPositions + 'px';
			list.set('morph',
				{
					duration: 250,
					link: 'cancel'
				}
			);
			list.get('morph').start( { 'background-position': [newBackgroundPosition] } );
		}
	);

	currentItems.each(
		function(currentItem){
			list = currentItem.getParent('ul');
			position = currentItem.getPosition(list);
			newBackgroundPosition = '0 ' + breadcrumbPositions + 'px';
			list.set('morph',
				{
					duration: 250,
					link: 'cancel'
				}
			);
			list.get('morph').start( { 'background-position': [newBackgroundPosition] } );
		}
	);
	var activeItems = $$('.rex-active');
	var num_activeItems = activeItems.length;
	var activeItem;
	activeItems.each(
		function(activeItem){
			if(activeItem.getParent('div').className != 'articleNavigation'){
				list = activeItem.getParent('ul');
				position = activeItem.getPosition(list);
				newBackgroundPosition = '0 ' + breadcrumbPositions + 'px';
				list.set('morph',
					{
						duration: 250,
						link: 'cancel'
					}
				);
				list.get('morph').start( { 'background-position': [newBackgroundPosition] } );
			}
		}
	);
}

// schwarzer Balken folgt der Maus
function setListBackground(){
	var listEntry = this;
	var parentList = listEntry.getParent('ul');
	if(parentList.get('allowShowSubNav') != 'false'){
		var list = listEntry.getParent('ul');
		var position = listEntry.getPosition(list);
		var newBackgroundPosition = '0px '+position.y+'px';
		list.set('morph',
			{
				duration: 250
			}
		);
		list.get('morph').start( { 'background-position': [newBackgroundPosition] } );
	}
}

// setzt die Navigation zurück
function resetNavigation(){
	clearTimeout(showSubNavDelay);
	var subNavigations = $$('.subNav');
	var num_subNavigations = subNavigations.length;
	var subNavigation;
	var height;
	var list;
	var listElements;
	var num_listElements;
	var listEntry;
	var currentPosition;
	subNavigations.each(
		function(subNavigation){
			height = subNavigation.getStyle('height');
			if(subNavigation.className.indexOf('activeSubNav') > -1){
				subNavigation.get('morph').start( { 'height': [135,135] } );
				list = subNavigation.getChildren('ul')[0];
				if(list){
					listElements = list.getChildren('li');
					num_listElements = listElements.length;
					for(var j=0;j<num_listElements;j++){
						listEntry = listElements[j];
						if(listEntry.className.indexOf('rex-normal') < 0){
							currentPosition = j*17;
							listEntry.get('morph').start( { 'margin-top': breadcrumbPositions-currentPosition } );
						}else{
							listEntry.get('morph').start( { 'visibility': 'hidden' } );
						}
					}
				}
				subNavigation.get('morph').start( { 'height': [135,135], 'opacity': 1 } );
			}else{
				subNavigation.get('morph').start( { 'height': [0,0] } );	
			}
		}
	);
	currentSubNavId = '';
}

function closeSubNav(listEntry){
	var subNavId = 'subNav_' + listEntry.id.split('-')[2];
	var subNav = $(subNavId);
	if(subNav){
		var parentDiv = subNav.getParent('div');
		var subNavigations = parentDiv.getChildren('div');
		var num_subNavigations = subNavigations.length;
		var subNavigation;
		subNavigations.each(
			function(subNavigation){
				if(subNavigation.id != subNavId && subNavigation.getStyle('height') != '0px'){
					subNavigation.get('morph').cancel();
					subNavigation.setStyles(
						{
							'opacity': 0,
							'height': 0
						}
					);
				}
			}
		);
	}
}

// öffnet die Subnavigation bei Mouseover
function openSubNav(){
	clearTimeout(showSubNavDelay);
	var listEntry = this;
	var parentList = listEntry.getParent('ul');
	if(parentList.get('allowShowSubNav') != 'false'){
		var subNavId = 'subNav_' + listEntry.id.split('-')[2];
		var subNav = $(subNavId);

		var parentDiv = subNav.getParent();
		var subNavigations = parentDiv.getChildren('div');
		var num_subNavigations = subNavigations.length;
		var subNavigation;
		var num_listElements;
		var listElements;
		subNav.set('morph',
			{
				link: 'cancel'
			}
		);
		subNav.get('morph').start( {'height': 135, 'opacity': 1 } );
		subNavigations.each(
			function(subNavigation){
				if(subNavigation.id != subNavId){
					subNavigation.get('morph').cancel();
					subNavigation.setStyles(
						{
							'opacity': 0,
							'height': 0
						}
					);
					list = subNavigation.getChildren('ul')[0];
					if(list){
						listElements = list.getChildren('li');
						num_listElements = listElements.length;
						for(var j=0;j<num_listElements;j++){
							closeSubNav(listElements[j]);
							listElements[j].setStyles(
								{
									'visibility': 'visible',
									'margin-top': 0 
								}
							);
						}
					}
				}else{
					list = subNavigation.getChildren('ul')[0];
					if(list){
						list.get('morph').start( { 'background-position': '0px -16px' } );
						listElements = list.getChildren('li');
						num_listElements = listElements.length;
						for(var j=0;j<num_listElements;j++){
							listElements[j].get('morph').cancel();
							listElements[j].setStyles(
								{
									'visibility': 'visible',
									'margin-top': 0 
								}
							);
						}
					}
				}	
			}
		);
	}
	return false;
}

// Funktionen auf die Navigationspunkte legen (schwarze Hintergrundfläche verschieben, Unternavigation öffnen ...)
function setNavigationFunctions(){
	var navigation = $('navigation');
	navigation.addEvent('mouseleave', resetNavigation);
	navigation.addEvent('mouseleave', resetListBackground);

	// level 1
	navigation = $('navigationLevel1');
	var list = navigation.getChildren('ul')[0];
	var listElements = list.getChildren('li');
	listElements.each(
		function(listEntry){
			listEntry.addEvent('mouseenter', openSubNav.bind(listEntry));
			listEntry.addEvent('mouseenter', setListBackground.bind(listEntry));
			listEntry.addEvent('click', changePage.bind(listEntry));
		}
	);

	// level 2
	navigation = $('navigationLevel2');
	var subNavigations = navigation.getChildren('div');
	subNavigations.each(
		function(subNavigation){
			list = subNavigation.getChildren('ul')[0];
			if(list){
				listElements = list.getChildren('li');
				num_listElements = listElements.length;
				listElements.each(
					function(listEntry){
						listEntry.addEvent('mouseenter', openSubNav.bind(listEntry));
						listEntry.addEvent('mouseenter', setListBackground.bind(listEntry));
						listEntry.addEvent('click', changePage.bind(listEntry));
					}
				);
			}	
		}
	);

	// level 3
	navigation = $('navigationLevel3');
	var subNavigations = navigation.getChildren('div');
	subNavigations.each(
		function(subNavigation){
			list = subNavigation.getChildren('ul')[0];
			if(list){
				listElements = list.getChildren('li');
				listElements.each(
					function(listEntry){
						listEntry.addEvent('mouseenter', setListBackground.bind(listEntry));
						listEntry.addEvent('click', changePage.bind(listEntry));
					}
				);
			}
		}
	);
	
	navigation = $$('.systemNavigation')[0];
	var list = navigation.getChildren('ul')[0];
	var listElements = list.getChildren('li');
	listElements.each(
		function(listEntry){
			if(listEntry.id != ''){
				listEntry.addEvent('click', changePage.bind(listEntry));
			}
		}
	);

	var activeSubNavs = $$('.activeSubNav');
	var num_activeSubNavs = activeSubNavs.length;
	var activeSubNav;
	var parentId;
	for(var i=0;i<num_activeSubNavs;i++){
		activeSubNav = activeSubNavs[i];
		parentId = 'rex-article-'+activeSubNav.id.split('_')[1];
		activeSubNav.addEvent('mouseenter', openSubNav.bind($(parentId)));
	}
}

// Artikelnavigation
var startContent;
var currentInterviewSite = 0;

function resetArticleContents(){
	$('article').set('html',startContent);
}

function showArticleContents(){
	var listEntry = this;
	var list = listEntry.getParent('ul');
	var listElements = list.getChildren('li');
	var num_listElements = listElements.length;
	var listEntry2;
	for(var i=0;i<num_listElements;i++){
		listEntry2 = listElements[i];
		listEntry2.getChildren('a')[0].setStyle('color', '#999999');
	}
	var anchor = listEntry.getChildren('a')[0];
	anchor.setStyle('color', '#FFFFFF');
	var rel = anchor.get('rel').split(',')[0];
	var headlineRel = 'headline_' + rel.split('_')[1];
	// nur für Interview / History Seite
	if($$('.interviewNavigation').length > 0){
		currentInterviewSite = rel.split('_')[1] * 1;
		if( $('text_'+(currentInterviewSite+1)) ){
			$$('.nextHistoryPage')[0].setStyle('display','block');
		}else{
			$$('.nextHistoryPage')[0].setStyle('display','none');
		}
	}
	$('article').set('html',$(rel).get('html'));
	// Fix für Flash Video Bug IE (Kopie des HTML Codes enthält keinen Flash-Content, mit Javascript neu initialisieren)
	var tempContents = $(rel).getElement('script');
	if(tempContents){
		eval(tempContents.get('html'));
	}
	
	if($(headlineRel)){
		var allHeadlines = $$('.headline');
		var num_allHeadlines = allHeadlines.length;
		for(var i=0;i<num_allHeadlines;i++){
			headline = allHeadlines[i];
			headline.setStyle('display', 'none');
		}
		$(headlineRel).setStyle('display', 'block');
		$(headlineRel).get('morph').start( { 'opacity': 1 } );
		var myElement = $(document.body);
		var myFx = new Fx.Scroll(myElement).start(0,0);
		myFx.toTop();
	}
	setExternalLinks();
}

function resetArticleBackground(){
	var articleNavigation = this;
	var list = articleNavigation.getChildren('ul')[0];
	var listElements = list.getChildren('li');
	var num_listElements = listElements.length;
	var anchor;
	var listEntry;
	for(var i=0;i<num_listElements;i++){
		listEntry = listElements[i];
		anchor = listEntry.getChildren('a')[0];
		if(anchor.getStyle('color') == '#ffffff'){
			break;
		}
		
	}
	var newBackgroundPosition = '0 ' + (i * 17) + 'px';
	list.get('morph').start( { 'background-position': newBackgroundPosition } );
}

function getArticleBackground(){
	var listEntry = this;
	var anchor = listEntry.getChildren('a')[0];
	var href = anchor.href;
	var rel = anchor.get('rel').split(',')[0];
	var newImage = anchor.get('rel').split(',')[1];
	var currentImage = $('visual').getStyle('background-image');
	var newHeight = 350;
	if(rel != 'text_0'){
		newHeight = 160;
	}
	$('visual').set('morph',
		{
			duration: 500
		}
	);
	if(currentImage.indexOf('/'+newImage) > 0 && newImage != ''){
	//	window.location = href + '?fadeVisual=0';
		$('visual').get('morph').start( { 'height': newHeight } );
	}else{
		$('visual').get('morph').start( { 'opacity': 0, 'height': newHeight } ).chain(
			function(){
	//			window.location = href + '?fadeVisual=1';
				$('visual').setStyle('background-image', 'url(\'./files/'+newImage+'\')');
				this.start( { 'opacity': 1 } );
			}
		);
	}
	return false;
}

function setArticleNavigationFunctions(){
	var articleNavigations = $$('.articleNavigation');
	// falls die Navigation für das Interview / die Historie existiert
	if($$('.interviewNavigation').length > 0){
		articleNavigations = $$('.interviewNavigation');
		var nextPageButton = $$('.nextHistoryPage')[0];
		if(nextPageButton){
			nextPageButton.addEvent('click',
				function(){
					var nextPageContent = $('text_'+(currentInterviewSite+1));
					if(nextPageContent){
						$('article').set('html', nextPageContent.get('html'));
						currentInterviewSite = currentInterviewSite+1;
						var interviewList = articleNavigations[0].getChildren('ul')[0];
						var interviewLis = interviewList.getChildren('li');
						interviewLis.each(
							function(el){
								el.getChildren('a')[0].setStyle('color','#999999');
							}
						);
						interviewLis[currentInterviewSite].getChildren('a')[0].setStyle('color','#FFFFFF');
						var myElement = $(document.body);
						var myFx = new Fx.Scroll(myElement).start(0,0);
						myFx.toTop();
					}
					if($('text_'+(currentInterviewSite+1))){
						nextPageButton.setStyle('display', 'block');
					}else{
						nextPageButton.setStyle('display', 'none');
					}
				}
			);
		}
	}
	var num_articleNavigations = articleNavigations.length;
	var list;
	var listElements;
	var num_listElements;
	var listEntry;
	for(var i=0;i<num_articleNavigations;i++){
		articleNavigation = articleNavigations[i];
		articleNavigation.addEvent('mouseleave', resetArticleBackground.bind(articleNavigation));
		list = articleNavigation.getChildren('ul')[0];
		if(list){
			list.setStyle('background-position', '0px -16px');
			list.set('morph',
				{
					link: 'cancel'
				}
			);
			listElements = list.getChildren('li');
			num_listElements = listElements.length;
			for(var j=0;j<num_listElements;j++){
				listEntry = listElements[j];
				listEntry.addEvent('mouseenter', setListBackground.bind(listEntry));
				if(!$('catid_7')){
					listEntry.addEvent('click', showArticleContents.bind(listEntry));
					listEntry.addEvent('click', setListBackground.bind(listEntry));
					listEntry.addEvent('click',
						function(){
							return false;
						}					
					);
				}else{
					listEntry.addEvent('click', showArticleContents.bind(listEntry));
					listEntry.addEvent('click', getArticleBackground.bind(listEntry));
					listEntry.addEvent('click',
						function(){
							return false;
						}					
					);
				}
			}
		}
		if($('article').get('html').trim() == '' && $('text_0')){
			$('article').set('html', $('text_0').get('html'));
			list.setStyle('background-position', '0px 0px');
			list.getChildren('li')[0].getChildren('a')[0].setStyle('color','#ffffff');
		}
		if($('article')){
			startContent = $('article').get('html');
		}
	}
}

// starten der Visual-Animation bei Klick auf's Logo
function setLogoFunctions(){
	var visualHeight = 350;
	var anchor = $('logo').getChildren('a')[0];
	var href = anchor.get('href');
	anchor.addEvent('click',
		function(){
			return false;
		}
	);
	anchor.addEvent('click', changePage.bind($('logo')));
}

// News-Text Scrolling
function moveNewsList(){
	var button = this;
	var newsScrollContainer = $$('.newsScrollContainer')[0];
	var size = newsScrollContainer.getSize();
	var maxTop = size.y - 135;
	var currentTop = newsScrollContainer.getStyle('margin-top').toInt();
	var newsEntries = newsScrollContainer.getChildren('div');
	var num_newsEntries = newsEntries.length;
	var tempHeight = 0;
	var entry;
	for(var i=0;i<num_newsEntries;i++){
		if(tempHeight >= (-1 * currentTop)){
			break;
		}
		entry = newsEntries[i];
		tempHeight += entry.getSize().y;
	}
	if(button.id == 'newsScrollDown'){
		if(i < (num_newsEntries - 1)){
			newsScrollContainer.get('morph').start( { 'margin-top': -1 * ( tempHeight + newsEntries[i].getSize().y) } );
		}
	}else{
		if(i > 0){
			newsScrollContainer.get('morph').start( { 'margin-top': -1 * ( tempHeight - newsEntries[i-1].getSize().y) } );
		}	
	}
}

function setNewsScrollingFunction(){
	var newsScrollContainer = $$('.newsScrollContainer')[0];
	var upButton = $('newsScrollUp');
	var downButton = $('newsScrollDown');
	var scrollSpeed = 15;
	upButton.addEvent('click',moveNewsList.bind(upButton));
	downButton.addEvent('click', moveNewsList.bind(downButton));
}

function setTopNewsFunctions(){
	var news = $$('.topNews');
	var num_news = news.length;
	var entry;
	for(var i=0;i<num_news;i++){
		entry = news[i];
		entry.addEvent('mouseenter', 
			function(){
				this.setStyle('color', '#FFFFFF');
			}
		);
		entry.addEvent('mouseleave', 
			function(){
				this.setStyle('color', '');
			}
		);
	}
}

// Philosophie-Seite
var currentHeadlineParent;
function showPhilosophyText(){
	var parentDiv = this.getParent('div');
	currentHeadlineParent = parentDiv.id;
	var parentDiv = this.getParent('div');
	var textBlock = parentDiv.getChildren('div')[0];
	var philosophyText = $('philosophyTextTop');
	if(parentDiv.id.split('_')[1] > 2){
		philosophyText = $('philosophyTextBottom');
	}
	if(philosophyText.id == 'philosophyTextBottom'){
		$('philosophyTextTop').get('morph').start( { 'height': 0 } );
		if($('philosophyTextBottom').getStyle('height').toInt() > 10){
			$('philosophyTextBottom').get('morph').start( { 'height': 0, 'opacity': 0 } ).chain(
				function(){
					philosophyText.set('html', textBlock.get('html'));
					var size = philosophyText.getChildren('div')[0].getSize();
					var fx = function(){
						philosophyText.get('morph').start( { 'height': size.y + 10, 'opacity': [1,1] } );
					}
					fx.delay(200);
				}
			);
		}else{
			philosophyText.set('html', textBlock.get('html'));
			var size = philosophyText.getChildren('div')[0].getSize();
			philosophyText.get('morph').start( { 'height': size.y + 10 } );		
		}
	}else{
		$('philosophyTextBottom').get('morph').start( { 'height': 0 } );		
		if($('philosophyTextTop').getStyle('height').toInt() > 10){
			$('philosophyTextTop').get('morph').start( { 'height': 0, 'opacity': 0 } ).chain(
				function(){
					philosophyText.set('html', textBlock.get('html'));
					var size = philosophyText.getChildren('div')[0].getSize();
					var fx = function(){
						philosophyText.get('morph').start( { 'height': size.y + 10, 'opacity': [1,1] } );
					}
					fx.delay(200);
				}
			);
		}else{
			philosophyText.set('html', textBlock.get('html'));
			var size = philosophyText.getChildren('div')[0].getSize();
			philosophyText.get('morph').start( { 'height': size.y + 10 } );		
		}
	}

}

function showPhilosophyHeadline(){
	var entries = $$('.philosophyEntry');
	var num_entries = entries.length;
	var entry;
	var headline;
	var textBlock;
	var headlineParent;
	entries.each(
		function(entry){
			headline = entry.getChildren('h1')[0];
			headlineParent = headline.getParent('div');
			if(headlineParent.id != currentHeadlineParent && headline.getStyle('height').toInt() > 55){
				headline.get('morph').start( { 'height': 55, 'margin-top': 0 } );
			}
		}
	);
	this.get('morph').start( { 'height': 84, 'margin-top': -10 } );
}

function hidePhilosophyHeadline(){
	var clickedElement = this;
	var clickedParentDiv = clickedElement.getParent('div');
	var entries = $$('.philosophyEntry');
	entries.each(
		function(entry){
			if(entry.id != currentHeadlineParent){
				var headline = entry.getElement('h1');
				if(headline.getStyle('height').toInt() > 55 ){
					headline.get('morph').start( { 'height': 55, 'margin-top': 0 } );
				}
			}
		}
	);
}

function hidePhilosophyHeadlineClick(){
	var clickedElement = this;
	var clickedParentDiv = clickedElement.getParent('div');
	var entries = $$('.philosophyEntry');
	entries.each(
		function(entry){
			if(entry.id != clickedParentDiv.id){
				var headline = entry.getElement('h1');
				if(headline.getStyle('height').toInt() > 55 ){
					headline.get('morph').start( { 'height': 55, 'margin-top': 0 } );
				}
			}
		}
	);
}

function hidePhilosophyText(){
	var headline = this;
	var parentDiv = headline.getParent('div');
	var philosophyText = $('philosophyTextTop');
	headline.get('morph').start( { 'height': 55, 'margin-top': 0 } );
}

function setPhilosophyFunctions(){
	if($$('.philosophy')){
		var entries = $$('.philosophyEntry');
		var headline;
		var textBlock;
		entries.each(
			function(entry){
				headline = entry.getChildren('h1')[0];
				headline.set('morph',
					{
						chain: 'cancel'
					}
				);
				headline.addEvent('mouseenter',showPhilosophyHeadline.bind(headline));
				headline.addEvent('mouseleave',hidePhilosophyHeadline.bind(headline));
				headline.addEvent('click',hidePhilosophyHeadlineClick.bind(headline));
				headline.addEvent('click',showPhilosophyText.bind(headline));		
			}
		);
	}
}

// Zitate auf der Startseite
var quoteInterval = null;

function startQuoteAnimation(){
	var quotes = $('visual').getChildren('div');
	var num_quotes = quotes.length;
	var nextQuote;
	var speed = 875;
	var defaultPaddingLeft = 180;
	var paddingDifference = 20;
	for(var i=0;i<num_quotes;i++){
		quote = quotes[i];
		quote.set('morph',
		{
			transition: 'circ:out',
			duration: speed
		}
		);
		if(quote.getStyle('visibility') == 'visible'){
			quote.get('morph').start( { 'opacity': 0, 'padding-left': defaultPaddingLeft-paddingDifference, 'width': 0 } ).chain(
				function(){
					this.start( { 'display': 'none', 'height': [0,0] } );
				}
			);
			nextQuote = quotes[i+1];
		}
	}
	if(nextQuote){
		quote = nextQuote;
	}else{
		quote = quotes[0];
	}
	var fx = function(){
		quote.setStyle('display', 'block');
		quote.get('morph').start( { 'opacity': [0,1], 'padding-left': [defaultPaddingLeft+paddingDifference,defaultPaddingLeft], 'height': [220,220], 'width': [936,936] } );
	}
	fx.delay(speed + 50);
}

function setQuoteFunctions(){
	quoteInterval = window.setInterval('startQuoteAnimation()', 9000);
}

// Referenzen
function getReferenceEntry(position){
	var referenceEntry;
	var references = $$('.referenceEntry');
	var num_references = references.length;
	var reference;
	if($(currentReferenceId) && !(num_references < 2)){
		for(var i=0;i<num_references;i++){
			reference = references[i];
			if(reference.id == currentReferenceId){
				if(i>0 && i<(num_references-1)){
					referenceEntry = references[i+position];
				}else{
					if(i==0 && position==-1){
						referenceEntry = references[num_references-1];
					}
					if(i==0 && position==1){
						referenceEntry = references[i+position];
					}
					if(i==(num_references-1) && position == 1){
						referenceEntry = references[0];
					}
					if(i==(num_references-1) && position == -1){
						referenceEntry = references[i+position];
					}
				}
			}
		}
	}else{
		referenceEntry = references[0];
	}
	return referenceEntry;
}

function showReference(){
	var button = this;
	var defaultLeftPosition = 106;
	var difference = 20;
	var speed = 875;
	if(button.id == 'next'){
		var referenceEntry = getReferenceEntry(1);
		$(currentReferenceId).get('morph').start( { 'left': defaultLeftPosition-difference, 'opacity': 0 } ).chain(
			function(){
				$(currentReferenceId).setStyle('display', 'none');
				currentReferenceId = referenceEntry.id;
				var fx = function(){
					$(currentReferenceId).setStyles(
						{
							'display': 'block',
							'opacity': 0
						}
					);
					$(currentReferenceId).get('morph').start( { 'left': [defaultLeftPosition+difference, defaultLeftPosition], 'opacity': [0,1] } );
				}
				fx.delay(50);
			}
		);
	}else{
		var referenceEntry = getReferenceEntry(-1);
		$(currentReferenceId).get('morph').start( { 'left': defaultLeftPosition+difference, 'opacity': 0 } ).chain(
			function(){
				$(currentReferenceId).setStyles(
					{
						'display': 'block',
						'opacity': 0
					}
				);
				currentReferenceId = referenceEntry.id;
				var fx = function(){
					$(currentReferenceId).setStyles(
						{
							'display': 'block',
							'opacity': 0
						}
					);
					$(currentReferenceId).get('morph').start( { 'left': [defaultLeftPosition-difference, defaultLeftPosition], 'opacity': [0,1] } );
				}
				fx.delay(50);
			}
		);
	}
}

function showClickedReference(){
	var anchor = this;
	var defaultLeftPosition = 106;
	var difference = 20;
	var rel = anchor.get('rel');
	if($(currentReferenceId)){
		$(currentReferenceId).get('morph').start( { 'left': defaultLeftPosition+difference, 'opacity': 0 } );
	}
	$(rel).setStyles(
		{
			'display': 'block',
			'opacity': 0
		}
	);
	$(rel).get('morph').start( { 'left': [defaultLeftPosition-difference, defaultLeftPosition], 'opacity': [0,1] } );
	currentReferenceId = rel;
}

function showReferenceTitle(){
	var listEntry = this;
	var anchor = listEntry.getChildren('a')[0];
	var subtitleDiv = anchor.getChildren('span')[0];
	if(subtitleDiv){
		var size = subtitleDiv.getSize();
		if(size.y > 5){
			listEntry.get('morph').start( { 'height': 17+size.y } );
		}
	}
}

function hideReferenceTitle(){
	var listEntry = this;
	listEntry.get('morph').start( { 'height': 17 } );
}

function showReferenceTextBlock(){
	var listEntry = this;
	var pageNo = listEntry.id.split('_')[2];
	var parentDiv = listEntry.getParent('div').getParent('div');
	var textBlocks = parentDiv.getChildren('div');
	var num_textBlocks = textBlocks.length - 1;
	var textBlock;
	parentDiv.get('morph').start( { 'opacity': 0 } ).chain(
		function(){
			for(var i=0;i<num_textBlocks;i++){
				if( ((pageNo * 1) - 1) == i){
					if(i > 0){
						parentDiv.getChildren('h2')[0].setStyle('display', 'none');
						parentDiv.getChildren('p')[0].setStyle('display', 'none');
						parentDiv.getChildren('h1')[0].setStyle('display', 'none');
					}else{
						parentDiv.getChildren('h2')[0].setStyle('display', '');
						parentDiv.getChildren('p')[0].setStyle('display', '');
						parentDiv.getChildren('h1')[0].setStyle('display', '');
					}
					textBlocks[i].setStyle('display', 'block');
				}else{
					textBlocks[i].setStyle('display', 'none');
				}
			}
			this.start( { 'opacity': 1 } );
		}
	);
}

function setReferenceFunctions(){
	if($('referenceNavigation')){
		var list = $('referenceNavigation');
		var listElements = list.getChildren('li');
		var num_listElements = listElements.length;
		var size;
		var listEntry;
		var anchor;
		for(var i=0;i<num_listElements;i++){
			listEntry = listElements[i];
			listEntry.addEvent('mouseenter', showReferenceTitle.bind(listEntry));
			listEntry.addEvent('mouseleave', hideReferenceTitle.bind(listEntry));
			anchor = listEntry.getChildren('a')[0];
			anchor.addEvent('click', showClickedReference.bind(anchor));
		}
	}
	if($('previous')){
		$('previous').addEvent('click', showReference.bind($('previous')));
	}
	if($('next')){
		$('next').addEvent('click', showReference.bind($('next')));
	}
	var pageNavigations = $$('.pageNavigation');
	var num_pageNavigations = pageNavigations.length;
	var pageNav;
	var list;
	var listElements;
	var num_listElements;
	var listEntry;
	var parentDiv;
	var textBlocks;
	var textBlock;
	for(var i=0;i<num_pageNavigations;i++){
		pageNav = pageNavigations[i];
		parentDiv = pageNav.getParent('div');
		textBlocks = parentDiv.getChildren('div');
		list = pageNav.getChildren('ul')[0];
		listElements = list.getChildren('li');
		num_listElements = listElements.length;
		for(var j=0;j<num_listElements;j++){
			listEntry = listElements[j];
			textBlock = textBlocks[j];
			listEntry.addEvent('click',function(){ return false; });
			listEntry.addEvent('click',showReferenceTextBlock.bind(listEntry));
			if(j > 0){
				textBlock.setStyle('display', 'none');
			}
		}
	}
}

// Standortkarte
var currentLocationAddress;
function showLocationAddress(){
	if(currentLocationAddress){
		currentLocationAddress.addEvent('mouseleave', hideLocationAddress.bind(currentLocationAddress));
		var currentAnchor = currentLocationAddress.getChildren('a')[0];
		var currentRel = currentAnchor.get('rel');
		$(currentRel).get('morph').start( { 'opacity': 0 } );
		currentLocationAddress = null;
	}
	var listEntry = this;
	var anchor = listEntry.getChildren('a')[0];
	var rel = anchor.get('rel');
	var mapImage = $$('.'+rel)[0];
	var topPosition = mapImage.getStyle('top').toInt();
	var leftPosition = mapImage.getStyle('left').toInt();
	var size = $(rel).getSize();
	var newTopPosition = topPosition - size.y;
	var newLeftPosition = leftPosition - size.x;
	mapImage.src = 'pics/map_cities_grau.png';
	$(rel).setStyle('opacity', 0);
	$(rel).setStyle('left', newLeftPosition);
	$(rel).setStyle('top', newTopPosition);
	$(rel).get('morph').start( { 'opacity': [0,1] } ); // , 'left': [newLeftPosition,newLeftPosition], 'top': [newTopPosition, newTopPosition]
}

function hideLocationAddress(){
	var listEntry = this;
	var anchor = listEntry.getChildren('a')[0];
	var rel = anchor.get('rel');
	$(rel).get('morph').start( { 'opacity': 0 } );
	var mapImage = $$('.'+rel)[0];
	mapImage.src = 'pics/map_cities.png';
}

function fixLocationAddress(){
	var listEntry = this;
	listEntry.removeEvents('mouseleave');
	currentLocationAddress = listEntry;
}

function setLocationFunctions(){
	if($('mapNavigation')){
		var list = $('mapNavigation');
		var listElements = list.getChildren('li');
		var num_listElements = listElements.length;
		var listEntry;
		var anchor;
		var rel;
		var mapImage;
		for(var i=0;i<num_listElements;i++){
			listEntry = listElements[i];
			anchor = listEntry.getChildren('a')[0];
			rel = anchor.get('rel');
			if($$('.'+rel).length > 0){
				mapImage = $$('.'+rel)[0];
				mapImage.title = '';
				mapImage.addEvent('mouseenter', showLocationAddress.bind(listEntry));
				mapImage.addEvent('mouseleave', hideLocationAddress.bind(listEntry));
				listEntry.addEvent('mouseenter', showLocationAddress.bind(listEntry));
				listEntry.addEvent('mouseleave', hideLocationAddress.bind(listEntry));
				listEntry.addEvent('click', fixLocationAddress.bind(listEntry));
			}
		}
		
	}
}

function openCompanyVideo(){
	var video = this;
	video.get('morph').start( { 'height': window.getSize().y });
}

function closeCompanyVideo(){
	this.get('morph').start( { 'height': 0 } );
}

function setVideoOverlays(){
	var videoLinks = $$('.video');
	var num_videoLinks = videoLinks.length;
	var link;
	var companyVideo = $$('.videoOverlay')[0];
	var flashDiv = companyVideo.getChildren('div')[0];
	flashDiv.addEvent('mouseenter',
		function(){
			companyVideo.removeEvents('click');
		}
	);
	flashDiv.addEvent('mouseleave',
		function(){
			companyVideo.addEvent('click', closeCompanyVideo.bind(companyVideo));
		}
	);
	companyVideo.addEvent('click', closeCompanyVideo.bind(companyVideo));
	for(var i=0;i<num_videoLinks;i++){
		link = videoLinks[i];
		link.addEvent('click', openCompanyVideo.bind(companyVideo));
		link.addEvent('click',
			function(){
				return false;
			}
		);
	}
}

// Zitate am Anfang verlinken auf Referenzseite
function showReferencePage(url){
	if(quoteInterval){
		clearInterval(quoteInterval);
	}
	$('wrapper').get('morph').start( { 'padding-bottom': 0 } );
	$('content').setStyle('overflow', 'hidden');
	$('content').get('morph').start( { 'opacity': 0, 'height': 0 } );
	$('visual').get('morph').start( { 'height': 682, 'margin-bottom': 0 } ).chain(
		function(){
			window.location = url;
		}
	);
	return false;
}

function openExternalLink(){
	window.open(this.href, 'externalSite');
	return false;
}

function setExternalLinks(){
	var links = $('content').getElements('a');
	var num_links = links.length;
	var anchor;
	for(var i=0;i<num_links;i++){
		anchor = links[i];
		if(anchor.get('href').indexOf('http://') > -1){
			anchor.addEvent('click', openExternalLink.bind(anchor));
			anchor.addEvent('click',
				function(){
					return false;
				}
			);
		}
	}
}

var activeFlashId = '';
var activeFlash;

function showFlashVideo(sliceId, file, width, height){
	var flashLayer = $('flashplayer_'+sliceId);
	if(activeFlashId != '' && activeFlashId != flashLayer.id){
		$(activeFlashId).get('morph').start( { 'height': 0, 'padding-top': 0 } ).chain(
			function(){
				$(activeFlashId).set('html','');
			}
		);
	}
	if(activeFlashId != flashLayer.id){
		flashLayer.get('morph').start( { 'height': height, 'padding-top': 5 } ).chain(
			function(){
				activeFlash = new SWFObject('player_rtmp.swf','mpl',width,height,'9');
				activeFlash.addParam('allowScriptAccess','always');
				activeFlash.addParam('allowfullscreen','true');
				activeFlash.addVariable('autostart','true');
				activeFlash.addParam('wmode','transparent');
				activeFlash.addVariable('id','P00015');
				activeFlash.addVariable('file',file);
				activeFlash.addVariable('streamer','rtmp://flv.daf.tmt.de/vod/');
				activeFlashId = 'flashplayer_'+sliceId;
				activeFlash.write('flashplayer_'+sliceId);			
			}
		);
	}else{
		$(activeFlashId).get('morph').start( { 'height': 0, 'padding-top': 0 } ).chain(
			function(){
				$(activeFlashId).set('html','');
				activeFlashId = '';
				activeFlash = null;
			}
		);
	}
	return false;
}

window.addEvent('domready',
	function(){
		setNavigationFunctions();
		if($$('.articleNavigation')){
			setArticleNavigationFunctions();
		}
		setLogoFunctions();
		setNewsScrollingFunction();
		if($$('.articleid_1').length != 1){
			if(fadeVisual){
				$('visual').set('morph',
					{
						duration: 400
					}
				);
				$('visual').get('morph').start( { 'opacity': [0,1] } );
			}else{
				$('visual').setStyle('visibility','visible');
				$('visual').setStyle('opacity',1);
				// $('visual').get('morph').start( { 'opacity': [1,1] } );
			}
		}else{
		//	setQuoteFunctions();
		}
		setTopNewsFunctions();
		setPhilosophyFunctions();
		setReferenceFunctions();
		setLocationFunctions();
		setVideoOverlays();
		setExternalLinks();
		if($('reference')){
			var fx = function(){
				$('reference').get('morph').start( { 'opacity': [0,1] } );
			}
			fx.delay(200);
		}
		
		// navigation zwingen sich einzufahren
		$('logo').addEvent('mouseover', resetNavigation);
		$('logo').addEvent('mouseover', resetListBackground);
		$('visual').addEvent('mouseover', resetNavigation);
		$('visual').addEvent('mouseover', resetListBackground);
		$('content').addEvent('mouseover', resetNavigation);
		$('content').addEvent('mouseover', resetListBackground);
		if(navigator.appName.indexOf("Explorer") != -1){
			if($$('.articleid_1').length > 0 || $$('.articleid_28').length > 0){
				// nix
			}else{
				Cufon.replace('#visual p', { fontFamily: 'UniverseMedium'});
			}
		}
		
/*		var flashStreams = $$('.flash_stream');
		if(flashStreams.length > 0){
			new Fx.Accordion($$('.videocaption'),$$('.flash_content'), {
				alwaysHide: true,
				display: -1
			});
		} */
	}
);

if(navigator.appName.indexOf("Explorer") != -1){
 // nix
}else{
	Cufon.replace('#visual p', { fontFamily: 'UniverseMedium'});
	Cufon.replace('.articleid_1 #visual p, h1, #reference h2', { fontFamily: 'UniverseRegular'});	
}
