$(document).ready(function(){
	smallsRotateLeft('small-difference-carousel','#non-existing');
	smallsRotateRigth('small-services-carousel','#non-existing');
	
	$('#small-difference-carousel ul li').click(function() { 
		showBig();
		var selectedId = $(this).attr('id').replace('difference-', '');
		goToId('#' + selectedId,false);
	});
	
	$('#small-services-carousel ul li').click(function() { 
		showBig();
		var selectedId = $(this).attr('id').replace('service-', '');
		goToId('#' + selectedId,true);
	});
	
});


function rotateAgain(carouselId, rotatingLeft){
	var active = $('#' + carouselId + ' ul li.active');
	if (!$('#carousel-home').is(":visible")) {
		setTimeout(function(){
			if(rotatingLeft){
				smallsRotateLeft(carouselId);
			} else {
				smallsRotateRigth(carouselId);
			}	
		},1200);
	}
}


function smallsRotateRigth(carouselId){
	
	var active = $('#' + carouselId + ' ul li.active');
	
	if(active.next().next().next().length == 0){
		$('#' + carouselId + ' ul li:eq(0)').appendTo($('#' + carouselId + ' ul'));
		$('#' + carouselId + ' ul').css({'left': parseInt($('#' + carouselId + ' ul').css('left')) + 90 + 'px'});
	}
	
	$('#' + carouselId + ' ul').animate({'left': '-=90px'}, function() { smallsAdjustRigthClasses(carouselId) }); 

	active.next().next().next().animate({top: 10, left:0});
	active.next().next().next().find('img').animate({width:90, opacity:1});
	active.next().next().next().css('z-index',10);

	active.next().next().animate({top: 5, left:-5});
	active.next().next().find('img').animate({width:110});
	active.next().next().css('z-index',20);

	active.next().animate({top: 0, left:-15});
	active.next().find('img').animate({width:120});
	active.next().css('z-index',30);
	
	active.animate({top: 5, left:-5});
	active.find('img').animate({width:110});
	active.css('z-index',20);

	active.prev().animate({top: 10, left:0});
	active.prev().find('img').animate({width:90});
	active.prev().css('z-index',10);

	active.prev().prev().animate({top: 20, left:0});
	active.prev().prev().find('img').animate({width:60, opacity:0});
	active.prev().prev().css('z-index',0);
	
	return false;
}

function smallsRotateLeft(carouselId){
	
	var active = $('#' + carouselId + ' ul li.active');

	if(active.prev().prev().prev().length == 0){
		$('#' + carouselId + ' ul li:last-child').prependTo($('#' + carouselId + ' ul'));
		$('#' + carouselId + ' ul').css({'left': '-90px'}); 
	}
	
	$('#' + carouselId + ' ul').animate({'left': '+=90px'}, function() { smallsAdjustLeftClasses(carouselId) }); 


	active.prev().prev().prev().animate({top: 10, left:0});
	active.prev().prev().prev().find('img').animate({width:90, opacity:1});
	active.prev().prev().prev().css('z-index',10);

	active.prev().prev().animate({top: 5, left:-5});
	active.prev().prev().find('img').animate({width:110});
	active.prev().prev().css('z-index',20);

	active.prev().animate({top: 0, left:-15});
	active.prev().find('img').animate({width:120});
	active.prev().css('z-index',30);
	
	active.animate({top: 5, left:-5});
	active.find('img').animate({width:110});
	active.css('z-index',20);

	active.next().animate({top: 10, left:0});
	active.next().find('img').animate({width:90});
	active.next().css('z-index',10);

	active.next().next().animate({top: 20, left:0});
	active.next().next().find('img').animate({width:60, opacity:0});
	active.next().next().css('z-index',0);


	return false;
}

function smallsAdjustRigthClasses(carouselId){
	var active = $('#' + carouselId + ' ul li.active');

	active.next().removeClass('second-level').addClass('active');
	active.next().next().removeClass('third-level').addClass('second-level');
	active.next().next().next().addClass('third-level');
	
	active.removeClass('active').addClass('second-level');
	
	active.prev().removeClass('second-level').addClass('third-level');
	active.prev().prev().removeClass('third-level');
		
	rotateAgain(carouselId,false);
}

function smallsAdjustLeftClasses(carouselId){
	var active = $('#' + carouselId + ' ul li.active');
	
	active.prev().removeClass('second-level').addClass('active');
	active.prev().prev().removeClass('third-level').addClass('second-level');
	active.prev().prev().prev().addClass('third-level');
	
	active.removeClass('active').addClass('second-level');
	active.next().removeClass('second-level').addClass('third-level');
	active.next().next().removeClass('third-level');

	rotateAgain(carouselId, true);
}

