function doScroll(){
	clearInterval(restartInterval);
    $images = $('.carussel img');
    //$.log('index: '+index+' | liClicked: '+liClicked+' | index: '+index);
    for (var i = 0; i < $images.length; i++) {
        $image = $('.carussel img:eq(' + i + ')');
        $tempLI = $('.carussel li:eq(' + i + ')');
        
        var angle = ((i * ((Math.PI * 2) / $images.length) + speed) + (Math.PI * 2000)) % (Math.PI * 2);
        var cosAngle = Math.cos(angle);
        var tempTop = Math.sin(angle) * -radius + centerX;
        var s = tempTop / (radius * 3) + 0.3333;
        $image.attr('width', s * $image.attr('rel'));
        var tempLeft = cosAngle * radius + centerX;
        
        $tempLI.css({
            'z-index': 5
        });
        if (angle < Math.PI && angle > 0) {
        
        
            if (liClicked == i || (index % $images.length) == i) {
                //alert('test');
                if ((radius - tempLeft) < 0) {
                    acelleration = ((radius - tempLeft) - centerX) / 5000;
                }
                else {
                    acelleration = ((radius - tempLeft) + centerX) / 5000;
                }
                
            }
            
        }
        else {
            $tempLI.css({
                'z-index': 10
            });
            
            if (liClicked == i || (index % $images.length) == i) {
                acelleration = ((radius) - tempLeft) / 5000;
                if (acelleration < 0.00001 && acelleration > -0.00001) {
                    clearInterval(scrollInterval);
                    $('.slidelink:eq(' + i + ')').removeClass('hide');
                    if (liClicked == -1) {
					
						$.log('gleich gehts weiter');
                        setTimeout(function(){
                            if (liClicked == -1) {
                                index--;
                                liClicked = -1;
                                $('.slidelink').addClass('hide')
								clearInterval(scrollInterval);
                                scrollInterval = setInterval("doScroll()", 40);
                            }
                        }, 3000);
                        
                    }
					else
					{
						$.log('gleich gehts nicht weiter');
                        restartInterval = setInterval("restart()", 10000);
					}
                }
                
            }
        }
        
        //if(i==0)
        //	$.log(angle+" | "+speed);
        
        $tempLI.css({
            'left': tempLeft
        });
        $image.css({
            'left': -$image.width() / 2
        })
        speed += acelleration;
        
        
    }
}

function restart(){
	$.log('restart');
	clearInterval(restartInterval);
	
	index = $images.length * 1000 + liClicked - 1;
	liClicked = -1;
	acelleration = 0.01 / $images.length;
	clearInterval(scrollInterval);
	scrollInterval = setInterval("doScroll()", 40);
	$('.slidelink').addClass('hide')
				
}

var carouselRad = 300

var radius = carouselRad;
var centerX = carouselRad;
var speed = 0;
var scrollInterval;
var restartInterval;
var acelleration = 0;

var index = 0;
var liClicked = -1;

$(document).ready(function(){
    carouselRad = $('.carussel').width() / 2;
    radius = carouselRad;
    centerX = carouselRad;
    $images = $('.carussel img');
    carussellHeight = 0
    index = $images.length * 1000;
    acelleration = 0.01 / $images.length;
    for (var i = 0; i < $images.length; i++) {
        $image = $('.carussel img:eq(' + i + ')');
        $tempLI = $('.carussel li:eq(' + i + ')');
        $tempLI.attr('rel', i);
        $image.attr('rel', $image.width());
        //$image.attr('alt', $image.height());
        if ($image.height() > carussellHeight) {
            carussellHeight = $image.height()
        }
        $image.removeAttr('height')
		
        $tempLI.click(function(){
		
			$.log('CLICK -> index: '+index+' | liClicked: '+liClicked);
			
			
			if (((liClicked > -1) && (liClicked == $(this).attr('rel'))) || (index%($images.length) == $(this).attr('rel'))) {
				$.log('if');
                window.location = $('.slidelink:eq(' + $(this).attr('rel') + ')').attr('href');
            }
            /*else if ((liClicked > -1) && (liClicked == $(this).attr('rel'))) {
				$.log('if');
                index = $images.length * 1000 + liClicked - 1;
                liClicked = -1;
                acelleration = 0.01 / $images.length;
                clearInterval(scrollInterval);
                scrollInterval = setInterval("doScroll()", 40);
                $('.slidelink').addClass('hide')
            }*/
            else {
				$.log('else');
                index = -1;
                liClicked = $(this).attr('rel');
                clearInterval(scrollInterval);
                scrollInterval = setInterval("doScroll()", 40);
                $('.slidelink').addClass('hide')
                $('.slidelink:eq(' + $(this).attr('rel') + ')').removeClass('hide')
				//location.replace($('.slidelink:eq(' + $(this).attr('rel') + ')').attr('href'));
            }
        });
    }
    if ($.browser.msie) {
        scrollInterval = setInterval("doScroll()", 40);
    }
    else {
        $('.carussel img').hide()
        scrollInterval = setInterval("doScroll()", 40);
        $('.carussel img').fadeIn(1500)
    }
    //Anpassen der Carussellhöhe für centrierung
    
    $('.carussel').height(carussellHeight);
    $('.carussel').css('paddingTop', (($('.teaser').height() - carussellHeight)) / 2)
    $('.carussel a').addClass('slidelink').addClass('hide')
    $('.carussel .slidelink').each(function(index){
        var text = '>> ' + $(this).text();
        $(this).text(text)
    });
    $('.carussel a').appendTo('.teaser')
})

