$(document).ready(function(){
	
	$('#control-right').click(function() { rotateRigth(false); return false; });
	$('#control-left').click(function() { rotateLeft(false); return false; });
	
	$('.carousell-controls a.control, #middle-difference, #middle-service').click(function(){
		$('.disable-controls').show();
		showBig();
		goToId($(this).attr('href'),false);
		
		return false;
	});
	
	$('#control-randomizer, #control-randomizerIcon').click( function() {
		$('.disable-controls').show();
		showBig();
		randomizer();
		return false;
	});
	
	/*
	$('#carousel-home ul li.active').bind('click', function(){
		$(this).animate({ left:-85, top:-50 });
		$(this).find('img').animate({ width:330});
	})
	*/
	
	// TODO: delete
	//showBig();
	//
});

function showBig() {
	if (!$('#carousel-home').is(":visible")) {
		$('#small-difference-carousel, #small-services-carousel, h1.entry-title, h2.entry-sub').hide();
		$('.control-top').show();
		$('#carousel-home').show();	
		$('.video').click(function() {
			showModal($(this).attr('href'));
			return false;
		})
		
	}
}
function randomizer(){
	var randomnumber = Math.floor(Math.random()*$('#carousel-home ul li').length)
	var nextId = $('#carousel-home ul li:eq(' + randomnumber + ')').attr('id');
	if(nextId == $('#carousel-home ul li.active').attr('id')){
		randomizer();
		return false;
	} else {
		goToId('#' + nextId,false);
	}
}

function goToId(idToSeek,rotatingLeft){
	var active = $('#carousel-home ul li.active');
	
	if('#' + active.attr('id') == idToSeek){
		$('.disable-controls').hide();
		return false;
	} else {
		if(rotatingLeft){
			rotateLeft(idToSeek,rotatingLeft);
		} else {
			rotateRigth(idToSeek,rotatingLeft);
		}
		
	}
}

function rotateRigth(idToSeek){
	
	$('#control-right').unbind('click');
	
	var active = $('#carousel-home ul li.active');

	if(active.next().next().next().length == 0){
		$('#carousel-home ul li:eq(0)').appendTo($('#carousel-home ul'));
		$('#carousel-home ul').css({'left': parseInt($('#carousel-home ul').css('left')) + 160 + 'px'});
	}
	
	$('#carousel-home ul').animate({'left': '-=160px'}, function() { adjustRigthClasses(idToSeek) }); 

	active.next().next().next().css({top: 80, left:0})
	active.next().next().next().find('img').css({width:60, opacity: 0})
	
	active.next().next().next().animate({top: 25, left:0});
	active.next().next().next().find('img').animate({width:160, opacity:1});
	active.next().next().next().css('z-index',10);
	
	active.next().next().animate({top: 15, left:-20});
	active.next().next().find('img').animate({width:200});
	active.next().next().css('z-index',20);
	
	active.next().animate({top: -50, left:-80});
	active.next().find('img').animate({width:330});
	active.next().css('z-index',30);
	
	active.animate({top: 15, left:-20});
	active.find('img').animate({width:200});
	active.css('z-index',20);
	
	active.prev().animate({top: 25, left:0});
	active.prev().find('img').animate({width:160});
	active.prev().css('z-index',10);
	
	active.prev().prev().animate({top: 80, left:100});
	active.prev().prev().find('img').animate({width:60, opacity:0});
	active.prev().prev().css('z-index',0);
	
	return false;
}

function rotateLeft(idToSeek){
	$('#control-left').unbind('click');
	
	var active = $('#carousel-home ul li.active');

	if(active.prev().prev().prev().length == 0){
		$('#carousel-home ul li:last-child').prependTo($('#carousel-home ul'));
		$('#carousel-home ul').css({'left': '-160px'}); 
	}
	
	$('#carousel-home ul').animate({'left': '+=160px'}, function() { adjustLeftClasses(idToSeek) }); 
	
	active.prev().prev().prev().css({top: 80, left:100})
	active.prev().prev().prev().find('img').css({width:60, opacity: 0})
	
	active.prev().prev().prev().animate({top: 25, left:0});
	active.prev().prev().prev().find('img').animate({width:160, opacity:1});
	active.prev().prev().prev().css('z-index',10);
	
	active.prev().prev().animate({top: 15, left:-20});
	active.prev().prev().find('img').animate({width:200});
	active.prev().prev().css('z-index',20);
	
	active.prev().animate({top: -50, left:-80});
	active.prev().find('img').animate({width:330});
	active.prev().css('z-index',30);
	
	active.animate({top: 15, left: -20});
	active.find('img').animate({width:200});
	active.css('z-index',20);
	
	active.next().animate({top: 25, left:0});
	active.next().find('img').animate({width:160});
	active.next().css('z-index',10);
	
	active.next().next().animate({top: 80, left:0});
	active.next().next().find('img').animate({width:60, opacity:0});
	active.next().next().css('z-index',0);
	
	
	
	return false;
}

function adjustRigthClasses(idToSeek){
	var active = $('#carousel-home ul li.active');
	$('#carousel-home ul li.active').unbind();
	active.next().removeClass('second-level-right').addClass('active');
	active.next().next().removeClass('third-level-right').addClass('second-level-right');
	active.next().next().next().addClass('third-level-right');
	
	active.removeClass('active').addClass('second-level-left');
	
	active.prev().removeClass('second-level-left').addClass('third-level-left');
	active.prev().prev().removeClass('third-level-left');
	
	$('#control-right').click(function() { rotateRigth(false); return false; } );
	
	if(idToSeek){
		goToId(idToSeek,false);
	}
	
	$('#carousel-home ul li.active').bind('click', function(){
		$(this).animate({ left:-85, top:-50 });
		$(this).find('img').animate({ width:330});
	})
	
}

function adjustLeftClasses(idToSeek){
	var active = $('#carousel-home ul li.active');
	$('#carousel-home ul li.active').unbind();
	active.prev().removeClass('second-level-left').addClass('active');
	active.prev().prev().removeClass('third-level-left').addClass('second-level-left');
	active.prev().prev().prev().addClass('third-level-left');
	
	active.removeClass('active').addClass('second-level-right');
	active.next().removeClass('second-level-right').addClass('third-level-right');
	active.next().next().removeClass('third-level-right');

	$('#control-left').click(function() { rotateLeft(false); return false; } );
	
	if(idToSeek){
		goToId(idToSeek,true);
	}
	
	$('#carousel-home ul li.active').bind('click', function(){
		$(this).animate({ left:-85, top:-50 });
		$(this).find('img').animate({ width:330});
	})

}

