$(document).ready(function() {
	$('.teaserbox_images:first-child').addClass('active');
	$.each($('.teaserbox_image'), function(index,value) {
		$(this).css({opacity: 0.0});
	});
	$.each($('.teaserbox_description'), function(index,value) {
		$(this).css({'right':-$(this).outerWidth()-1});
	});
	$('.teaserbox_description:last').css({'right':0});
	$('.teaserbox_image:last').css({'opacity':1});
	$('.teaserbox_description:last').delay(3900).animate({right: -$('.teaserbox_description:last').outerWidth()-1},1000);
	$('.teaserbox_descriptions:first-child').addClass('active');
	
	setInterval( "slideSwitch()", 5000 );
	setInterval( "textSwitch()", 5000 );
});

function slideSwitch() {
    var $active = $('.teaserbox_images .teaserbox_image.active');

    if ( $active.length == 0 ) $active = $('.teaserbox_images .teaserbox_image:last');

    var $next =  $active.next().length ? $active.next()
        : $('.teaserbox_images .teaserbox_image:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function textSwitch() {
	var $active = $('.teaserbox_descriptions .teaserbox_description.active');
	
	if ( $active.length == 0 ) $active = $('.teaserbox_descriptions .teaserbox_description:last');
	
	var $next =  $active.next().length ? $active.next()
		: $('.teaserbox_descriptions .teaserbox_description:first');
	
		$active.addClass('last_active');
	
		$next.css({right: -$next.outerWidth()-1})
			.addClass('active')
			.animate({right: 0}, 1000, function() {
			})
			.delay(2900).animate({right: -$next.outerWidth()-1}, 1000, function() {
				$active.removeClass('active last_active');
			});
		
}
