//based on Slideshow Code by John Raasch
//http://jonraasch.com/blog/a-simple-jquery-slideshow

jQuery.noConflict();

(function($){
	var fadeTime = 3000;
	var showTime = 10000;
	var crossfadeTime = 500;
	var slideShow;
	var slideShowLock = false;
	
	jQuery.extend(jQuery.easing, {
		easeInQuad: function (x, t, b, c, d) {
			return c*(t/=d)*t + b;
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
	});
	
	function prepareDom(){
		$('.stage_panel').each(function(index, item){
			$(item).parent().replaceWith($(item));
		});
		$('.stage_panel').css({opacity: 0.0}).hide();
		$('.stage_panel:first').addClass('active').css({opacity: 1.0}).show();
		$('#startpage_stage').prepend($('<ul id="slideshowControllers"><li><a id="nextSlide" href="#">&gt;</a></li><li class="slideshowControl"><a id="prevSlide" href="#">&lt;</a></li></ul>'));
		// for accordion
		$('.dach_portal .col_inner_right .csc-textpic-clear').remove();
		$('.dach_portal .col_inner_right .clear').each(function(index, item){
			$(item).replaceWith($(item).children());
		});
		$('.accordeon_container .csc-textpic-clear').remove();
		$('.accordeon_container .clear').each(function(index, item){
			$(item).replaceWith($(item).children());
		});
		// anchors
		$('a.blue-block').prepend('&raquo; ');
		$('a.red-stripe').append(' &raquo;');
	}
	
	function postprocessAccordion() {
		$('.ui-accordion-header h1').append('<span class="accordion-arrow"></span>');
	}
	
	function slideSwitchNext(fadeDuration) {
		if (slideShowLock == true) {
			return;
		}
		slideShowLock = true;
	    var active = $('.stage_panel.active');
	    if ( active.length == 0 ) active = $('.stage_panel:last');
	    var next =  active.next('.stage_panel').length ? active.next('.stage_panel') : $('.stage_panel:first');
		active.animate({opacity: 0.0}, fadeDuration, 'easeOutQuad', function(){
			active.hide();
		});
	    next.css({opacity: 0.0})
	    	.show()
	        .addClass('active')
	        .animate({opacity: 1.0}, fadeDuration, 'easeInQuad', function() {
	            active.removeClass('active');
	            slideShowLock = false;
	        });
	}
	
	function slideSwitchPrev(fadeDuration) {
		if (slideShowLock == true) {
			return;
		}
		slideShowLock = true;
	    var active = $('.stage_panel.active');
	    if ( active.length == 0 ) active = $('.stage_panel:last');
	    var prev =  active.prev('.stage_panel').length ? active.prev('.stage_panel') : $('.stage_panel:last');
		active.animate({opacity: 0.0}, fadeDuration, 'easeOutQuad', function(){
			active.hide();
		});
		prev.css({opacity: 0.0})
	    	.show()
	        .addClass('active')
	        .animate({opacity: 1.0}, fadeDuration, 'easeInQuad', function() {
	            active.removeClass('active');
	            slideShowLock = false;
	        });
	}
	
	$(document).ready(function(){
		
		prepareDom();
		$('.dach_portal .col_inner_right').accordion({ autoHeight: false });
		$('.accordeon_container').accordion({ autoHeight: false });
		postprocessAccordion();

		$('#prevSlide').click(function(e){
			e.preventDefault();
			slideSwitchPrev(crossfadeTime);
		});

		$('#nextSlide').click(function(e){
			e.preventDefault();
			slideSwitchNext(crossfadeTime);
		});
		slideShow = setInterval(function() { slideSwitchNext(fadeTime); }, showTime);

		$('#startpage_stage').hover(
			function() {
				window.clearInterval(slideShow);
			},
			function() {
				slideShow = setInterval(function() { slideSwitchNext(fadeTime); }, showTime);
			}
		);
	});
})(jQuery);

