var activeImage;
var imageCount;

var timeCounter;

$(document).ready(function() {
	
	imageCount = 0;
	timeCounter = 0;
	
	
	
	$(".ce_gallery").find(".image_container").each(function(i) {
		$(this).fadeOut(0);
		imageCount++;
	});
	
	
	
	$(".floating_gallery").find(".image_container").each(function(i) {
		
		$(this).show();
		imageCount--;
		
	});
	
	changeImage(0);
	
	$(".floating_gallery a").fancybox({
		'titleShow'		: false,
		'overlayColor'	: '#FFFFFF',
		'overlayOpacity': 0.8
	});
	
	
	
	$(".next_image").click(function() {
		nextImage();
	});
	
	$(".previous_image").click(function() {
		previousImage();
	});
	
	setInterval ( "everySecond()", 1000 );

});

function everySecond(){
	timeCounter++;
	if (timeCounter > 5){
		nextImage();
	}
}

function nextImage(){
	
	if (activeImage < imageCount-1){
		changeImage(activeImage+1);
	}else{
		changeImage(0);
	}
}

function previousImage(){
	
	if (activeImage > 0){
		changeImage(activeImage-1);
	}else{
		changeImage(imageCount-1);
	}
}

function changeImage(i){
	timeCounter = 0;
	
	activeImage = i;
	
	$(".ce_gallery").find(".image_container").each(function(j) {
		if (j < imageCount){
			if (j == activeImage){
				$(this).fadeIn(500);
			}else{
				$(this).fadeOut(500);
			}
		}
	});
}
