$(function() {

	// Setting the animation
	var timeperslide = 5000;
	var easingT = 500;
	var easingFx = 'easeOutExpo';
	
	// create needed elements
	
	$('.teaser_slider').first().before('<div id="slidshow"><div id="teaserslidshow"><div id="sliderreel"></div></div></div>');
	
	$('.teaser_slider').each(function() {
		$(this).appendTo($('#sliderreel')); 
	});
    
	$('.text').each(function() {
    	if($(this).find('h1').html() == '' && $(this).find('p').html() == ''){
			$(this).remove();
		}
	});
	
	$('#teaserslidshow').after('<div id="slideshow-nav"></div><a href="#" id="slideshow-left" class="slideshow-arrow" style="top: 207px; display: none; "></a><a href="#" id="slideshow-right" class="slideshow-arrow" style="top: 207px; display: block; "></a>');

	function changeSlide( newSlide ) {
		// cancel any timeout
		clearTimeout( slideTimeout );
		
		// Clear Animation Queue for fast clickers
		$slidereel.clearQueue();
		
		// change the currSlide value
		currSlide = newSlide;
		
		// make sure the currSlide value is not too low or high
		if ( currSlide > maxSlide ) currSlide = 0;
		else if ( currSlide < 0 ) currSlide = maxSlide;
		
		// animate the slide reel
		$slidereel.animate({
			left : currSlide * -slideWidth
		}, easingT, easingFx, function() {
			// hide / show the arrows depending on which frame it's on
			if ( currSlide == 0 ) $slideLeftNav.hide();
			else $slideLeftNav.show();
			
			if ( currSlide == maxSlide ) $slideRightNav.hide();
			else $slideRightNav.show();
			
			// set new timeout if active
			if ( activeSlideshow ) slideTimeout = setTimeout(nextSlide, timeperslide);
		});
		
		// change the navigation indicator
		$('#slideshow-nav .nav-item-dot').removeClass('aktive');
		$('#slideshow-nav .nav-item:nth-child('+(currSlide+1)+') .nav-item-dot').addClass('aktive');
	}
	
	function nextSlide() {
		changeSlide(currSlide + 1);
	}
	
	// define some variables / DOM references
	var activeSlideshow = true,
	currSlide = 0,
	slideTimeout,
	$teaserslidshow = $('#teaserslidshow'),
	$slidereel = $teaserslidshow.find('#sliderreel'),
	maxSlide = $slidereel.children().length - 1,
	slideWidth = $slidereel.find('.teaser_slider').width(),
	$slideLeftNav = $('#slidshow').find('#slideshow-left'),
	$slideRightNav = $('#slidshow').find('#slideshow-right');
	
	$('.teaser_slider').each(function(){
		$('#slideshow-nav').append('<a href="#" class="nav-item"><span class="nav-item-dot"></span></a>');
	})
	
	$('.nav-item-dot').first().addClass('aktive');
	
	$('#sliderreel').width(slideWidth*(maxSlide+1));
	
	$('.slideshow-arrow').css({
		top: ($('.teaser_slider').height()/2)-28
	})
	
	// set navigation click events
	
	// left arrow
	$slideLeftNav.click(function(ev) {
		ev.preventDefault();
		
		activeSlideshow = false;
		
		changeSlide( currSlide - 1 );
	});
	
	// right arrow
	$slideRightNav.click(function(ev) {
		ev.preventDefault();
		
		activeSlideshow = false;
		
		changeSlide( currSlide + 1 );
	});
	
	// main navigation
	$('#slidshow').find('#slideshow-nav a.nav-item').click(function(ev) {
		ev.preventDefault();
		
		activeSlideshow = false;
		
		changeSlide( $(this).index() );
	});
	
	// change to single view
	
	if(maxSlide == 0){
		$('#slideshow-nav').remove();
		$('#slideshow-left').remove();
		$('#slideshow-right').remove();
		$('#slidshow').addClass('single')
	}
	// start the animation
	
 	slideTimeout = setTimeout(nextSlide, timeperslide);
});

$(window).load(function(){
	$('#left .linklist').each(function(index){
		
		$('#right .teaser').css({
			position: 'absolute'
		});
		
		var topM = $(this).next('a').find('.teaser').position().top;
		
		$('#right').find('.teaser:nth-child('+(index+2)+')').css({
			top: topM,
			position: 'absolute'
		});
		
		var teaserheight = $('#right').find('.teaser:nth-child('+(index+2)+')').outerHeight();
		
		$('#right').find('.teaser:nth-child('+(index+3)+')').css({
			top: parseInt(topM) + parseInt(teaserheight) + 20 ,
			position: 'absolute'
		});
		
		var rHeight = $('#right').find('.teaser:nth-child('+(index+2)+')').innerHeight()
		
		var lHeight = $(this).next('a').find('.teaser').innerHeight();
		
		if(parseInt(rHeight) > parseInt(lHeight)){
			$(this).next('a').find('.teaser').css({
				marginBottom: 60+(parseInt(rHeight) - parseInt(lHeight))
			})
		}
	});


	$('.teaser_normal_headline_plus, .teaser_normal_headline_pic').each(function(index){
		var link = $(this).find('h3').find('a').attr('href');
		
		$(this).find('img').parent().attr('href', link);
	})

	
	$('a').each(function(){
		if($(this).attr('href')){
			var href = $(this).attr('href').split('/');
			if(href[0] == 'http:' || href[0] == 'fileadmin'){
				$(this).attr('target','_blank');
			}
		}
	})
	
	
	var LastTopDist = $('#right').find('.teaser:last-child').position().top;
	var LastHeight = $('#right').find('.teaser:last-child').innerHeight();
	
	
	$('#right').height(LastTopDist+LastHeight-$('#right').position().top);


	$('#nav_main').prepend('<div id="act_arr" style="left:10px"></div>');
	
	var navLeft = parseInt($('.curr').position().left);
	
	var offset = parseInt(($('#nav_main .curr').outerWidth()-24)/2)
	
	$('#act_arr').css({ left:navLeft+offset });
	
	if($('#nav_sub').html() != null){
		$('#act_arr').css({ backgroundPosition: 'right bottom' });
	};
	
	
});
















