function getArticleId(href){
	var articleId = 0;
	if(href.indexOf('article_id=') > -1){
		articleId = href.split('article_id=')[1];
	}else{
		articleId = href.split('-')[0];
	}
	return articleId;
}

function saveQuizValues(formEl){
	var ajaxArticleId = 74;
	var requestUrl = 'index.php?article_id='+ajaxArticleId;
	new Request.HTML({
		url: requestUrl,
		onSuccess: function(responseText, responseXML, responseHTML){
			getNextQuiz($('nextQuizId').value);
		}
	}).post(formEl);
	return false;
}

function getNextQuiz(nextQuizId){
	var href = nextQuizId;

	var newArticleId = getArticleId(href);
	
	var ajaxArticleId = 73;
	var requestMethod = 'get';
	var updateDomElement = $('temp_gedaechtnistraining');

	var requestUrl = 'index.php?article_id='+ajaxArticleId+'&pageid='+newArticleId+'&clang=0';
	
	var quiz = $$('.quiz');
	if(newArticleId == 71){
		ET_Event.click('Gedächtnistraining - Auswertung', '');
	}
	if(quiz.length > 0){
		new Request.HTML({
			method: requestMethod,
			update: updateDomElement,
			url: requestUrl,
			onSuccess: function(responseText, responseXML, responseHTML){
				quiz[quiz.length-1].get('morph').start( { 'left': -620 } ).chain(
					function(){
						$('gedaechtnistraining').set('html', $('article_content').get('html'));
						var new_quiz = $('gedaechtnistraining').getElement('.quiz');
						if(new_quiz) {							
							new_quiz.get('morph').start( { 'left': 0 } ).chain(
								function(){
									Cufon.refresh();
									setQuizFunctions();
									$('temp_gedaechtnistraining').set('html', '');
								}
							);
						}
					}
				);
			}
		}).send();
	}else{
		new Request.HTML({
			method: requestMethod,
			update: updateDomElement,
			url: requestUrl,
			onSuccess: function(responseText, responseXML, responseHTML){
				// alert($('article_content').get('html'));
				$('gedaechtnistraining').set('html', $('article_content').get('html'));
				Cufon.refresh();
				setQuizFunctions();
			}
		}).send();
	}
	return false;
}

function getCurrentNavigation(){
	var navigation = $('navigation');
	var currentNavigation = navigation.getElements('.rex-current');
	if(currentNavigation.length > 0){
		var currentElement = navigation.getElement('.rex-current');
		if(currentElement.getParent('ul').className != 'rex-navi1'){
			currentElement = currentElement.getParent('li');
		}
		currentElement = currentElement.getElement('ul');
		return currentElement;
	}else{
		return null;
	}
}

function changePage(){
	var anchor = this;
	var subNavigation = anchor.getParent('li').getElement('ul');
	var currentSubNavigation = getCurrentNavigation();
	subNavigation.setStyles(
		{
			'height': 'auto',
			'padding-top': 0,
			'padding-bottom': 0
		}
	);
	var height = subNavigation.getSize().y;
	subNavigation.setStyle('height', 0);

	if(currentSubNavigation){
		currentSubNavigation.setStyles(
			{
				'border-top': 'none',
				'visiblity': 'hidden',
				'opacity': 0
			}
		);
		currentSubNavigation.get('morph').start( { 'height': 0, 'padding-top': 0, 'padding-bottom': 0 } ).chain(
			function(){
				subNavigation.setStyles(
					{
						'border-top': '1px solid #84888B',
						'visibility': 'visible'
					}
				);
				subNavigation.get('morph').start( { 'height': height, 'padding-top': 8, 'padding-bottom': 8 } ).chain(
					function(){
						window.location.href = anchor.href;
					}
				);
			}
		);
		var parentAnchor = currentSubNavigation.getParent('li').getElement('a');
		parentAnchor.className = 'level1 rex-normal';
	}else{
		subNavigation.setStyles(
			{
				'border-top': '1px solid',
				'visibility': 'visible'
			}
		);
		subNavigation.get('morph').start( { 'height': height, 'padding-top': 8, 'padding-bottom': 8 } ).chain(
			function(){
				window.location.href = anchor.href;
			}
		);
	}
	anchor.className = 'level1 rex-current';
	Cufon.refresh();
}

function setNavigationFunctions(){
	var navigation = $('navigation');
	if(navigation){
		var lists = navigation.getChildren('ul');
		var toggler = new Array();
		var element = new Array();
		var currentElement = 0;
		var counter = 0;
		lists.each(
			function(list){
				var listElements = list.getChildren('li');
				listElements.each(
					function(entry){
						var anchor = entry.getChildren('a')[0];
						var subNavigation = entry.getChildren('ul')[0];
						toggler.push(anchor);
						if(anchor.className.indexOf('rex-current') != -1 || anchor.className.indexOf('rex-active') != -1 ){
							currentElement = counter;
						}
						if(subNavigation){
							element.push(subNavigation);
							anchor.addEvent('click',
								function(){
									return false;
								}
							);
						}else{
							subNavigation = new Element('ul',
								{
									'class': 'rex-navi2'
								}
							);
							element.push(subNavigation);
						}
						counter++;
					}
				);
			}
		);
		toggler.each(
			function(el){
				el.addEvent('click', changePage.bind(el));
			}
		);
	}
}

function setFlashVideo(swf_player, id, dom_element_id, width, height, flash_version, video_file, preview_image){
	var params = {
		allowscriptaccess: 'always',
		allowfullscreen: 'true',
		wmode: 'transparent'
	};
	var attributes = {};
	var flashvars = {
		file: 'files/'+video_file
	};
	
	flash_version = '9.0.0';
	
	swfobject.embedSWF(swf_player, dom_element_id, width, height, flash_version, false, flashvars, params, attributes);
}

function startFlashVideo(){
	var anchor = this;
	var rel = anchor.get('rel').split('|');
	var parentDiv = anchor.getParent('div');
	
	var swf_player = 'player.swf';
	var id = 'obj_'+parentDiv.id;
	var dom_element_id = parentDiv.id;
	var width = rel[0];
	var height = rel[1];
	var flash_version = "9.0.0";
	var video_file = anchor.get('href');
	
	var containerEl = $('kopfzeile');
	var textBox = containerEl.getElement('.text');
	var flashBox = containerEl.getElement('.header_image');
	
	var params = {
		allowscriptaccess: 'always',
		allowfullscreen: 'true',
		wmode: 'transparent'
	};
	var attributes = {};
	var flashvars = {
		file: 'files/'+video_file,
		autostart: 'true'
	};
	
	swfobject.embedSWF(swf_player, dom_element_id, width, height, flash_version, false, flashvars, params, attributes);
	
	containerEl.get('morph').start( { 'height': height } );
	textBox.get('morph').start( { 'height': height-10 } );
	flashBox.get('morph').start( { 'height': height } );
	
	ET_Event.videoStart(video_file, '');

	return false;
}

function setQuizBegriffeBehalten(){
	var delay = 90500;
	var begriffe = $$('.begriffe');
	if(begriffe.length > 0){
		var fx = function(){
			var width = begriffe[0].getSize().x;
			begriffe[0].get('morph').start( { 'left': -1*width } );
			$$('.begriffe_eingabe')[0].setStyle('display', 'block');
			$$('.begriffe_eingabe')[0].get('morph').start( { 'left': [width,0] } );
		}
		var inputButton = $('nextButton2').getElement('input');
		inputButton.addEvent('click', fx);
	}
}

function startQuiz(){
	$('los_button').setStyle('display', 'none');
	$('nextButton2').setStyle('display', 'block');
	setQuizBegriffeBehalten();
	var begriffe = $$('.begriffe');
//	begriffe[0].setStyle('left',620);
//	begriffe[0].getElement('.wordList').setStyle('margin-left', 620);
	begriffe[0].getElement('.wordList').get('morph').start( { 'margin-left': [620,0] } );
}

function setAuswertung(){

}

function setQuizFunctions(){
	var quiz = $$('.quiz');
	if(quiz.length > 0){
	//	setQuizBegriffeBehalten();
	//	setAuswertung();
	}
}


function initializeGoogleMaps() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
	var marker = new GMarker(new GLatLng(50.2308345, 8.6145278));
	map.addOverlay(marker);
	var mapControl = new GMapTypeControl();
	map.addControl(mapControl);
	map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(50.2308345, 8.6145278), 16);
  }
}

// Animation für NewsTeaser auf Startseite
var oNewsTeaserTimer = null;

function startNewsTeaserAnimation() {
	
	var bReturn = false;
	oNewsTeaserTimer = window.setInterval('showNextNewsTeaser()', 9000);
	
	if(oNewsTeaserTimer != null) {
		bReturn = true;
	}
	
	return bReturn;
	
}
function showNextNewsTeaser() {
	var oItem = null;
	// alle NewsTeaser holen
	var oaNewsTeaser = $$('.newsTeaserItem');
	var nAnzNewsTeaser = oaNewsTeaser.length;
	var nSpeed = 875;
	var oNextTeaser;
	if(nAnzNewsTeaser > 0){
		for(var i=0; i<nAnzNewsTeaser; i++) {
			oItem = oaNewsTeaser[i];
			oItem.set('morph',
			{
				transition: 'circ:out',
				duration: nSpeed
			}
			);
			if(oItem.getStyle('visibility') == 'visible'){
				oItem.get('morph').start( { 'opacity': 0, 'top': [10,100] } ).chain(
					function(){
						this.start( { 'display': 'none', 'height': [0,0] } );
					}
				);
				oNextTeaser = oaNewsTeaser[i+1];
			}
		}
	
		if(oNextTeaser){
			oItem = oNextTeaser;
		}else{
			oItem = oaNewsTeaser[0];
		}
	
		var fx = function(){
			oItem.setStyle('display', 'block');
			oItem.get('morph').start( { 'opacity': [0,1], 'top': [-100,10] } );
		}
		fx.delay(nSpeed + 50);
	}
}

// Animation für Diashow-Modul

var oDiashowTimer = null;
var oItem = null;
function startDiashowAnimation() {
	
	var bReturn = false;
	oDiashowTimer = window.setInterval('showNextDia()', 7000);
	
	if(oDiashowTimer != null) {
		bReturn = true;
	}
	
	return bReturn;
	
}
function showNextDia() {
	// alle Bilder holen
	var oaDia = $$('.diashowItem');
	var nAnzDia = oaDia.length;
	var nSpeed = 1500;
	
	var oCurDia;
	var oNextDia;
	
	if(nAnzDia > 0) {
		
		// aktuelles Bild auslesen
		for(var i=0; i<nAnzDia; i++) {
			if(oaDia[i].getStyle('visibility') == 'visible') {
				oCurDia = oaDia[i];
				oNextDia = oaDia[i+1];
				break;
			}
		}
		
		// nächstes Bild holen
		if(oNextDia){
			oItem = oNextDia;
		}else{
			oItem = oaDia[0];
		}
	
		// neues Bild einblenden
		oItem.set('morph',
		{
			transition: 'linear',
			duration: nSpeed
		}
		);
		var fx = function(){
			oItem.setStyle('display', 'block');
			oItem.get('morph').start( { 'opacity': [0,1] } );
		}
		fx.delay(0);
		
		// altes Bild ausblenden
		oItem = oCurDia;
		oItem.set('morph',
		{
			transition: 'circ:out',
			duration: nSpeed
		}
		);
		if(oItem.getStyle('visibility') == 'visible') {
			oItem.get('morph').start( { 'opacity': [1,0] } ).chain(
				function(){
					this.start( { 'display': 'none' } );
				}
			);
		}
	}
}

function startMonoslideshow(){
	var anchor = this;
	var rel = anchor.get('rel').split('|');
	var flashvars = {dataFile: "monoslideshow_"+rel[2]+".xml"};
	var params = {};
	var attributes = {};
	swfobject.embedSWF(
	"monoslideshow.swf", "flashContent",
	"620", "400", "9", false,
	flashvars, params, attributes
	);
	return false;
}

function initKenBurnsEffect(data,htmlId){
	var myShow = new Slideshow.KenBurns(htmlId, data, { captions: true, controller: true, delay: 5000, duration: 2000, height: 190, hu: '', width: 620 });
}

function showNextHeaderText(){
	var texts = $('kopfzeile').getElements('.text_fade');
	if(texts[0].getStyle('visibility') == 'visible'){
		texts[0].get('morph').start( { 'opacity': [1,0] } ).chain(
			function(){
				texts[1].get('morph').start( { 'opacity': [0,1] } );
			}
		);
	}else{
		if(texts[1].getStyle('visibility') == 'visible'){
			if(texts[2]){
				texts[1].get('morph').start( { 'opacity': [1,0] } ).chain(
					function(){
						texts[2].get('morph').start( { 'opacity': [0,1] } );
					}
				);
			}else{
				texts[1].get('morph').start( { 'opacity': [1,0] } ).chain(
					function(){
						texts[0].get('morph').start( { 'opacity': [0,1] } );
					}
				);
			}
		}else{
			if(texts[2].getStyle('visibility') == 'visible'){
				texts[2].get('morph').start( { 'opacity': [1,0] } ).chain(
					function(){
						texts[0].get('morph').start( { 'opacity': [0,1] } );
					}
				);
			}
		}
	}
}

function initHeaderTextAnimation(){
	if($('kopfzeile')){
		var texts = $('kopfzeile').getElements('.text_fade');
		if(texts.length > 1){
			texts.each(
				function(text){
					text.set('morph',
						{
							duration: 1500
						}
					);
				}
			);
			window.setInterval('showNextHeaderText()', 6500);
		}
	}
}

function openGedaechtnistraining(){
	Shadowbox.open({
	    content:    '<iframe src="66-0-Interaktives-Gedaechtnistraining.html?scriptLayer=1" width="620" height="500" frameborder="0" border="0" scrolling="no" />',
	    player:     "html",
	    title:      "",
	    height:     500, 
	    width:      620
	});
	return false;
}

// öffnet eine Seite in einem neuen Fenster, Timestamp an Fensternamen, damit wirklich jedesmal ein neues Fenster geöffnet wird
function openExternalLink(){
	var currentDate = new Date();
	window.open(this.href, 'externalSite_'+currentDate.getTime());
	return false;
}

// setzt auf Links mit externem Ziel die Funktion 'openExternalLink'
function initExternalLinks(){
	var anchors = $('content').getElements('a');
	anchors.each(
		function(anchor){
			if(anchor.get('href').indexOf('http://') > -1){
				anchor.addEvent('click', openExternalLink.bind(anchor));
				anchor.addEvent('click',
					function(){
						return false;
					}
				);
			}else{
				if(getArticleId(anchor.get('href')) == 66){
					anchor.addEvent('click',openGedaechtnistraining);
				}
			}
		}
	);
}

function trackDownload(){
	var link = this;
	var href = link.get('href').split('?filename=');
	filename = href[(href.length-1)];
	ET_Event.download(filename,'');
	window.open(link.get('href'), 'download');
	return false;
}

function initDownloadLinks(){
	$$('.downloadLink').each(
		function(link){
			link.addEvent('click', trackDownload);
		}
	);
}

window.addEvent('domready',
	function(){
		setNavigationFunctions();
		setQuizFunctions();
		// IE fix
		if($('kopfzeile')){
			var texts = $('kopfzeile').getElements('.text_fade');
			texts.each(
				function(text){
					var display = text.getStyle('display');
					if(display == 'none'){
						text.setStyles(
							{
								'display': 'block',
								'visibility': 'hidden',
								'z-index': 1
							}
						);
					}else{
						text.setStyles(
							{
								'visibility': 'visible',
								'z-index': 1
							}
						);
					}
				}
			);
		}
		initHeaderTextAnimation();
		var nCountDiashow = $$('.diashowItem');
		if(nCountDiashow.length > 0) {
			startDiashowAnimation();
		}
		$$('.start_flash_video').each(
			function(anchor){
				anchor.addEvent('click', startFlashVideo.bind(anchor));
			}
		);
		if($('rex-article-66')){
			$('rex-article-66').addEvent('click',openGedaechtnistraining);
		}
		initExternalLinks();
		initDownloadLinks();
	}
);
Shadowbox.init();

window.addEvent('domready',function() {
	new SmoothScroll({ duration:600, transitions: 'quart:ease:out' }, window); //700 milliseconds to get there
});

Cufon.replace('h2, #navigation a, #smallNav a, #metaNav a, #copyright, #kopfzeile .text, ul.listeAktuelles a',{ fontFamily: 'MetaPlusMedium', hover: true, hoverables: { a: true } });
Cufon.replace('h1, #smallNav a.rex-active, .auswertung h2', { fontFamily: 'MetaPlusBold'} );
