var contentElementWidth = 640;
var contentIndex = 0;
var imageIndex = 0;
var irWaitTime = 4000;
var animationTime = 500;
var captionWidth = 288; 
var rotateTimeout = new Array();
var elementCount = new Array();
 
$(document).ready(function() {
	
	$('.btn-next').click(function(e) {
		rotatorIndex = $('.gallery').index($(this).parent());
		totalList = $('.galleryList').eq(rotatorIndex).children('li').length;
		elementCount[rotatorIndex] = totalList - 2;
		currentImage = parseInt($('.currentImage').eq(rotatorIndex).text());
		nextImage = parseInt(currentImage);
		e.preventDefault();
		updateContent((nextImage), elementCount[rotatorIndex], rotatorIndex);
	});
	
	$('.btn-prev').click(function(e) {
		rotatorIndex = $('.gallery').index($(this).parent());
		totalList = $('.galleryList').eq(rotatorIndex).children('li').length;
		elementCount[rotatorIndex] = totalList - 2;
		currentImage = parseInt($('.currentImage').eq(rotatorIndex).text());
		prevImage = parseInt(currentImage - 2);
		e.preventDefault();
		updateContent((prevImage), elementCount[rotatorIndex], rotatorIndex);
	});
	
	$('.gallery').each(function(index) {
		rotatorIndex = index;
		elementCount[rotatorIndex] = $('.galleryList', this).children('li').length;
		
		$('.galleryList', this).width((elementCount[rotatorIndex] * contentElementWidth)+1280);
		$('.galleryList', this).css({'position': 'relative'});
		$('.galleryList', this).append('<li>'+$('.galleryList li:eq(0)', this).html()+'</li>');
		$('.galleryList', this).prepend('<li>'+$('.galleryList li:eq('+(elementCount[rotatorIndex]-1)+')', this).html()+'</li>');
		
		$(this).css({display: 'block'});
		
	    initGallery($(this), elementCount[rotatorIndex], imageIndex, contentIndex, rotatorIndex);
	});
	
});

function initGallery(selectedElement, elementCount, imageIndex, contentIndex, rotatorIndex) {
	$(selectedElement).animate({opacity: 1}, 1000, "swing", function() {
		rotateTimeout[rotatorIndex] = setTimeout(function() {
			rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
		}, irWaitTime);
	});
};

function updateContent(index, elementCount, rotatorIndex) {
    imageIndex = index;
    contentIndex = index-1;
    clearTimeout(rotateTimeout[rotatorIndex]);
    rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
}

function rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex) {
    imageIndex++;
	contentIndex = contentIndex == elementCount-1 ? 0 : contentIndex+1;	
	
    newH3 = $('.listH3').eq(imageIndex).text();
    newBody = $('.listBody').eq(imageIndex).text();
    $('.finalH3').eq(rotatorIndex).text(newH3);
    $('.finalBody').eq(rotatorIndex).text(newBody);
    
    var rotatorLocation = $('.gallery:eq('+rotatorIndex+')');
    if(imageIndex == elementCount+1){
        imageIndex = 1;
        $('.galleryList', rotatorLocation).css({right: '0px'});
        $('.currentImage').eq(rotatorIndex).text(imageIndex);
    } else if (imageIndex == 0){
    	imageIndex = elementCount;
    	newRight = (elementCount * contentElementWidth)+contentElementWidth;
    	$('.galleryList', rotatorLocation).css({right: newRight+'px'});
    	$('.currentImage').eq(rotatorIndex).text(imageIndex);
    } else {
	    $('.currentImage').eq(rotatorIndex).text(imageIndex);
    }
    $('.galleryList', rotatorLocation).stop(true, true);
    $('.galleryList', rotatorLocation).animate({right: (imageIndex*contentElementWidth)+'px'}, animationTime, "swing");
    
    
	rotateTimeout[rotatorIndex] = setTimeout(function() {
		rotateImage(elementCount, imageIndex, contentIndex, rotatorIndex);
	}, irWaitTime);
}
